From 07df8c913534e54911b69836dce3dd8d7dda6b2f Mon Sep 17 00:00:00 2001 From: Sujit Baniya Date: Tue, 22 Mar 2022 13:58:30 +0545 Subject: [PATCH] Change name to get URL from (#1831) * Add Global Layout for view render * Add test case for Views Layout * Update ctx_test.go * Add App Name function to pass custom app name * Remove json tag for function * Change func to string * Add test for AppName * Add RedirectToRoute and RedirectBack with fallback if referer in header not found * replace errors.New with fmt.Errorf * simplified code * Add tests for different formats * Add method to get route location and add benchmarks * Add ToString function * Fix error * rearrange case for fmt.Stringer * Fix bug for error return * Lock latest route for app.Name(namee string) * decreasing timeout for client test with timeout * remove println and adjust condition to > 0 * Change name to get route url * Change name to get route url * Update ctx.go Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com> Co-authored-by: RW Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com> --- ctx.go | 7 ++++++- ctx_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ctx.go b/ctx.go index 126c7ddd8b..926e6270a0 100644 --- a/ctx.go +++ b/ctx.go @@ -1079,7 +1079,7 @@ func (c *Ctx) Bind(vars Map) error { return nil } -// get URL location from route using parameters +// getLocationFromRoute get URL location from route using parameters func (c *Ctx) getLocationFromRoute(route Route, params Map) (string, error) { buf := bytebufferpool.Get() for _, segment := range route.routeParser.segs { @@ -1104,6 +1104,11 @@ func (c *Ctx) getLocationFromRoute(route Route, params Map) (string, error) { return location, nil } +// GetRouteURL generates URLs to named routes, with parameters. URLs are relative, for example: "/user/1831" +func (c *Ctx) GetRouteURL(routeName string, params Map) (string, error) { + return c.getLocationFromRoute(c.App().GetRoute(routeName), params) +} + // RedirectToRoute to the Route registered in the app with appropriate parameters // If status is not specified, status defaults to 302 Found. func (c *Ctx) RedirectToRoute(routeName string, params Map, status ...int) error { diff --git a/ctx_test.go b/ctx_test.go index ee5520b33e..6ff6660e27 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -2556,6 +2556,20 @@ func Benchmark_Ctx_Get_Location_From_Route(b *testing.B) { } } +// go test -run Test_Ctx_Get_Location_From_Route_name +func Test_Ctx_Get_Location_From_Route_name(t *testing.T) { + app := New() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(c) + app.Get("/user/:name", func(c *Ctx) error { + return c.SendString(c.Params("name")) + }).Name("User") + + location, err := c.GetRouteURL("User", Map{"name": "fiber"}) + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, "/user/fiber", location) +} + type errorTemplateEngine struct{} func (t errorTemplateEngine) Render(w io.Writer, name string, bind interface{}, layout ...string) error {