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.
10 # Easy to install with **composer**
15 "dannyvankooten/php-router": "dev-master"
24 Create a simple .htaccess file on your root directory.
27 Options +FollowSymLinks
29 RewriteRule ^(.*)$ index.php [NC,L]
32 It's a simple example of routers in action
36 require __DIR__.'/vendor/autoload.php';
38 use PHPRouter\RouteCollection;
42 $collection = new RouteCollection();
43 $collection->attach(new Route('/users/', array(
44 '_controller' => 'someController::users_create',
48 $collection->attach(new Route('/', array(
49 '_controller' => 'someController::indexAction',
53 $router = new Router($collection);
54 $router->setBasePath('/PHP-Router');
55 $route = $router->matchCurrentRequest();
60 ## Load routers from a `yaml` file
62 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.
64 The route definition should follow the example below:
70 index: [/index, someClass.indexAction, GET]
71 contact: [/contact, someClass.contactAction, GET]
72 about: [/about, someClass.aboutAction, GET]
74 In our **Front Controller** would have something like:
78 require __DIR__.'/vendor/autoload.php';
80 use PHPRouter\RouteCollection;
85 $config = Config::loadFromFile(__DIR__.'/router.yaml');
86 $router = Router::parseConfig($config);
87 $router->matchCurrentRequest();
91 Have a look at the example.php file or read trough the class' documentation for a better understanding on how to use this class.
93 If you like PHP Router you might also like [AltoRouter](//github.com/dannyvankooten/AltoRouter).
96 MIT Licensed, http://www.opensource.org/licenses/MIT