Skip to content

Commit

Permalink
Change name to get URL from (#1831)
Browse files Browse the repository at this point in the history
* 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 <rene@gofiber.io>
Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 22, 2022
1 parent 528b8b4 commit af339a8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ctx.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions ctx_test.go
Expand Up @@ -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 {
Expand Down

0 comments on commit af339a8

Please sign in to comment.