diff --git a/_examples/README.md b/_examples/README.md index 7c5aff1b..a5163d31 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -162,6 +162,8 @@ Navigate through examples for a better understanding. ### MVC - [Hello world](mvc/hello-world/main.go) +- [Basic](mvc/basic/main.go) +- [Basic: wildcard](mvc/basic/wildcard/main.go) **NEW** - [Regexp](mvc/regexp/main.go) - [Session Controller](mvc/session-controller/main.go) - [Overview - Plus Repository and Service layers](mvc/overview) diff --git a/_examples/mvc/basic/wildcard/main.go b/_examples/mvc/basic/wildcard/main.go new file mode 100644 index 00000000..915d3e3d --- /dev/null +++ b/_examples/mvc/basic/wildcard/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "github.com/kataras/iris/v12" + "github.com/kataras/iris/v12/mvc" +) + +func main() { + app := iris.New() + usersRouter := app.Party("/users") + mvc.New(usersRouter).Handle(new(myController)) + // Same as: + // usersRouter.Get("/{p:path}", func(ctx iris.Context) { + // wildcardPathParameter := ctx.Params().Get("p") + // ctx.JSON(response{ + // Message: "The path parameter is: " + wildcardPathParameter, + // }) + // }) + + /* + curl --location --request GET 'http://localhost:8080/users/path_segment_1/path_segment_2' + + Expected Output: + { + "message": "The wildcard is: path_segment_1/path_segment_2" + } + */ + app.Listen(":8080") +} + +type myController struct{} + +type response struct { + Message string `json:"message"` +} + +func (c *myController) GetByWildcard(wildcardPathParameter string) response { + return response{ + Message: "The path parameter is: " + wildcardPathParameter, + } +}