Skip to content

Commit 17b5569

Browse files
committed
routing docs
1 parent 8f39920 commit 17b5569

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

routing.md

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,41 +65,86 @@ Route.get('/user', HelloController)
6565

6666

6767
## 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. 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.
6969

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+
```
7079

71-
### Required Route Parameters
7280

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:
7383

7484
```ts
75-
Route.get('/:page', ShowDocs)
85+
Route.get('/hello/:name', ({ request }) => {
86+
const name = request.param('name')
87+
88+
return `Hello ${name}`
89+
})
7690
```
7791

7892

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:
8095

8196
```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+
})
83102
```
84103

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+
85106

86107
## 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.
88111

89112

90113
### 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:
115+
116+
```ts
117+
Route.middleware(['first', 'second']).group(() => {
118+
// `first` and `second` middleware run for all routes defined in this group
119+
120+
Route.get('/', () => {
121+
//
122+
})
123+
})
124+
```
92125

93126

94127
### 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+
```
96141

97142

98143
## Accessing the Current Route
99144
You can retrieve route information using the [request](/docs/requests) instance. At this point you can’t access the route instance used by Supercharge.
100145

101146

102147
## 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.
104149

105150

0 commit comments

Comments
 (0)