|
1 | 1 | # Routing
|
2 | 2 |
|
3 |
| -## `routes.php` |
| 3 | +## Basic Usage |
| 4 | +`myapp/routes.php`: |
4 | 5 |
|
5 |
| -Routing in PHPFramework is bloody easy. Inside your application folder you define your routes in `routes.php`. It has to return an array with your routes. |
| 6 | + <?php |
| 7 | + return [ |
| 8 | + 'users/' => 'MyVendor\\MyApp\\Controller\\UserListController', |
| 9 | + '.' => 'MyVendor\\MyApp\\Controller\\IndexController', |
| 10 | + ]; |
| 11 | + |
| 12 | +The controller for the matched route will be called. The method that is being called depends on the HTTP request type. E.g. a GET Request results in the `get()` method being called. |
| 13 | + |
| 14 | +If the request come from the command line `cli()` will be called. |
| 15 | + |
| 16 | +## Background |
| 17 | + |
| 18 | +The Router (`\AppZap\PHPFramework\Mvc\Router`) will check if the current url matches any of the regular expressions and calls the first matching Controller. For that it takes the URL and strips the hostname. If your application runs in a subfolder you should strip it with the following option: |
| 19 | + |
| 20 | +`settings.ini`: |
6 | 21 |
|
7 |
| -The routes array has regular expressions as keys and controller classnames as values. |
| 22 | + [phpframework] |
| 23 | + uri_path_prefix = "path/to/application" |
8 | 24 |
|
9 |
| -### example |
| 25 | +The remaining part is compared to the keys of the routes array. Notice that there must be no leading slashes in the route definitions. |
| 26 | + |
| 27 | +The first matching route will be followed. |
| 28 | + |
| 29 | +To configure the empty route (e.g. `http://mydomain.tld/`), use the dot placeholder `"."` (see in above example). |
| 30 | + |
| 31 | +## Advanced usage |
| 32 | + |
| 33 | +### parameters |
| 34 | + |
| 35 | +Urls may contain dynamic parts such as a username or an ID. You want to match those URLs in your routing and provide these values to your controller. Use the `?` placeholder for those parameters. |
| 36 | + |
| 37 | +`routes.php`: |
10 | 38 |
|
11 | 39 | <?php
|
| 40 | + |
12 | 41 | return [
|
13 |
| - '|/users/$|' => 'MyVendor\\MyApp\\Controller\\UserController', |
14 |
| - '|/$|' => 'MyVendor\\MyApp\\Controller\\IndexController', |
| 42 | + 'users/?/' => 'MyVendor\\MyApp\\Controller\\ProfileController', |
15 | 43 | ];
|
16 | 44 |
|
17 |
| -The Router (`\AppZap\PHPFramework\Mvc\Router`) will check if the current url matches any of the regular expressions and calls the first matching Controller. |
| 45 | +The `$params` array of your Controller method will be populated with the matched parameters. |
18 | 46 |
|
19 |
| -## method |
| 47 | +### Beginning, middle, end |
20 | 48 |
|
21 |
| -The controller method that is called depends on the HTTP Request method. e.g. a GET Request results in the `get()` method being called. |
| 49 | +You can also match routes that start with, end with or contain a certain expression. The syntax is similar to the MySQL `LIKE` statement: |
22 | 50 |
|
23 |
| -If the request come from the command line `cli()` will be called. |
| 51 | +`routes.php`: |
24 | 52 |
|
25 |
| -## parameters |
| 53 | + <?php |
26 | 54 |
|
27 |
| -*todo..* |
| 55 | + return [ |
| 56 | + 'shop/%' => 'MyVendor\\MyApp\\Controller\\ShopController', |
| 57 | + ]; |
28 | 58 |
|
29 |
| -## callable routing |
| 59 | +Now every request that **starts** with `http://mydomain.tld/shop/` will be processed by the `ShopController` and it takes responsibility to check the url for further routing. |
30 | 60 |
|
31 |
| -Instead of providing a controller class name, you can also directly provide a [callable](http://de2.php.net/manual/de/function.is-callable.php), e.g. an anonymous function. |
| 61 | +Use `%/shop/` to route requests that **end** with "/shop/".<br> |
| 62 | +Use `%/shop/%` to route requests that **contain** "/shop/".<br> |
| 63 | + |
| 64 | +### Regular expressions |
| 65 | + |
| 66 | +If you need more flexibility you can use regular expressions for your route definitions. Example: |
| 67 | + |
| 68 | +`routes.php`: |
| 69 | + |
| 70 | + <?php |
| 71 | + |
| 72 | + return [ |
| 73 | + '|^trader/(.*)/$|' => 'MyVendor\\MyApp\\Controller\\TraderController', |
| 74 | + '|^ajax/(.*)$|' => 'MyVendor\\MyApp\\Controller\\AjaxController', |
| 75 | + '||' => 'MyVendor\\MyApp\\Controller\\IndexHandler', |
| 76 | + ]; |
32 | 77 |
|
33 |
| -### example |
| 78 | +The strings *matched* by the paranthesis of the regular expressions will be passed to the Controller as `$params`. |
| 79 | + |
| 80 | + |
| 81 | +### callable routing |
| 82 | + |
| 83 | +Instead of providing a controller class name, you can also directly provide a [callable](http://de2.php.net/manual/de/function.is-callable.php), e.g. an anonymous function. |
34 | 84 |
|
| 85 | +`routes.php`: |
35 | 86 |
|
36 | 87 | <?php
|
37 | 88 | return [
|
38 |
| - '|/is/phpframework/awesome/$|' => function() { return 'yes!' }, |
| 89 | + 'is/phpframework/awesome/' => function() { return 'yes!' }, |
39 | 90 | ];
|
0 commit comments