Skip to content

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
}

Catch-all param

{name*} matches the rest of the path, with slashes.

Route.get('/docs/{page*}', (DoxRequest req, String page) {
    return page; // GET /docs/a/b/c gives 'a/b/c'
});

Serve storage files

Route.files(path) serves files of a storage disk at GET <path>/{key*}. By default only signed URLs work. See File Storage.

Route.files('files');
Route.files('public', disk: 'local', signed: false);

Request body for the docs

post, put and patch take a body: schema. It shows in the OpenAPI docs. It does not validate.

Route.post('/posts', controller.store, body: v.object(<String, Schema<Object?>>{
    'title': v.string().min(3),
}));

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';
    }
}