To install Routeria, you need to add it into your composer.json file
{ "require": "terrydjony/routeria" }
Or, by using terminal
composer require terrydjony/routeria
First of all, you need to configure .htaccess file.
You need to turn your rewrite engine on and add rules so any requests to non-existing directory or filename will be rewritten to index.php.
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [QSA,L]
In order to use the router, just instantiate the router and the request object.
<?php use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; $request = Request::createFromGlobals(); $router = new Routeria;
For a simple callback route, you just need to use Routeria class which belongs to the Routeria namespace. The Request component of Symfony HttpFoundation is required to tell the request path to the router.
<?php use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; $request = Request::createFromGlobals(); $router = new Routeria; $router->get('/', function() { echo 'Hello World';}); $router->route($request->getPathInfo(), $request->getMethod());
You can also use named parameters. To do so, you also need to declare the parameter on the callback function.
The order of parameters in the callback doesn't matter.You just need to specify all the necessary variables.
<?php use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; $request = Request::createFromGlobals(); $router = new Routeria; $callback = function($fname, $lname) { echo "Hello $fname $lname. Nice to meet ya!"; }; $router->get('/greet/{fname:alpha}/{lname:alpha}', $callback); $router->route($request->getPathInfo(), $request->getMethod());
There are six placeholders available.
INT
for integers (regex: [0-9]+)
ALPHA
for alphabets (regex: [a-zA-Z_-]+)
ALNUM
for alphanumeric characters (regex: [a-zA-Z0-9_-]+)
HEX
for hexadecimals (regex: [0-9A-F]+)
ALL
for all characters (regex: .+)
WORD
is an alias for ALPHA
You can also perform a specific http method routing easily, even the custom one.
Different method, different route.
<?php use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; $request = Request::createFromGlobals(); $router = new Routeria; $router->get('/', function() { echo 'HTTP METHOD : GET';}); $router->post('/', function() { echo 'HTTP METHOD : POST';}); $router->put('/', function() { echo 'HTTP METHOD : PUT';}); $router->delete('/', function() { echo 'HTTP METHOD : DELETE';}); $router->add('/', function() { echo 'HTTP METHOD : CUSTOM';}, 'CUSTOM'); $router->route($request->getPathInfo(), $request->getMethod());
You can also dispatch a controller on the route easily. Just pass 'Controller::method' as the second argument.
<?php use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; class User { public function getInfo($id, $name) { echo 'Hello ' . $name . ' ID: ' . $id; } } $request = Request::createFromGlobals(); $router = new Routeria; $router->get('/user/{name:alpha}/{id:int}', 'User::getInfo'); $router->route($request->getPathInfo(), $request->getMethod());
It's also easy to convert or proccess the arguments that's passed to router before it's used. This is how to do it.
<?php use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; $request = Request::createFromGlobals(); $router = new Routeria; $router->get('/posts/{title:alpha}', function($title) { echo '<h1>'.$title.'</h1>';}) ->convert(function($title) { return ucwords(str_replace('-', ' ', $title)); }); $router->route($request->getPathInfo(), $request->getMethod());
The converter in this example changes all hypens into spaces in the title argument.
So, if you go to /posts/lorem-ipsum-dolor-sit-amet'
, it will print <h1>lorem ipsum dolor sit amet</h1>.
Notice that the argument 'lorem-ipsum-dolor-sit-amet'
has been converted into 'lorem ipsum dolor sit amet'
You can define your own route collection by implementing RouteProviderInterface.
<?php use Symfony\Component\HttpFoundation\Request; use Routeria\Routeria; use Routeria\RouteCollection; use Routeria\ControllerRoute; use Routeria\RouteProviderInterface; class BlogCollection implements RouteProviderInterface { public function register(RouteCollection $collection) { $blogRoutes = array( 'index' => new ControllerRoute('/','Blog::index','GET'), 'post' => new ControllerRoute('/{id:int}/{title:alnum}','Blog::showPost','GET'), 'page' => new ControllerRoute('/page/{title:alpha}','Blog::showPage','GET') ); $collection->addRoutes($blogRoutes); } } $request = Request::createFromGlobals(); $router = new Routeria; $collection = new BlogCollection; $router->register($collection); $router->route($request->getPathInfo(), $request->getMethod());
As for in this example, you need your own blog controller to make it work.