diff --git a/adaptors/gorillamux/_example/main.go b/adaptors/gorillamux/_example/main.go index 1d6d8f0d..3bcda3f3 100644 --- a/adaptors/gorillamux/_example/main.go +++ b/adaptors/gorillamux/_example/main.go @@ -43,9 +43,18 @@ func main() { games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/demote", h) } - app.Get("/anything/{anythingparameter:.*}", func(ctx *iris.Context) { + myroute := app.Get("/anything/{anythingparameter:.*}", func(ctx *iris.Context) { s := ctx.Param("anythingparameter") ctx.Writef("The path after /anything is: %s", s) + }) // .ChangeName("myroute") + + app.Get("/reverse_myroute", func(ctx *iris.Context) { + // reverse routing snippet using templates: + // https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_3 (gorillamux) + // https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_4 (httprouter) + + myrouteRequestPath := app.Path(myroute.Name(), "anythingparameter", "something/here") + ctx.Writef("Should be '/anything/something/here': %s", myrouteRequestPath) }) p := app.Party("mysubdomain.") diff --git a/adaptors/gorillamux/gorillamux.go b/adaptors/gorillamux/gorillamux.go index 9c7aa412..5da6d074 100644 --- a/adaptors/gorillamux/gorillamux.go +++ b/adaptors/gorillamux/gorillamux.go @@ -64,9 +64,15 @@ func New() iris.Policies { // {{ url "providerLink" "provider" "facebook"}} // for a path: "/auth/{provider}" with name 'providerLink' URLPath: func(r iris.RouteInfo, args ...string) string { + if router == nil { + logger(iris.ProdMode, "gorillamux' reverse routing 'URLPath' should be called after Boot/Listen/Serve") + return "" + } + if r == nil { return "" } + if gr := router.Get(r.Name()); gr != nil { u, err := gr.URLPath(args...) if err != nil { diff --git a/adaptors/gorillamux/gorillamux_test.go b/adaptors/gorillamux/gorillamux_test.go index d2908dc2..7a3ef3c6 100644 --- a/adaptors/gorillamux/gorillamux_test.go +++ b/adaptors/gorillamux/gorillamux_test.go @@ -17,6 +17,6 @@ func TestRouteURLPath(t *testing.T) { expected := "/profile/42/iris-go/something" if got := app.Path("profile", "user_id", 42, "ref", "iris-go", "anything", "something"); got != expected { - t.Fatalf("httprouter's reverse routing 'URLPath' error: expected %s but got %s", expected, got) + t.Fatalf("gorillamux' reverse routing 'URLPath' error: expected %s but got %s", expected, got) } } diff --git a/iris.go b/iris.go index 6493c6f4..3130346b 100644 --- a/iris.go +++ b/iris.go @@ -730,7 +730,12 @@ func (s *Framework) Cache(bodyHandler HandlerFunc, expiration time.Duration) Han } // Path used to check arguments with the route's named parameters and return the correct url -// if parse failed returns empty string +// if parse failed returns empty string. +// Used for reverse routing, depends on router adaptor. +// +// Examples: +// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_3 (gorillamux) +// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_4 (httprouter) func (s *Framework) Path(routeName string, args ...interface{}) string { r := s.Router.Routes().Lookup(routeName) if r == nil { @@ -768,39 +773,13 @@ func (s *Framework) Path(routeName string, args ...interface{}) string { return s.policies.RouterReversionPolicy.URLPath(r, argsString...) } -// DecodeQuery returns the uri parameter as url (string) -// useful when you want to pass something to a database and be valid to retrieve it via context.Param -// use it only for special cases, when the default behavior doesn't suits you. +// URL returns the subdomain + host + Path(...optional named parameters if route is dynamic) +// returns an empty string if parse is failed. +// Used for reverse routing, depends on router adaptor. // -// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm -// it uses just the url.QueryUnescape -func DecodeQuery(path string) string { - if path == "" { - return "" - } - encodedPath, err := url.QueryUnescape(path) - if err != nil { - return path - } - return encodedPath -} - -// DecodeURL returns the decoded uri -// useful when you want to pass something to a database and be valid to retrieve it via context.Param -// use it only for special cases, when the default behavior doesn't suits you. -// -// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm -// it uses just the url.Parse -func DecodeURL(uri string) string { - u, err := url.Parse(uri) - if err != nil { - return uri - } - return u.String() -} - -// URL returns the subdomain+ host + Path(...optional named parameters if route is dynamic) -// returns an empty string if parse is failed +// Examples: +// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_3 (gorillamux) +// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_4 (httprouter) func (s *Framework) URL(routeName string, args ...interface{}) (url string) { r := s.Router.Routes().Lookup(routeName) if r == nil { @@ -833,6 +812,37 @@ func (s *Framework) URL(routeName string, args ...interface{}) (url string) { return } +// DecodeQuery returns the uri parameter as url (string) +// useful when you want to pass something to a database and be valid to retrieve it via context.Param +// use it only for special cases, when the default behavior doesn't suits you. +// +// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm +// it uses just the url.QueryUnescape +func DecodeQuery(path string) string { + if path == "" { + return "" + } + encodedPath, err := url.QueryUnescape(path) + if err != nil { + return path + } + return encodedPath +} + +// DecodeURL returns the decoded uri +// useful when you want to pass something to a database and be valid to retrieve it via context.Param +// use it only for special cases, when the default behavior doesn't suits you. +// +// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm +// it uses just the url.Parse +func DecodeURL(uri string) string { + u, err := url.Parse(uri) + if err != nil { + return uri + } + return u.String() +} + var errTemplateRendererIsMissing = errors.New( ` manually call of Render for a template: '%s' without specified RenderPolicy!