Routing
Routes can be found in lib/routes
folder. And api.dart
is with prefix /api
.
Supported routes
Route.get(routeName, controller.method)
Route.post(routeName, controller.method)
Route.put(routeName, controller.method)
Route.patch(routeName, controller.method)
Route.delete(routeName, controller.method)
Info
Other supported routes are COPY
, HEAD
, OPTIONS
, LINK
, UNLINK
, PURGE
, LOCK
, UNLOCK
and VIEW
.
Param route
Route.get('/blog/{id}', info);
Route.get('/blog/{id}/activate', activate);
info(DoxRequest req, String id) {
/// your response here
}
activate(DoxRequest req, String id) {
/// your response here
}
Group Routes
Route.group(prefix, () {
Route.get(path, controller.method);
Route.post(path, controller.method);
});
Route.domain('dartondox.com', () {
Route.get(path, controller.method);
Route.post(path, controller.method);
});
Route.middleware([CustomMiddleware()], () {
Route.get(path, controller.method);
Route.post(path, controller.method);
});
Resource
Route.resource('blogs', BlogController());
GET /blogs
GET /blogs/create
POST /blogs
GET /blogs/{id}
GET /blogs/{id}/edit
PUT|PATCH /blogs/{id}
DELETE /blogs/{id}
Info
index
, create
, store
, show
, edit
, update
and destroy
are controller methods.
Check here documentation for generating resource controller.
Route with callback
Route.get('/ping', (DoxRequest req) => 'pong');
Route with controller
var webController = WebController();
Route.get('ping', webController.pong);
/// or
Route.get('ping', [webController.pong]);
class WebController {
pong(DoxRequest req) async {
return 'pong';
}
}
Class based middleware
var loggerMiddleware = LoggerMiddleware();
Route.get('ping', [loggerMiddleware, webController.pong]);
class LoggerMiddleware extends DoxMiddleware {
@override
handle(DoxRequest req) {
/// write your logic here
/// return DoxRequest back to continue next function (controller)
return req;
}
}
Info
To implement class-based middleware, you can create a new class that extends the DoxMiddleware
class. Within this new class, you'll need to define a handle method where you can incorporate your custom logic.
Function middleware
Route.get('ping', [authMiddleware, webController.pong]);
authMiddleware(DoxRequest req) {
/// write your logic here
/// return DoxRequest back to continue next function (controller)
return req;
}
Info
To create function-based middleware, you can define a function that takes a DoxRequest parameter.
Route with multi-middleware
Route.get('ping', [ loggerMiddleware, authMiddleware, webController.pong ]);
class LoggerMiddleware extends IDoxMiddleware {
@override
handle(IDoxRequest req) {
/// write your logic here
/// return DoxRequest back to continue next to the controller
return req;
}
}
authMiddleware(DoxRequest req) {
/// write your logic here
/// return DoxRequest back to continue next to the controller
return req;
}
class WebController {
pong(DoxRequest req) async {
return 'pong';
}
}