Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3cc7a12

Browse files
committedJan 15, 2015
Routing
1 parent cf8f639 commit 3cc7a12

File tree

2 files changed

+68
-16
lines changed

2 files changed

+68
-16
lines changed
 

‎in-depth_documentation/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,5 @@ Line 11 reads the `server_url` from the section `application` and assigns it to
9898
| plugins.*&lt;ARRAY&gt;* | | The keys are the PHP namespaces of the plugins that you are using in your installation and the value is `1`if you want to activate it. Example:<br>`plugins.MyVendor\MyPackage\FirstPlugin = 1`<br>`plugins.MyVendor\MyPackage\SecondPlugin = 0`<br>Will result in the first plugin being loaded and the second not. You can get the Plugin namespaces from the README files of the plugin packages you're using (hopefully). |
9999
| powered_by | true | Adds a HTML comment to the output with a hint to PHPFramework |
100100
| template_file_extension | `.twig` / `.html` | By default PHPFramwork uses the `TwigView` to render templates. Then `.twig` will be the default template file extension.<br>If you use your own view class based on `AbstractView` then `.html` will be the default template file extension. |
101+
| uri_path_prefix | | Provide this prefix if your application runs in a subfolder of your host. E.g. for `www.mydomain.tld/path/to/application` set `path/to/application` as `uri_path_prefix`. |
101102
| version | *version* | Is automatically set to the current 2-digit version of PHPFramework, e.g. `1.3`. |

‎in-depth_documentation/routing.md

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,90 @@
11
# Routing
22

3-
## `routes.php`
3+
## Basic Usage
4+
`myapp/routes.php`:
45

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`:
621

7-
The routes array has regular expressions as keys and controller classnames as values.
22+
[phpframework]
23+
uri_path_prefix = "path/to/application"
824

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`:
1038

1139
<?php
40+
1241
return [
13-
'|/users/$|' => 'MyVendor\\MyApp\\Controller\\UserController',
14-
'|/$|' => 'MyVendor\\MyApp\\Controller\\IndexController',
42+
'users/?/' => 'MyVendor\\MyApp\\Controller\\ProfileController',
1543
];
1644

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.
1846

19-
## method
47+
### Beginning, middle, end
2048

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:
2250

23-
If the request come from the command line `cli()` will be called.
51+
`routes.php`:
2452

25-
## parameters
53+
<?php
2654

27-
*todo..*
55+
return [
56+
'shop/%' => 'MyVendor\\MyApp\\Controller\\ShopController',
57+
];
2858

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.
3060

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+
];
3277

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.
3484

85+
`routes.php`:
3586

3687
<?php
3788
return [
38-
'|/is/phpframework/awesome/$|' => function() { return 'yes!' },
89+
'is/phpframework/awesome/' => function() { return 'yes!' },
3990
];

0 commit comments

Comments
 (0)
Please sign in to comment.