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)
8 A simple Rails inspired PHP router class.
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.
17 - [Danny van Kooten](https://github.com/dannyvankooten)
18 - [Jefersson Nathan](https://github.com/malukenho)
20 # Easy to install with **composer**
23 $ composer require dannyvankooten/php-router
30 Create a simple .htaccess file on your root directory if you're using Apache with mod_rewrite enabled.
33 Options +FollowSymLinks
35 RewriteRule ^(.*)$ index.php [NC,L]
38 If you're using nginx, setup your server section as following:
43 server_name mydevsite.dev;
44 root /var/www/mydevsite/public;
49 try_files $uri $uri/ /index.php?$query_string;
53 fastcgi_split_path_info ^(.+\.php)(/.+)$;
54 # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
57 fastcgi_pass unix:/var/run/php5-fpm.sock;
58 fastcgi_index index.php;
60 fastcgi_intercept_errors on;
65 This is a simple example of routers in action
69 require __DIR__.'/vendor/autoload.php';
71 use PHPRouter\RouteCollection;
75 $collection = new RouteCollection();
76 $collection->attachRoute(new Route('/users/', array(
77 '_controller' => 'someController::usersCreate',
81 $collection->attachRoute(new Route('/', array(
82 '_controller' => 'someController::indexAction',
86 $router = new Router($collection);
87 $router->setBasePath('/PHP-Router');
88 $route = $router->matchCurrentRequest();
93 ## Load routers from a `yaml` file
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.
97 The route definition should follow the example below:
103 index: [/index, someClass.indexAction, GET]
104 contact: [/contact, someClass.contactAction, GET]
105 about: [/about, someClass.aboutAction, GET]
107 In our **Front Controller** would have something like:
111 require __DIR__.'/vendor/autoload.php';
113 use PHPRouter\RouteCollection;
114 use PHPRouter\Config;
115 use PHPRouter\Router;
118 $config = Config::loadFromFile(__DIR__.'/router.yaml');
119 $router = Router::parseConfig($config);
120 $router->matchCurrentRequest();
125 If you like PHP Router you might also like [AltoRouter](//github.com/dannyvankooten/AltoRouter).
129 MIT Licensed, http://www.opensource.org/licenses/MIT