diff --git a/api/ctx.md b/api/ctx.md index f03da977c76..1a6c3b99c25 100644 --- a/api/ctx.md +++ b/api/ctx.md @@ -587,6 +587,35 @@ func (c *Ctx) GetRespHeaders() map[string]string ``` {% endcode %} +## GetRouteURL + +Generates URLs to named routes, with parameters. URLs are relative, for example: "/user/1831" + +{% code title="Signature" %} +```go +func (c *Ctx) GetRouteURL(routeName string, params Map) (string, error) +``` +{% endcode %} + +{% code title="Example" %} +```go +app.Get("/", func(c *fiber.Ctx) error { + return c.SendString("Home page") +}).Name("home") + +app.Get("/user/:id", func(c *fiber.Ctx) error { + return c.SendString(c.Params("id")) +}).Name("user.show") + +app.Get("/test", func(c *fiber.Ctx) error { + location, _ := c.GetRouteURL("user.show", fiber.Map{"id": 1}) + return c.SendString(location) +}) + +// /test returns "/user/1" +``` +{% endcode %} + ## Hostname Returns the hostname derived from the [Host](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host) HTTP header.