OSDN Git Service

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