Configure Routing

The routing engine is responsible for taking the request URL and figure out what controller to create, what action to invoke and with which variables. For example, the routing engine can take a path like /user/read/1 and use it to invoke the read function in the user controller with the value '1' as its first variable or translate /admin to invoke the users controllers login action, all depending on its configuration.

The configuration file is located with the rest of the applications configuration in /Application/Config and is expected to be namned Routes.json and is (as you might quess) a JSON file.

Contents of the root element

Routes
Contains a list of routes. The routes are read from top-to-bottom with a first match basis. Its a good idea to have a general "catch-all" route at the bottom. If no valid route is found for a URL the routing engine will treat this as a 404-not found error.

Defaults
Default values for variables used in routing parts. Can be either string or JSON objects.

The default routing config

Below is the default content of the routing configuration. It will be used as an example to explain the very basics of how to write custom routes.

{
  "Routes": [
    {
      "/{Controller}/{Action}/{...}": {
        "Controller": "{Controller}",
        "Action": "{Action}",
        "Variables": "{...}"
      }
    }
  ],
  "Default": {
    "Controller": "Projects",
    "Action": "Index",
    "Variables": {
    }
  }
}

Names surrounded by curly brackets are variables. In this example controller, action and the ellipsis (...) are all variables. Controller and action are named variables and the ellipsis is a special catch-the-rest variable. The will be given values from the URL string based o their position. The url /home/index/test01/test would result in the controller assuming the value "home", action would assume the value "index" and the ellipsis would catch the rest of the string, therefore becoming an array with the values {"test02", "test"}. If no values can be found for a value, eg. if the request URL is is /home the variable would assume the value of "home" and both action and the ellipsis would be empty. If they are left empty, the routing engine will try to populate them with values from the defaults element. In the default files action defaults to "index" and the ellipsis defaults to an empty array, and the routing engine would therefore still invoke the index action in the home controller, but with an empty parameter list.

Custom routes

Custom routes can also be added. Say you have a user controller with a login action you want to use to allow users to login but want the URL to just be /login. With the default configuration this would means the login controller's index action would be invoked, but that was not the intended result. Lets instead add a new route above the default one.
{
    "/login/{...} {
    "Controller": "user",
    "Action": "login",
    "Variables":{
    }
  }
}
Note that the start of the route definition, the word login, is not encased in curly brackets is therefor not a variable but a set value. The routing engine will look at the first part of the URL. If it is "login" it would match this rule. The ellipsis after is important if you want any URL beggining with /login to match this route. If it's left out only exact matches (that is URL that has the exact value of "/login" would match. With the ellipsis the URL "/login/this/is/a/test/" would also match this routing rule. Also note that the value given to the variables value is an empty string instead of the value of the ellipsis variable. This means that the routing engine will capture the rest of the url but wont use it.