OSDN Git Service

convert short array syntax to a php5.3+ compatible one
[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 # Authors
11
12 - [Danny van Kooten](https://github.com/dannyvankooten)
13 - [Jefersson Nathan](https://github.com/malukenho)
14
15 # Easy to install with **composer**
16
17 ```javascript
18 {
19     "require": {
20         "dannyvankooten/php-router": "dev-master"
21     }
22 }
23 ```
24
25 ## Usage
26
27 ### Friendly URL
28
29 Create a simple .htaccess file on your root directory.
30
31 ```apache
32 Options +FollowSymLinks
33 RewriteEngine On
34 RewriteRule ^(.*)$ index.php [NC,L]
35 ```
36
37 It's a simple example of routers in action
38
39 ```php
40 <?php
41 require __DIR__.'/vendor/autoload.php';
42
43 use PHPRouter\RouteCollection;
44 use PHPRouter\Router;
45 use PHPRouter\Route;
46
47 $collection = new RouteCollection();
48 $collection->attachRoute(new Route('/users/', array(
49     '_controller' => 'someController::users_create',
50     'methods' => 'GET'
51 )));
52
53 $collection->attachRoute(new Route('/', array(
54     '_controller' => 'someController::indexAction',
55     'methods' => 'GET'
56 )));
57
58 $router = new Router($collection);
59 $router->setBasePath('/PHP-Router');
60 $route = $router->matchCurrentRequest();
61
62 var_dump($route);
63 ```
64
65 ## Load routers from a `yaml` file
66
67 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.
68
69 The route definition should follow the example below:
70
71 ```yaml
72 base_path: /blog
73
74 routes:
75   index: [/index, someClass.indexAction, GET]
76   contact: [/contact, someClass.contactAction, GET]
77   about: [/about, someClass.aboutAction, GET]
78 ```
79 In our **Front Controller** would have something like:
80
81 ```php
82 <?php
83 require __DIR__.'/vendor/autoload.php';
84
85 use PHPRouter\RouteCollection;
86 use PHPRouter\Config;
87 use PHPRouter\Router;
88 use PHPRouter\Route;
89
90 $config = Config::loadFromFile(__DIR__.'/router.yaml');
91 $router = Router::parseConfig($config);
92 $router->matchCurrentRequest();
93 ```
94
95 ## More information
96 Have a look at the example.php file or read trough the class' documentation for a better understanding on how to use this class.
97
98 If you like PHP Router you might also like [AltoRouter](//github.com/dannyvankooten/AltoRouter).
99
100 ## License
101 MIT Licensed, http://www.opensource.org/licenses/MIT