OSDN Git Service

composer require fig/http-message-util
[php-libraries/Router.git] / README.md
1 # PHP Router class
2
3 [![Latest Stable Version](https://poser.pugx.org/dannyvankooten/php-router/v/stable)](https://packagist.org/packages/dannyvankooten/php-router) 
4 [![Total Downloads](https://poser.pugx.org/dannyvankooten/php-router/downloads)](https://packagist.org/packages/dannyvankooten/php-router) 
5 [![Latest Unstable Version](https://poser.pugx.org/dannyvankooten/php-router/v/unstable)](https://packagist.org/packages/dannyvankooten/php-router) 
6 [![License](https://poser.pugx.org/dannyvankooten/php-router/license)](https://packagist.org/packages/dannyvankooten/php-router)
7
8 A simple Rails inspired PHP router class.
9
10 * Usage of different HTTP Methods
11 * REST / Resourceful routing
12 * Reversed routing using named routes
13 * Dynamic URL's: use URL segments as parameters.
14
15 # Authors
16
17 - [Danny van Kooten](https://github.com/dannyvankooten)
18 - [Jefersson Nathan](https://github.com/malukenho)
19
20 # Easy to install with **composer**
21
22 ```sh
23 $ composer require dannyvankooten/php-router
24 ```
25
26 ## Usage
27
28 ### Friendly URL
29
30 Create a simple .htaccess file on your root directory if you're using Apache with mod_rewrite enabled.
31
32 ```apache
33 Options +FollowSymLinks
34 RewriteEngine On
35 RewriteRule ^(.*)$ index.php [NC,L]
36 ```
37
38 If you're using nginx, setup your server section as following:
39
40 ```nginx
41 server {
42         listen 80;
43         server_name mydevsite.dev;
44         root /var/www/mydevsite/public;
45
46         index index.php;
47
48         location / {
49                 try_files $uri $uri/ /index.php?$query_string;
50         }
51
52         location ~ \.php$ {
53                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
54                 # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
55
56                 # With php5-fpm:
57                 fastcgi_pass unix:/var/run/php5-fpm.sock;
58                 fastcgi_index index.php;
59                 include fastcgi.conf;
60                 fastcgi_intercept_errors on;
61         }
62 }
63 ```
64
65 This is a simple example of routers in action
66
67 ```php
68 <?php
69 require __DIR__.'/vendor/autoload.php';
70
71 use PHPRouter\RouteCollection;
72 use PHPRouter\Router;
73 use PHPRouter\Route;
74
75 $collection = new RouteCollection();
76 $collection->attachRoute(new Route('/users/', array(
77     '_controller' => 'someController::usersCreate',
78     'methods' => 'GET'
79 )));
80
81 $collection->attachRoute(new Route('/', array(
82     '_controller' => 'someController::indexAction',
83     'methods' => 'GET'
84 )));
85
86 $router = new Router($collection);
87 $router->setBasePath('/PHP-Router');
88 $route = $router->matchCurrentRequest();
89
90 var_dump($route);
91 ```
92
93 ## Load routers from a `yaml` file
94
95 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.
96
97 The route definition should follow the example below:
98
99 ```yaml
100 base_path: /blog
101
102 routes:
103   index: [/index, someClass.indexAction, GET]
104   contact: [/contact, someClass.contactAction, GET]
105   about: [/about, someClass.aboutAction, GET]
106 ```
107 In our **Front Controller** would have something like:
108
109 ```php
110 <?php
111 require __DIR__.'/vendor/autoload.php';
112
113 use PHPRouter\RouteCollection;
114 use PHPRouter\Config;
115 use PHPRouter\Router;
116 use PHPRouter\Route;
117
118 $config = Config::loadFromFile(__DIR__.'/router.yaml');
119 $router = Router::parseConfig($config);
120 $router->matchCurrentRequest();
121 ```
122
123 ## More information
124
125 If you like PHP Router you might also like [AltoRouter](//github.com/dannyvankooten/AltoRouter).
126
127 ## License
128
129 MIT Licensed, http://www.opensource.org/licenses/MIT