You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Route parameters represent segments in the route’s URI. For example, you may want to capture a user’s ID from the URI. You can do that by using route parameters. Route parameters
68
+
Route parameters represent segments in the route’s URI. For example, you may want to capture a user’s ID from the URI. You can do that by using route parameters.
69
69
70
+
Route parameters start with a colon, like `:userId`. The route parameter name follows after the colon and should consist of alphabetic characters. You can also use underscores (`_`) in route parameter names:
71
+
72
+
```ts
73
+
Route.get('/hello/:name', ({ request }) => {
74
+
const name =request.param('name')
75
+
76
+
return`Hello ${name}`
77
+
})
78
+
```
70
79
71
-
### Required Route Parameters
72
80
81
+
### Required Path Parameters
82
+
Required path parameters end with their name. The router matches only requests providing a name as the second URI segment:
73
83
74
84
```ts
75
-
Route.get('/:page', ShowDocs)
85
+
Route.get('/hello/:name', ({ request }) => {
86
+
const name =request.param('name')
87
+
88
+
return`Hello ${name}`
89
+
})
76
90
```
77
91
78
92
79
-
### Optional Route Parameters
93
+
### Optional Path Parameters
94
+
Sometimes it’s useful to define a path parameter that isn’t required to be present on a URI. You can specify optional path parameters by putting a `?` mark at the end of the parameter name:
80
95
81
96
```ts
82
-
Route.get('/:page?', ShowDocs)
97
+
Route.get('/hello/:name?', ({ request }) => {
98
+
const name =request.params().get('name', 'Supercharge')
99
+
100
+
return`Hello ${name}`
101
+
})
83
102
```
84
103
104
+
You’ve access to the path parameters on the [request](/docs/{{version}}/requests) instance. The `request` instance allows you to fetch path parameters and assign a default value in case no parameter value is present on the URI.
105
+
85
106
86
107
## Route Groups
87
-
Tba.
108
+
Route groups share attributes, like middleware or URI prefixes, across a number of routes without needing to assign these attributes on each route individually.
109
+
110
+
The Supercharge router also supports nested route groups. Attributes for nested route groups are merged with their parents. Middleware between route groups are merged. Path prefixes are appended and slashes will be added automatically to properly resolve route paths.
88
111
89
112
90
113
### Middleware
91
-
Tba.
114
+
You can assign [middlware](/docs/{{version}}/middleware) to a route group using the `middleware` method before defining the group. Pass the defined middleware names as the parameter to the route group’s `middleware` method:
// `first` and `second` middleware run for all routes defined in this group
119
+
120
+
Route.get('/', () => {
121
+
// …
122
+
})
123
+
})
124
+
```
92
125
93
126
94
127
### Prefixes
95
-
Tba.
128
+
You can assign URI prefixes to a route group using the `prefix` method. Each route in that group receives the provided prefix. For nested route groups with prefixes, Supercharge appends the prefixes from parents:
129
+
130
+
```ts
131
+
Route.prefix('/admin').group(() => {
132
+
Route.get('/', () => {
133
+
// resolves to route path `/admin`
134
+
})
135
+
136
+
Route.get('/users', () => {
137
+
// resolves to route path `/admin/users`
138
+
})
139
+
})
140
+
```
96
141
97
142
98
143
## Accessing the Current Route
99
144
You can retrieve route information using the [request](/docs/requests) instance. At this point you can’t access the route instance used by Supercharge.
100
145
101
146
102
147
## Cross-Origin Resource Sharing (CORS)
103
-
Tba.
148
+
A Supercharge applications comes with a CORS middleware. This CORS middleware is enabled by default for new applications and configured in `middleware` method of the HTTP kernel. You can find the HTTP kernel in `app/http/kernel.ts`. All CORS settings are adjustable in the `config/cors.ts` configuration file.
0 commit comments