diff --git a/README_ES.md b/README_ES.md index a5657180..ff9854b2 100644 --- a/README_ES.md +++ b/README_ES.md @@ -1,6 +1,6 @@ -# Iris +# Iris [![build status](https://img.shields.io/travis/kataras/iris/master.svg?style=for-the-badge)](https://travis-ci.org/kataras/iris) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/iris) [![view examples](https://img.shields.io/badge/learn%20by-examples-0077b3.svg?style=for-the-badge)](https://github.com/kataras/iris/tree/master/_examples) [![chat](https://img.shields.io/gitter/room/iris_go/community.svg?color=blue&logo=gitter&style=for-the-badge)](https://gitter.im/iris_go/community) [![release](https://img.shields.io/badge/release%20-v11.2-0077b3.svg?style=for-the-badge)](https://github.com/kataras/iris/releases) diff --git a/README_GR.md b/README_GR.md index ea3d5253..8b9e4e9f 100644 --- a/README_GR.md +++ b/README_GR.md @@ -1,4 +1,4 @@ -# Iris +# Iris [![build status](https://img.shields.io/travis/kataras/iris/master.svg?style=for-the-badge)](https://travis-ci.org/kataras/iris) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/iris) [![view examples](https://img.shields.io/badge/learn%20by-examples-0077b3.svg?style=for-the-badge)](https://github.com/kataras/iris/tree/master/_examples) [![chat](https://img.shields.io/gitter/room/iris_go/community.svg?color=blue&logo=gitter&style=for-the-badge)](https://gitter.im/iris_go/community) [![release](https://img.shields.io/badge/release%20-v11.2-0077b3.svg?style=for-the-badge)](https://github.com/kataras/iris/releases) diff --git a/README_ZH.md b/README_ZH.md index 72c59361..269cf27f 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -1,6 +1,6 @@ -# Iris +# Iris [![build status](https://img.shields.io/travis/kataras/iris/master.svg?style=for-the-badge)](https://travis-ci.org/kataras/iris) [![report card](https://img.shields.io/badge/report%20card-a%2B-ff3333.svg?style=for-the-badge)](https://goreportcard.com/report/github.com/kataras/iris) [![view examples](https://img.shields.io/badge/learn%20by-examples-0077b3.svg?style=for-the-badge)](https://github.com/kataras/iris/tree/master/_examples) [![chat](https://img.shields.io/gitter/room/iris_go/community.svg?color=blue&logo=gitter&style=for-the-badge)](https://gitter.im/iris_go/community) [![release](https://img.shields.io/badge/release%20-v11.2-0077b3.svg?style=for-the-badge)](https://github.com/kataras/iris/releases) diff --git a/mvc/controller.go b/mvc/controller.go index a6c3ddfd..c469eb03 100644 --- a/mvc/controller.go +++ b/mvc/controller.go @@ -70,6 +70,9 @@ type ControllerActivator struct { // to register any custom controller's methods as handlers. router router.Party + macros macro.Macros + tmplParamStartIndex int + // initRef BaseController // the BaseController as it's passed from the end-dev. Value reflect.Value // the BaseController's Value. Type reflect.Type // raw type of the BaseController (initRef). @@ -116,6 +119,7 @@ func newControllerActivator(router router.Party, controller interface{}, depende // give access to the Router to the end-devs if they need it for some reason, // i.e register done handlers. router: router, + macros: *router.Macros(), Value: reflect.ValueOf(controller), Type: typ, // the full name of the controller: its type including the package path. @@ -133,6 +137,8 @@ func newControllerActivator(router router.Party, controller interface{}, depende errorHandler: errorHandler, } + fpath, _ := macro.Parse(c.router.GetRelPath(), c.macros) + c.tmplParamStartIndex = len(fpath.Params) return c } @@ -339,7 +345,7 @@ func (c *ControllerActivator) handleMany(method, path, funcName string, override } // parse a route template which contains the parameters organised. - tmpl, err := macro.Parse(path, *c.router.Macros()) + tmpl, err := macro.Parse(path, c.macros) if err != nil { c.addErr(fmt.Errorf("MVC: fail to parse the path for '%s.%s': %v", c.fullName, funcName, err)) return nil @@ -350,7 +356,7 @@ func (c *ControllerActivator) handleMany(method, path, funcName string, override // get the path parameters bindings from the template, // use the function's input except the receiver which is the // end-dev's controller pointer. - pathParams := getPathParamsForInput(tmpl.Params, funcIn[1:]...) + pathParams := getPathParamsForInput(c.tmplParamStartIndex, tmpl.Params, funcIn[1:]...) // get the function's input arguments' bindings. funcDependencies := c.dependencies.Clone() funcDependencies.AddValues(pathParams...) diff --git a/mvc/controller_handle_test.go b/mvc/controller_handle_test.go index 6634dedc..28a9b307 100644 --- a/mvc/controller_handle_test.go +++ b/mvc/controller_handle_test.go @@ -166,3 +166,20 @@ func TestControllerHandle(t *testing.T) { e.GET("/hi/param/empty/input/with/ctx/value").Expect().Status(httptest.StatusOK). Body().Equal("empty in but served with ctx.Params.Get('param2')= value == id == value") } + +type testControllerHandleWithDynamicPathPrefix struct { + Ctx iris.Context +} + +func (c *testControllerHandleWithDynamicPathPrefix) GetBy(id string) string { + params := c.Ctx.Params() + return params.Get("model") + params.Get("action") + id +} + +func TestControllerHandleWithDynamicPathPrefix(t *testing.T) { + app := iris.New() + New(app.Party("/api/data/{model:string}/{action:string}")).Handle(new(testControllerHandleWithDynamicPathPrefix)) + e := httptest.New(t, app) + e.GET("/api/data/mymodel/myaction/myid").Expect().Status(httptest.StatusOK). + Body().Equal("mymodelmyactionmyid") +} diff --git a/mvc/param.go b/mvc/param.go index 180f30da..269dea9a 100644 --- a/mvc/param.go +++ b/mvc/param.go @@ -7,7 +7,7 @@ import ( "github.com/kataras/iris/macro" ) -func getPathParamsForInput(params []macro.TemplateParam, funcIn ...reflect.Type) (values []reflect.Value) { +func getPathParamsForInput(startParamIndex int, params []macro.TemplateParam, funcIn ...reflect.Type) (values []reflect.Value) { if len(funcIn) == 0 || len(params) == 0 { return } @@ -42,7 +42,7 @@ func getPathParamsForInput(params []macro.TemplateParam, funcIn ...reflect.Type) continue } - funcDep, ok := context.ParamResolverByTypeAndIndex(in, param.Index) + funcDep, ok := context.ParamResolverByTypeAndIndex(in, startParamIndex+param.Index) if !ok { continue }