Route
ctx.Request.Route provides information on the route GOX resolved to process the current request.
In addition to the route requested by the client, Route allows to consult the internal destination that will be executed, the pattern of the route that produced the coincidence, the method solved and the dynamic parameters extracted during the resolution process.
All information provided by Route is read-only and represents the final result of the resolution process carried out by GOX.
When an request does not match a custom route rule, Route continue to provide information on the route that the server will try to solve.
See the documentation for Route configuration to learn how to define routes, dynamic parameters and internal destinations.
Structure
ctx.Request.Route implement the following API:
Route
{
Params Collection[string]
Original() string
Pattern() string
Target() string
Method() string
IsMatched() bool
IsStatic() bool
IsAction() bool
} Properties
| Property | Description |
|---|---|
| Params | It contains the dynamic parameters extracted during the route resolution process. |
Methods
| Method | Description |
|---|---|
| Original() | Returns the original route requested by the client. |
| Pattern() | Returns the pattern of the route that produced the coincidence. |
| Target() | Returns the internal destination that GOX will resolve to process the request. |
| Method() | Returns the name of the method GOX will run to process the request. |
| IsMatched() | It indicates whether the request coincided with a custom route rule. |
| IsStatic() | It indicates whether the request will be treated as a static resource. |
| IsAction() | It indicates whether the request will be processed as an Action. |
Params
ctx.Request.Route.Params contains the dynamic parameters extracted during the route resolution process.
When an request matches a path that defines dynamic parameters, GOX automatically stores its values in this collection.
For example, if there is the following rule:
/blog/:slug -> /blog/show and the client requests:
/blog/introducing-gox the collection shall contain:
slug = introducing-gox func GET(ctx *gox.Context) gox.Page {
slug := ctx.Request.Route.Params.Get("slug")
return <p>{{ slug }}</p>
} Structure
ctx.Request.Route.Params implement the following API:
Params
{
Get(key string) string
GetAll(key string) []string
Has(key string) bool
IsEmpty() bool
Count() int
Keys() []string
Values() []string
Entries() map[string][]string
} All methods are read only.
Methods
| Method | Description |
|---|---|
| Get() | Returns the value associated with a parameter. |
| GetAll() | Returns all values associated with a parameter. |
| Has() | It indicates if a parameter exists. |
| IsEmpty() | It indicates whether the collection contains parameters. |
| Count() | Returns the number of parameters available. |
| Keys() | Returns the names of all parameters. |
| Values() | Returns the values of all parameters. |
| Entries() | Returns all parameters along with their values. |
Get()
Firma:func (Params) Get(key string) string
Get() returns the value associated with a parameter.
func GET(ctx *gox.Context) gox.Page {
slug := ctx.Request.Route.Params.Get("slug")
return <p>{{ slug }}</p>
} When the parameter does not exist, Get() return an empty string.
GetAll()
Firma:func (Params) GetAll(key string) []string
GetAll() returns all values associated with a parameter.
func GET(ctx *gox.Context) gox.Page {
values := ctx.Request.Route.Params.GetAll("slug")
return <p>{{ len(values) }}</p>
} In most cases, each parameter contains a single value.
Has()
Firma:func (Params) Has(key string) bool
Has() indicates if a parameter exists.
func GET(ctx *gox.Context) gox.Page {
if ctx.Request.Route.Params.Has("slug") {
return <p>Parameter available.</p>
}
return <p>Parameter not found.</p>
} This method is useful when a parameter can be optional.
IsEmpty()
Firma:func (Params) IsEmpty() bool
IsEmpty() indicates whether the collection contains parameters.
func GET(ctx *gox.Context) gox.Page {
if ctx.Request.Route.Params.IsEmpty() {
return <p>No parameters.</p>
}
return <p>Parameters available.</p>
} Count()
Firma:func (Params) Count() int
Count() returns the number of parameters available.
func GET(ctx *gox.Context) gox.Page {
total := ctx.Request.Route.Params.Count()
return <p>{{ total }}</p>
} Keys()
Firma:func (Params) Keys() []string
Keys() returns the names of all parameters.
func GET(ctx *gox.Context) gox.Page {
keys := ctx.Request.Route.Params.Keys()
return <p>{{ len(keys) }}</p>
} Values()
Firma:func (Params) Values() []string
Values() returns the values of all parameters.
func GET(ctx *gox.Context) gox.Page {
values := ctx.Request.Route.Params.Values()
return <p>{{ len(values) }}</p>
} Entries()
Firma:func (Params) Entries() map[string][]string
Entries() returns all parameters along with their values.
func GET(ctx *gox.Context) gox.Page {
params := ctx.Request.Route.Params.Entries()
return <p>{{ len(params) }}</p>
} Each key represents the name of a parameter and its associated value contains all the values available for that parameter.
Original()
Firma:func (Route) Original() string
Original() returns the original route requested by the client.
The value corresponds exactly to the route received by the server before GOX applies any resolution rule.
func GET(ctx *gox.Context) gox.Page {
path := ctx.Request.Route.Original()
return <p>{{ path }}</p>
} If the request is for:
/blog/introducing-gox Original() it shall return:
/blog/introducing-gox Pattern()
Firma:func (Route) Pattern() string
Pattern() returns the pattern of the route that produced the coincidence during the resolution process.
When the request matches a dynamic route, this method returns the pattern defined in the route configuration.
For example, if there is the following rule:
/blog/:slug -> /blog/show and the client requests:
/blog/introducing-gox the methods of Route they shall return:
Original() -> /blog/introducing-gox
Pattern() -> /blog/:slug
Target() -> /blog/show func GET(ctx *gox.Context) gox.Page {
pattern := ctx.Request.Route.Pattern()
return <p>{{ pattern }}</p>
} When the request does not match a custom route, Pattern() returns the same route that GOX will use to solve the request.
To learn how to define dynamic routes and custom patterns, see Route configuration.
Target()
Firma:func (Route) Target() string
Target() returns the internal destination that GOX will solve to process the request.
This value may differ from the original route when a custom route redirects the request internally to another destination.
func GET(ctx *gox.Context) gox.Page {
target := ctx.Request.Route.Target()
return <p>{{ target }}</p>
} If there is the following rule:
/blog/:slug -> /blog/show and the client requests:
/blog/introducing-gox Target() it shall return:
/blog/show Method()
Firma:func (Route) Method() string
Method() returns the name of the method GOX will run to process the request.
On traditional pages, the value usually matches the HTTP method solved.
func GET(ctx *gox.Context) gox.Page {
method := ctx.Request.Route.Method()
return <p>{{ method }}</p>
} Resultado:
GET Where the request corresponds to an Action, Method() returns the name of the Action that GOX will run.
func CreateUser(ctx *gox.Context) gox.Action {
method := ctx.Request.Route.Method()
// method == "CreateUser"
...
} IsMatched()
Firma:func (Route) IsMatched() bool
IsMatched() indicates whether the request coincided with a custom route during the resolution process.
func GET(ctx *gox.Context) gox.Page {
if ctx.Request.Route.IsMatched() {
return <p>Ruta personalizada.</p>
}
return <p>Ruta directa.</p>
} This method only indicates whether GOX used a custom route to solve the request.
IsStatic()
Firma:func (Route) IsStatic() bool
IsStatic() indicates whether the request will be treated as a static resource.
func GET(ctx *gox.Context) gox.Page {
if ctx.Request.Route.IsStatic() {
return <p>Static resource.</p>
}
return <p>Dynamic resource.</p>
} This method is useful when the same logic needs to differentiate between static resources and dynamic requests.
IsAction()
Firma:func (Route) IsAction() bool
IsAction() indicates whether the request will be processed as an Action.
func POST(ctx *gox.Context) gox.Page {
if ctx.Request.Route.IsAction() {
return <p>Action request.</p>
}
return <p>Regular request.</p>
} This method only reflects the outcome of the resolution process carried out by GOX.
Best practices
- Use
Original()when you need to know the route requested by the client. - Use
Pattern()to identify the custom route that produced the coincidence. - Use
Target()When you need to know the internal fate that GOX will solve. - Use
Method()when you need to know the method GOX will run to process the request. - Use
IsMatched()to determine whether the request was resolved by a custom route. - Use
Has()before access to optional parameters.
To learn more about URLSee your documentation. To learn more about Headers, check your documentation.