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"
23 require __DIR__.'/vendor/autoload.php';
25 use PHPRouter\RouteCollection;
29 $collection = new RouteCollection();
30 $collection->attach(new Route('/users/', array(
31 '_controller' => 'someController::users_create',
35 $collection->attach(new Route('/', array(
36 '_controller' => 'someController::indexAction',
40 $router = new Router($collection);
41 $router->setBasePath('/PHP-Router');
42 $route = $router->matchCurrentRequest();
47 ## Load routers from a `yaml` file
49 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.
51 The route definition should follow the example below:
57 index: [/index, someClass.indexAction, GET]
58 contact: [/contact, someClass.contactAction, GET]
59 about: [/about, someClass.aboutAction, GET]
61 In our **Front Controller** would have something like:
65 require __DIR__.'/vendor/autoload.php';
67 use PHPRouter\RouteCollection;
72 $config = Config::loadFromFile(__DIR__.'/router.yaml');
73 $router = Router::parseConfig($config);
74 $router->matchCurrentRequest();
78 Have a look at the example.php file or read trough the class' documentation for a better understanding on how to use this class.
80 If you like PHP Router you might also like [AltoRouter](//github.com/dannyvankooten/AltoRouter).
83 MIT Licensed, http://www.opensource.org/licenses/MIT