Welcome to the Finch Routing Guide! This guide will walk you through the steps to configure routing in your Finch application. Whether you're a seasoned developer or just starting, Finch offers a robust set of tools to simplify server-side web app development.
FinchRoute
The FinchRoute class is used to define a route in your Finch application. It contains all the necessary properties to define a route. Here is an example of how to use it:
Properties:
path: The path of the route. It can contain variables in the form of{variableName}. For example,/users/{id}.index: The function to be called when the route is matched. It should return aFuture<String>.methods: The HTTP methods that the route should respond to. It is a list ofString. For example,['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'CONNECT', 'TRACE'].controller: The controller to be used for the route. It should be an instance ofController.children: The child routes of the current route. It is a list ofFinchRoute.extraPath: Additional paths for the route. It is a list ofString. For example,['/home', '/index'].apiDoc: The function to generate API documentation for the route. It should return aFuture<ApiDoc>.auth: The authentication controller to be used for the route. It should be an instance ofAuthController.permissions: The permissions required for the route. It is a list ofString. For example,['admin'].widget: The widget to be rendered for the route. It should be aStringrepresenting the path to the widget.params: The default parameters to be passed to the widget. It is aMap<String, Object?>.title: The title of the page. It is aString.excludePaths: The paths to be excluded from the route. It is a list ofString.hosts: The hosts that the route should respond to. It is a list ofString. For example,['example.com', 'www.example.com'].ports: The ports that the route should respond to. It is a list ofint. For example,[80, 443].
Example of a simple route definition:
Example of a route with a controller:
Example of a route with child routes:
Example of a route with authentication and permissions:
AppAuthController is a class that extends AuthController. You can find an example of it in the example directory.
API Routing
Finch provides a convenient way to define API routes. You can use /api path of FinchApp to define an API route. Here is an example of how to use it:
Parameters Routing
for defining parameters in the path, you can use the {} syntax. For example, /users/{id}. You can access the parameters in the index function using the rq.get method. For example, rq.getParam('id').