2017 © Pedro Peláez
 

library routeria

A simple fast yet powerful PHP router

image

terrydjony/routeria

A simple fast yet powerful PHP router

  • Tuesday, March 28, 2017
  • by terryds
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Routeria

Routeria is a lightweight and easy-to-use routing component., (*1)

Installing

Routeria installation using Composer, (*2)

composer require terrydjony/routeria ~2.0

Usage

The installed Routeria and all of the components is in the vendor folder.
In order to use it, you just need to require the autoload.
And, you need to load the namespace using use keyword., (*3)

require_once __DIR__ . '/vendor/autoload.php';

Configuration (.htaccess)

Before using Routeria, 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., (*4)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Simple Callback Routing

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., (*5)

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());

Don't forget to write line ->route($request->getPathInfo(), $request->getMethod()); to make it work, (*6)

Using Named Parameters

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());

The order of parameters in the callback doesn't matter.
You just need to specify all the necessary variables., (*7)

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, (*8)

Routing with specific HTTP Method

You can also perform other http methods routing easily. (even the custom one), (*9)

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());

Different method, different route., (*10)

Dispatch Controller

You can also dispatch a controller using Routeria., (*11)

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());

If you go to '/user/terry/35', the router will dispatch the getInfo method so it prints 'Hello terry ID: 35'.
Don't forget to specify the namespace if the class has., (*12)

Converting arguments

use Symfony\Component\HttpFoundation\Request;
use Routeria\Routeria;

$request = Request::createFromGlobals();
$router = new Routeria;
$router->get('/posts/{title:alpha}', function($title) { echo '

'.$title.'

';}) ->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' before the callback fires., (*13)

Custom route collection

You can define your own route collection by implementing RouteProviderInterface., (*14)

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());

You need your own blog controller to make it work., (*15)

Contribute to this library

Please contribute to this project by forking it, make good commits and then perform a pull request.
Thanks for your support., (*16)

The Versions

28/03 2017

v2.x-dev

2.9999999.9999999.9999999-dev

A simple fast yet powerful PHP router

  Sources   Download

MIT

The Requires

 

The Development Requires

by Terry Djony

routing router url router

28/03 2017

dev-master

9999999-dev

A simple fast yet powerful PHP router

  Sources   Download

MIT

The Requires

 

The Development Requires

by Terry Djony

routing router url router

02/06 2016

2.0.0

2.0.0.0

A simple fast yet powerful PHP router

  Sources   Download

MIT

The Requires

 

The Development Requires

by Terry Djony

routing router url router

06/07 2015

1.x-dev

1.9999999.9999999.9999999-dev

A simple fast yet powerful PHP router

  Sources   Download

MIT

The Requires

 

The Development Requires

by Terry Djony

routing router