Controller
Class based controller
with static method
You can also use as static method in the controller.
Function based controller
Resource controller
import 'package:dox_core/dox_core.dart';
class BlogController {
/// GET /resource
index(DoxRequest req) async {}
/// GET /resource/create
create(DoxRequest req) async {}
/// POST /resource
store(DoxRequest req) async {}
/// GET /resource/{id}
show(DoxRequest req, int id) async {}
/// GET /resource/{id}/edit
edit(DoxRequest req, int id) async {}
/// PUT|PATCH /resource/{id}
update(DoxRequest req, int id) async {}
/// DELETE /resource/{id}
destroy(DoxRequest req, int id) async {}
}
Parameters
A controller asks for what it needs by type. Dox fills each parameter.
Route.put('/teams/{teamId}/users/{id}', controller.update);
class UserController {
Future<User> update(int teamId, int id, UpdateUser body) async { ... }
}
| Parameter type | Dox gives |
|---|---|
DoxRequest |
the request |
a @Dto class |
the checked body or query. See Validation. |
String, int, double, num, bool |
the path params, in the order of the route |
the same types with ? |
an optional path param ({id?}). null when it is not in the URL. |
Rules:
- The order of parameters is free. Path params are matched by position: the first path-param parameter gets the first
{...}in the route. DoxRequestis not needed. Add it only when you use it.- At most one
@Dtoparameter. - Only positional parameters. Named and
[optional]parameters are not supported. - A catch-all
{path*}param is aString. - The same rules work for static methods and plain functions.
Path param types. Text from the URL is converted:
int:"42"is42."abc"or"4.5"answers404 Not Found: no such page.double,num:"4.5"is4.5.bool:true,1/false,0.- Any other text answers
404too. - Path params are checked first. When one is wrong, the DTO is not checked.
Checked at start. The server does not start when:
- a parameter type is not in the list above, and not a
@Dto(did you rundart run build_runner build?), - the method has more path-param parameters than the route has
{...}params.
Why types, not names?
Dox reads the parameter types of your method once, when the routes are registered at start. It needs no dart:mirrors.
So update(int teamId, int id) works by position, not by the names teamId and id.