1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

add CustomPathWordFunc

This commit is contained in:
yale8848
2021-03-14 20:58:10 +08:00
parent 7272c76847
commit 8f3e6f7bbf
4 changed files with 53 additions and 6 deletions

View File

@@ -2,10 +2,10 @@ package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
"github.com/kataras/iris/v12/middleware/logger"
"github.com/kataras/iris/v12/middleware/recover"
"github.com/kataras/iris/v12/mvc"
"strings"
)
// This example is equivalent to the
@@ -40,6 +40,16 @@ func newApp() *iris.Application {
// Serve a controller based on the root Router, "/".
mvc.New(app).Handle(new(ExampleController))
// Add custom path func
mvc.New(app).SetCustomPathWordFunc(func(path, w string, wordIndex int) string {
if wordIndex == 0 {
w = strings.ToLower(w)
}
path += w
return path
}).Handle(new(ExampleControllerCustomPath))
return app
}
@@ -80,6 +90,13 @@ func (c *ExampleController) GetHello() interface{} {
return map[string]string{"message": "Hello Iris!"}
}
// GetHelloWorld serves
// Method: GET
// Resource: http://localhost:8080/hello/world
func (c *ExampleController) GetHelloWorld() interface{} {
return map[string]string{"message": "Hello Iris! DefaultPath"}
}
// BeforeActivation called once, before the controller adapted to the main application
// and of course before the server ran.
// After version 9 you can also add custom routes for a specific controller's methods.
@@ -105,6 +122,16 @@ func (c *ExampleController) CustomHandlerWithoutFollowingTheNamingGuide() string
return "hello from the custom handler without following the naming guide"
}
type ExampleControllerCustomPath struct{}
// GetHelloWorld serves
// Method: GET
// Resource: http://localhost:8080/helloWorld
func (c *ExampleControllerCustomPath) GetHelloWorld() interface{} {
return map[string]string{"message": "Hello Iris! CustomPath"}
}
// GetUserBy serves
// Method: GET
// Resource: http://localhost:8080/user/{username:string}