From 409f83ca66e28eca31d68801996007119c90d68f Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Mon, 2 Mar 2020 20:11:44 +0200 Subject: [PATCH] :information_source: add mvc 'ByWildcard' example as requested at #1459 Although, we already had a usage of this as a test at mvc/controller_test.go#testControllerRelPathFromFunc Former-commit-id: 66343933aa86750615ab89767f149f6636d03be7 --- _examples/README.md | 2 ++ _examples/mvc/basic/wildcard/main.go | 41 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 _examples/mvc/basic/wildcard/main.go 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, + } +}