OSDN Git Service

Insert nginx configuration in readme file
[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 if you're using Apache with mod_rewrite enabled.
30
31 ```apache
32 Options +FollowSymLinks
33 RewriteEngine On
34 RewriteRule ^(.*)$ index.php [NC,L]
35 ```
36
37 If you're using nginx, setup your server section as following:
38
39 ```nginx
40 server {
41         listen 80;
42         server_name mydevsite.dev;
43         root /var/www/mydevsite/public;
44
45         index index.php;
46
47         location / {
48                 try_files $uri $uri/ /index.php?$query_string;
49         }
50
51         location ~ \.php$ {
52                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
53                 # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
54
55                 # With php5-fpm:
56                 fastcgi_pass unix:/var/run/php5-fpm.sock;
57                 fastcgi_index index.php;
58                 include fastcgi.conf;
59                 fastcgi_intercept_errors on;
60         }
61 }
62 ```
63
64 This is a simple example of routers in action
65
66 ```php
67 <?php
68 require __DIR__.'/vendor/autoload.php';
69
70 use PHPRouter\RouteCollection;
71 use PHPRouter\Router;
72 use PHPRouter\Route;
73
74 $collection = new RouteCollection();
75 $collection->attachRoute(new Route('/users/', array(
76     '_controller' => 'someController::usersCreate',
77     'methods' => 'GET'
78 )));
79
80 $collection->attachRoute(new Route('/', array(
81     '_controller' => 'someController::indexAction',
82     'methods' => 'GET'
83 )));
84
85 $router = new Router($collection);
86 $router->setBasePath('/PHP-Router');
87 $route = $router->matchCurrentRequest();
88
89 var_dump($route);
90 ```
91
92 ## Load routers from a `yaml` file
93
94 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.
95
96 The route definition should follow the example below:
97
98 ```yaml
99 base_path: /blog
100
101 routes:
102   index: [/index, someClass.indexAction, GET]
103   contact: [/contact, someClass.contactAction, GET]
104   about: [/about, someClass.aboutAction, GET]
105 ```
106 In our **Front Controller** would have something like:
107
108 ```php
109 <?php
110 require __DIR__.'/vendor/autoload.php';
111
112 use PHPRouter\RouteCollection;
113 use PHPRouter\Config;
114 use PHPRouter\Router;
115 use PHPRouter\Route;
116
117 $config = Config::loadFromFile(__DIR__.'/router.yaml');
118 $router = Router::parseConfig($config);
119 $router->matchCurrentRequest();
120 ```
121
122 ## More information
123 Have a look at the example.php file or read trough the class' documentation for a better understanding on how to use this class.
124
125 If you like PHP Router you might also like [AltoRouter](//github.com/dannyvankooten/AltoRouter).
126
127 ## License
128 MIT Licensed, http://www.opensource.org/licenses/MIT