Routing
Plumier generate routes directly from controllers. By default it will looks into the ./controller directory. Except other directory or controller classes specified on the configuration.
The main idea of Plumier route generator is generate route based on: <directories>/<controller name>/<method name>. Further more decorator(s) @route can be used to customize the generation result
Name Convention#
If no @route decorator provided, route will be generated by convention using controller name, method name and parameter names. By default route by convention will have GET http method.
Directory Convention#
Route generated based on controller directory hierarchy. For example if the controller hierarchy like below:
info
Directory convention can be disabled by using ControllerFacility and set directoryAsPath to false
Route generated:
Note that AnimalController will have their own root route based on their directory.
Http Verb Override#
Http verb override will only override the http verb of the route, route will be constructed using controller name (omit controller word) and action name
Absolute Route Override#
Absolute route override (route start with /) will ignore all the controller and action name including the directory convention name, instead it will used provided route.
Relative Route Override#
Relative route override will only rename the name of the action and keep using controller name.
Ignore Action Name#
You can provided empty string on the route parameter to ignore action name
Route Parameter Mapping#
Its possible to use different parameter name on route and the action parameter name like below
Above code means that the id route parameter will be bound to the name action parameter. Using this configuration will get rid the route analysis error.
Example Restful Api#
Sum up of above rules you can construct clean Restful API routes like below:
Root Route#
Root route only override the controller name
info
Relative/Absolute rule also applied into the Root route. When placed inside directory convention, an absolute Root route will skipped the directory name convention.
And logically Relative/Absolute rule, doesn't affected Root route name.
Parameterized Root Route#
Root route can be parameterized and provided backing parameter on all of the action, except absolute route
Root Route Parameter Mapping#
Same as the @route decorator, the @route.root also provide parameter mapping which work the same
Example Nested Restful API#
By using rules above you can configure nested restful api like below:
Multiple Route Decorator#
Multiple routes can be applied to an action, this functionalities needed for example when hosting an SPA with url rewrite
info
A more convenient way to serve SPA url rewrite is using @route.historyApiFallback() see here for more info
Multiple Root Route#
Multiple root routes can also be applied into a controller and resulting create multiple routes for each methods.
info
Absolute route when combined with multiple root route will cause conflict, consider to avoid them.
Ignore Route Generation#
By default Route Generation System will generate all methods inside controller into route. In some case you need to create an helper method inside controller but you don't want the method generated into route.
Above code showing that helper method will not be generated because it is marked with @route.ignore()
Ignore Controller Completely#
@route.ignore() can be applied on controller and make all routes inside controller ignored completely. This feature important on CRUD Route generation, where controller automatically generated and you don't have control to the controller.
Above code will cause all routes inside HomeController ignored completely.
Ignore Specific Method#
If you have a controller with inheritance, you can ignore super class routes by specifying method name like below
Above code showing that we ignore the save and replace method which cause only GET /users/get will be generated.
