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**
20 "dannyvankooten/php-router": "dev-master"
29 Create a simple .htaccess file on your root directory if you're using Apache with mod_rewrite enabled.
32 Options +FollowSymLinks
34 RewriteRule ^(.*)$ index.php [NC,L]
37 If you're using nginx, setup your server section as following:
42 server_name mydevsite.dev;
43 root /var/www/mydevsite/public;
48 try_files $uri $uri/ /index.php?$query_string;
52 fastcgi_split_path_info ^(.+\.php)(/.+)$;
53 # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
56 fastcgi_pass unix:/var/run/php5-fpm.sock;
57 fastcgi_index index.php;
59 fastcgi_intercept_errors on;
64 This is a simple example of routers in action
68 require __DIR__.'/vendor/autoload.php';
70 use PHPRouter\RouteCollection;
74 $collection = new RouteCollection();
75 $collection->attachRoute(new Route('/users/', array(
76 '_controller' => 'someController::usersCreate',
80 $collection->attachRoute(new Route('/', array(
81 '_controller' => 'someController::indexAction',
85 $router = new Router($collection);
86 $router->setBasePath('/PHP-Router');
87 $route = $router->matchCurrentRequest();
92 ## Load routers from a `yaml` file
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.
96 The route definition should follow the example below:
102 index: [/index, someClass.indexAction, GET]
103 contact: [/contact, someClass.contactAction, GET]
104 about: [/about, someClass.aboutAction, GET]
106 In our **Front Controller** would have something like:
110 require __DIR__.'/vendor/autoload.php';
112 use PHPRouter\RouteCollection;
113 use PHPRouter\Config;
114 use PHPRouter\Router;
117 $config = Config::loadFromFile(__DIR__.'/router.yaml');
118 $router = Router::parseConfig($config);
119 $router->matchCurrentRequest();
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.
125 If you like PHP Router you might also like [AltoRouter](//github.com/dannyvankooten/AltoRouter).
128 MIT Licensed, http://www.opensource.org/licenses/MIT