OSDN Git Service

Modify RouterCollection to use `SplObjectStorage`
[php-libraries/Router.git] / README.md
1 # PHP Router class
2
3 A simple Rails inspired PHP router class.
4
5 * Usage of different HTTP Methods
6 * REST / Resourceful routing
7 * Reversed routing using named routes
8 * Dynamic URL's: use URL segments as parameters.
9
10 # Easy to install with **composer**
11
12 ```javascript
13 {
14     "require": {
15         "dannyvankooten/php-router": "dev-master"
16     }
17 }
18 ```
19
20 ## Usage
21 ```php
22 <?php
23 require __DIR__.'/vendor/autoload.php';
24
25 use PHPRouter\RouteCollection;
26 use PHPRouter\Router;
27 use PHPRouter\Route;
28
29 $collection = new RouteCollection();
30 $collection->attach(new Route('/users/', array(
31     '_controller' => 'someController::users_create',
32     'methods' => 'GET'
33 )));
34
35 $collection->attach(new Route('/', array(
36     '_controller' => 'someController::indexAction',
37     'methods' => 'GET'
38 )));
39
40 $router = new Router($collection);
41 $router->setBasePath('/PHP-Router');
42 $route = $router->matchCurrentRequest();
43
44 var_dump($route);
45 ```
46
47 ## Load routers from a `yaml` file
48
49 We can define in a `yaml` file all the routes of our application. This facilitates our life when we need to *migrate*, *modify*, or later *add* new routes.
50
51 The route definition should follow the example below:
52
53 ```yaml
54 base_path: /blog
55
56 routes:
57   index: [/index, someClass.indexAction, GET]
58   contact: [/contact, someClass.contactAction, GET]
59   about: [/about, someClass.aboutAction, GET]
60 ```
61 In our **Front Controller** would have something like:
62
63 ```php
64 <?php
65 require __DIR__.'/vendor/autoload.php';
66
67 use PHPRouter\RouteCollection;
68 use PHPRouter\Config;
69 use PHPRouter\Router;
70 use PHPRouter\Route;
71
72 $config = Config::loadFromFile(__DIR__.'/router.yaml');
73 $router = Router::parseConfig($config);
74 $router->matchCurrentRequest();
75 ```
76
77 ## More information
78 Have a look at the example.php file or read trough the class' documentation for a better understanding on how to use this class.
79
80 If you like PHP Router you might also like [AltoRouter](//github.com/dannyvankooten/AltoRouter).
81
82 ## License
83 MIT Licensed, http://www.opensource.org/licenses/MIT