Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change name to get URL from #1831

Merged
merged 34 commits into from Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ae0cc9a
Merge pull request #4 from gofiber/master
sujit-baniya May 6, 2021
744fbe2
Merge branch 'gofiber:master' into master
sujit-baniya Jun 2, 2021
75b5c18
Merge branch 'gofiber:master' into master
sujit-baniya Jun 14, 2021
5a81a51
Add Global Layout for view render
sujit-baniya Jun 14, 2021
2437d8f
Add test case for Views Layout
sujit-baniya Jun 14, 2021
6f4f14f
Update ctx_test.go
ReneWerner87 Jun 14, 2021
3812595
Merge branch 'gofiber:master' into master
sujit-baniya Jun 23, 2021
27a01e7
Merge branch 'gofiber:master' into master
sujit-baniya Jun 27, 2021
2f05a64
Merge branch 'gofiber:master' into master
sujit-baniya Jul 1, 2021
ad8b4f9
Add App Name function to pass custom app name
sujit-baniya Jul 1, 2021
5b6e866
Remove json tag for function
sujit-baniya Jul 1, 2021
3ee7557
Change func to string
sujit-baniya Jul 1, 2021
1c6e102
Add test for AppName
sujit-baniya Jul 5, 2021
5dddb64
Merge branch 'master' into master
ReneWerner87 Jul 16, 2021
86ef9ce
Merge branch 'gofiber:master' into master
sujit-baniya Aug 21, 2021
3a367be
Merge branch 'gofiber:master' into master
sujit-baniya Feb 4, 2022
a5f6791
Add RedirectToRoute and RedirectBack with fallback if referer in head…
sujit-baniya Feb 4, 2022
82873c8
replace errors.New with fmt.Errorf
sujit-baniya Feb 4, 2022
8e8d92e
simplified code
sujit-baniya Feb 4, 2022
0215512
Add tests for different formats
sujit-baniya Feb 7, 2022
9013dc5
Add method to get route location and add benchmarks
sujit-baniya Feb 7, 2022
e716d23
Add ToString function
sujit-baniya Feb 7, 2022
d694752
Fix error
sujit-baniya Feb 8, 2022
05dd7ba
rearrange case for fmt.Stringer
sujit-baniya Feb 8, 2022
a9bc1f6
Fix bug for error return
sujit-baniya Feb 8, 2022
9c6a15d
Merge branch 'gofiber:master' into master
sujit-baniya Feb 8, 2022
3075c02
Lock latest route for app.Name(namee string)
sujit-baniya Feb 8, 2022
81455f0
Merge remote-tracking branch 'origin/master'
sujit-baniya Feb 8, 2022
245148e
decreasing timeout for client test with timeout
sujit-baniya Feb 8, 2022
4591b66
remove println and adjust condition to > 0
sujit-baniya Feb 9, 2022
9ef04dc
Merge branch 'w_m'
sujit-baniya Mar 21, 2022
b6899ec
Change name to get route url
sujit-baniya Mar 21, 2022
c60d023
Change name to get route url
sujit-baniya Mar 21, 2022
25c401f
Update ctx.go
sujit-baniya Mar 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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