This file specifies the routes and which controller to call for each route.
"index": {
"pattern": "/{A}/shop/{C}/{D}",
"default": {
"_controller": "Graphit\\Examples\\Testing:index"
},
"requirements": {
"_method": "GET",
"_schema": "https",
"_ajax": false,
"_host": "example.com",
"_port": 666
}
}
A route has a name (for example 'index') and a few parameters:
The pattern of the route. Based on the pattern it is decided whether a requested path matches the route or not. Since the the first route that matches the route is used, the order of routes is important! Given that a route with pattern '/{A}' is followed by one with the pattern '/' the later would never be choosen for a request. It could still be called form within the source code. {variable_name}
is how a variable is defined. Variables inside a pattern must need to be separated by at least a slash (/
). A pattern always starts with a slash and you can have multiple variables.
pattern | valid pattern | variables | function | argument values |
---|---|---|---|---|
/ |
true | - | index($A) | $A = not defined |
{A} |
false | A | - | - |
/{A} |
true | A | index($A) | $A = A |
/test |
true | - | index() | - |
/test/{A} |
true | - | index() | - |
/test/{A} |
true | A | index($A) | $A = A |
/test/{A} |
true | A | index($C, $A) | $C = not defined, $A = A |
/{C}/{A} |
true | C, A | index() | - |
/{C}/{A} |
true | C, A | index($A) | $A = A |
/{C}-{A} |
false | C, A | - | - |
/test/{A}/log/{B} |
true | A, B | index($A) | $A = A |
/test/{A}/log/{B} |
true | A, B | index($C = 'd', $A) | $C = 'd', $A = A |
/test/{A}/log/{C} |
true | A, C | index($C, $A) | $C = C, $A = A |
When setting variables in patterns there are a few things to keep in mind:
This array is ment to hold default values for a specific route. Only the '_controller' key is reserved inside the default-array.
_controller: The controller function to call. "_controller" = "<full classname>:<functioname>"
.
'full classname' means that it's the whole classname without the 'Controller'-part but including the namespace. Also the namespace is not separated by single- (\), but double-backlashs (\\).
'functionname' is the function name of the function without the 'Action'-part.
Example:
To set the Graphit\Examples\TestingController\indexAction
-method as controller one would use:
"_controller": "Graphit\\Examples\\Testing:index"
The requirements array allows to set a few restrictions for the matching/acceptance of requests. Only accept matches of request, where:
Setting
"requirements": {
"_method": "GET",
"_schema": "https",
"_ajax": false,
"_host": "example.com",
"_port": 666
}
would only accept matches where: a. the pattern matches the URL of the request b. the request was send via https. c. the request came from example.com. d. the request was send to port 666.