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
Of course, sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters:
122
123
123
124
```go
124
-
route.Get("/user/{id}", func(req *context.Request, id string) thinkgo.Response {
125
+
route.Get("/user/{id}", func(req *context.Request, id string) *context.Response {
125
126
return thinkgo.Text(fmt.Sprintf("User %s", id))
126
127
})
127
128
```
128
129
129
130
You may define as many route parameters as required by your route:
The prefix method may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with `admin`:
Middleware provide a convenient mechanism for filtering HTTP requests entering your application. You only need to implement the `Middleware` interface.
}).Middleware(func(request *context.Request, next router.Closure) interface{} {
161
+
if_, err:= request.Input("name"); err != nil {
162
+
return thinkgo.Text("Invalid parameters")
163
+
}
164
+
returnnext(request)
165
+
})
166
+
```
167
+
168
+
#### Route Groups
169
+
170
+
Route groups allow you to share route attributes, such as middleware or prefix, across a large number of routes without needing to define those attributes on each individual route.
If your controller method is also expecting input from a route parameter you should list your route parameters after the request dependencies. For example, you can access your route parameter `name` like so:
182
245
183
246
```go
184
-
route.Put("/user/{name}", func(req *context.Request, name string) thinkgo.Response {
247
+
route.Put("/user/{name}", func(req *context.Request, name string) *context.Response {
0 commit comments