Routing By Convention
Every router has a default route already defined for you in the application templates, which we refer to as routing by convention:
1
route( ":handler/:action?").end();
Copied!
The URL pattern in the default route includes two special position placeholders, meaning that the handler and the action will come from the URL. Also note that the :action has a question mark (?), which makes the placeholder optional, meaning it can exist or not from the incoming URL.
  • :handler - The handler to execute (It can include a Package and/or Module reference)
  • :action - The action to relocate to (See the ?, this means that the action is optional)
Behind the scenes the router creates two routes due to the optional placeholder in the following order:
  1. 1.
    route( "/:handler/:action" )
  2. 2.
    route( "/:handler)
Tip The :handler parameter allows you to nest module names and handler names. Ex: /module/handler/action
If no action is passed the default action is index
This route can handle pretty much all your needs by convention:
1
// Basic Routing
2
http://localhost/general -> event=general.index
3
http://localhost/general/index -> event=general.index
4
5
// If 'admin' is a package/folder in the handlers directory
6
http://localhost/admin/general/index -> event=admin.general.index
7
8
// If 'admin' is a module
9
http://localhost/admin/general/index -> event=admin:general.index
Copied!

Convention Name-Value Pairs

Any extra name-value pairs in the remaining URL of a discovered URL pattern will be translated to variables in the request collection (rc) for you automagically.
1
http://localhost/general/show/page/2/name/luis
2
# translate to
3
event=general.show, rc.page=2, rc.name=luis
4
5
http://localhost/users/show/export/pdf/name
6
# translate to
7
event=users.show, rc.export=pdf, rc.name={empty value}
Copied!
Tip: You can turn this feature off by using the valuePairTranslation( false ) modifier in the routing DSL on a route by route basis
route( "/pattern" ) .to( "users.show" ) .valuePairTranslation( false );
Last modified 1mo ago
Copy link
Edit on GitHub