Router library of the Ride framework
Router library of the PHP Ride framework., (*1)
Routing is used to translate a incoming HTTP request to a callback., (*2)
A Route defines a request path with the callback., (*3)
The definition of a route provides 2 ways for passing arguments to the action:, (*4)
You can optionally set an id to a route to make retrieval easy in your code. By using ids, you are able to override a path through your configuration without changing your code., (*5)
Keep your code clean by implementing only one action in a callback. Limiting a route to a specific, or multiple request methods (GET, POST, ...) can help you with this., (*6)
You can set a base URL to a route to limit your action to a certain domain., (*7)
A locale can also be set to a route. This is usable to act on localized paths later on the process., (*8)
You can help your security layer by adding permissions to your route. The router itself will not take these into account., (*9)
An Alias defines an aliased path for existing paths with query parameters., (*10)
It can be forced in order to redirect the original path to the alias., (*11)
A RouteContainer is the collection of your routes. It offers an easy interface to manage the routes and aliases., (*12)
Use the route container to generate an URL in order for the aliases to be handled., (*13)
The Router is what performes the actual translating of the incoming request to the route. It's a simple interface but a generic implementation is added to the library., (*14)
The result of a route action on the router is a RouterResult object., (*15)
This object has 3 possible states:, (*16)
An Url is a mutable object to update and manipulate a generated URL., (*17)
Check this code sample to see the possibilities of this library:, (*18)
<?php use ride\library\router\GenericRouter; use ride\library\router\RouteContainer; // create a route container $routeContainer = new RouteContainer(); // create a route with a path and a php callback $route = $routeContainer->createRoute('/path/to/%action%', 'callback', 'id'); // single method allowed $route->setAllowedMethods('GET'); // multiple methods allowed, case does not matter $route->setAllowedMethods(array('GET', 'post')); // add the route to the route container $routeContainer->setRoute($route); // create an alias $alias = $routeContainer->createAlias('/path/to/content', '/ptc'); // add the alias to the route container $routeContainer->setAlias($alias); // create the router $router = new GenericRouter($routeContainer); // set a default action for the / request $router->setDefaultCallback('callback'); // match a route $result = $router->route('GET', '/foo/bar'); // no match $result->isEmpty(); // true // let's try again $result = $router->route('PUT', '/path/to/content'); // a match but nothing to invoke $result->isEmpty(); // false $result->getAlias(); // null $result->getRoute(); // null $result->getAllowedMethods(); // array('GET' => true, 'POST' => true) // now with the right method $result = $router->route('GET', '/path/to/content'); // a match with arguments set to the route $result->isEmpty(); // false $result->getAlias(); // null $result->getRoute(); // Route instance $result->getRoute()->getArguments(); // array('action' => 'content'); // what about the alias? $result = $router->route('GET', '/ptc'); // the same match will be generated $result->isEmpty(); // false $result->getAlias(); // null $result->getRoute(); // Route instance $result->getRoute()->getArguments(); // array('action' => 'content'); // let's force the alias $alias->setIsForced(true); // what about the alias now? $result = $router->route('GET', '/ptc'); // still the same $result->isEmpty(); // false $result->getAlias(); // null $result->getRoute(); // Route instance $result->getRoute()->getArguments(); // array('action' => 'content'); // but when we take our original request ... $result = $router->route('GET', '/path/to/content'); // ... we see we need to redirect $result->isEmpty(); // false $result->getAlias(); // Alias instance // let's test multi domain support $route = new Route('/path', 'callback', 'id2'); $route->setBaseUrl('http://some-server.com'); $routeContainer->setRoute($route); $result = $router->route('GET', '/path', 'http://other-server.com'); $result->isEmpty(); // true $result = $router->route('GET', '/path', 'http://some-server.com'); $result->isEmpty(); // false // create some urls // http://some-server.com/path $url = $routeContainer->getUrl('http://my-server.com', 'id2'); // http://my-server.com/ptc $url = $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'content')); // http://my-server.com/path/to/my-action $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'my-action')); // http://my-server.com/path/to/my-action?limit=20&page=1 $url = $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'my-action'), array('page' => 1, 'limit' => 20)); // http://my-server.com/path/to/your-action?limit=20&page=2 $url = $routeContainer->getUrl('http://my-server.com', 'id', array('action' => 'my-action'), array('page' => 1, 'limit' => 20), '&'); $url->setArgument('action', 'your-action'); $url->setQueryParameter('page', 2); // translates an URL to it's alias if available and needed $url = $routeContainer->getUrlAlias($url);
For more examples, you can check the following implementation of this library: - ride/web, (*19)
You can use Composer to install this library., (*20)
composer require ride/lib-router