3 A simple Rails inspired PHP router class.
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.
12 - [Danny van Kooten](https://github.com/dannyvankooten)
13 - [Jefersson Nathan](https://github.com/malukenho)
15 # Easy to install with **composer**
18 $ composer require frostbane/php-router
25 Create a simple .htaccess file on your root directory if you're using Apache with mod_rewrite enabled.
28 Options +FollowSymLinks
30 RewriteRule ^(.*)$ index.php [NC,L]
33 If you're using nginx, setup your server section as following:
38 server_name mydevsite.dev;
39 root /var/www/mydevsite/public;
44 try_files $uri $uri/ /index.php?$query_string;
48 fastcgi_split_path_info ^(.+\.php)(/.+)$;
49 # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
52 fastcgi_pass unix:/var/run/php5-fpm.sock;
53 fastcgi_index index.php;
55 fastcgi_intercept_errors on;
60 This is a simple example of routers in action
64 require __DIR__.'/vendor/autoload.php';
66 use PHPRouter\RouteCollection;
70 $collection = new RouteCollection();
71 $collection->attachRoute(new Route('/users/', array(
72 '_controller' => 'someController::usersCreate',
76 $collection->attachRoute(new Route('/', array(
77 '_controller' => 'someController::indexAction',
81 $router = new Router($collection);
82 $router->setBasePath('/PHP-Router');
83 $route = $router->matchCurrentRequest();
88 ## Load routers from a `yaml` file
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.
92 The route definition should follow the example below:
98 index: [/index, someClass.indexAction, GET]
99 contact: [/contact, someClass.contactAction, GET]
100 about: [/about, someClass.aboutAction, GET]
102 In our **Front Controller** would have something like:
106 require __DIR__.'/vendor/autoload.php';
108 use PHPRouter\RouteCollection;
109 use PHPRouter\Config;
110 use PHPRouter\Router;
113 $config = Config::loadFromFile(__DIR__.'/router.yaml');
114 $router = Router::parseConfig($config);
115 $router->matchCurrentRequest();
120 If you like PHP Router you might also like [AltoRouter](//github.com/dannyvankooten/AltoRouter).
124 MIT Licensed, http://www.opensource.org/licenses/MIT