Skip to content

Routes

Routes are the core of every web application. They are the endpoints that the client will interact with.

Serinus provides a simple way to define routes using the Route class. You can decide if defining a custom class that extends the Route class or use the factory constructor to create one.

dart
import 'package:serinus/serinus.dart';

class MyController extends Controller {
  MyController({super.path = '/'}) {
    on(Route.get('/'), (context) async {
      return 'Hello World';
    });
  }
}

Using the factory constructors

The factory constructors are a more concise way to define routes. You can use them to define routes without creating a custom class.

dart
import 'package:serinus/serinus.dart';

class MyController extends Controller {
  MyController({super.path = '/'}) {
    on(Route.get('/'), (context) async {
      return 'Hello World';
    });
  }
}
Factory ConstructorMethodDescription
Route.getGETDefines a route that only accepts GET requests.
Route.postPOSTDefines a route that only accepts POST requests.
Route.putPUTDefines a route that only accepts PUT requests.
Route.deleteDELETEDefines a route that only accepts DELETE requests.
Route.patchPATCHDefines a route that only accepts PATCH requests.

Create a custom route

You can create a custom route by extending the Route class.

dart
import 'package:serinus/serinus.dart';

class MyRoute extends Route {
  MyRoute(String path): super({path: path, method: HttpMethod.get});
}

While this approach can seem more verbose, extending the Route class allows you to add lifecycle hooks and other custom logic to your routes.

Lifecycle hooks

The Route, since it is a subclass of the Hookable class, has access to the lifecycle hooks provided by some mixins.

MixinHookDescription
OnBeforeHandlebeforeHandleExecutes code before the request is handled.
OnAfterHandleafterHandleExecutes code after the request is handled.

Metadata

You can add metadata to your routes by using the metadata parameter in the route constructor.

This is available in both the factory constructor and the custom route class.

dart
import 'package:serinus/serinus.dart';

class MyController extends Controller {
  MyController({super.path = '/'}) {
    on(Route.get('/', metadata: [GuardMetadata()]), (context) async {
      return 'Hello World';
    });
  }
}

If you want to know more about metadata, please refer to the metadata page.

Built with 💙 and Dart 🎯 | One of the 🐤 of Avesbox