2017 © Pedro Peláez
 

library php-routing

PHP Routing Library

image

itlessons/php-routing

PHP Routing Library

  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 11 Forks
  • 0 Open issues
  • 2 Versions
  • 5 % Grown

The README.md

PHP Routing Library

Routing associates a request with the code that will convert it to a response., (*1)

The example below demonstrates how you can set up a fully working routing system:, (*2)

use Routing\Router;

$host = 'http://domain.tld';

$router = new Router($host);

$router->add('home', '/', 'controller:action');
$router->add('hello', '/hello', 'static:welcome', 'GET');
$router->add('profile', '/user(id:num)', 'profile:index', 'GET|POST');

$route = $router->match('GET', '/user777');
// $route->getController() => 'static:welcome'
// $route->getParameters() => [id:777]

$url = $router->generate('profile', array('id' => 777));
// $url => /user777

$url = $router->generate('profile', array('id' => 777), true);
// $url => http://domain.tld/user777

URL Matching Only

You can use url matcher standalone:, (*3)

use Routing\UrlMatcher;

$matcher = new UrlMatcher();
$matcher->register('GET', '/', 'controller:action');
$matcher->register('GET', '/hello', 'static:welcome');
$matcher->register('GET|POST', '/user(id:num)', 'profile:index');

$route = $router->match('GET', '/hello');

URL Generating Only

You can use url generator standalone:, (*4)

use Routing\UrlGenerator;

$generator = new UrlGenerator('http://domain.tld');
$generator->add('home', '/');
$generator->add('hello', '/hello');
$generator->add('profile', '/user(:id)');

$url = $generator->generate('profile', array('id' => 888), true);

Optional Last Placeholder

You can specify optional placeholder in the end of pattern:, (*5)

use Routing\Router;

$host = 'http://domain.tld';

$router = new Router($host);
$router->add('blog', '/blog/(page:num:?)', 'controller:action');

// match
$route = $router->match('GET', '/blog');
$route = $router->match('GET', '/blog/1');

// generate
$router->generate('blog'); => /blog
$router->generate('blog',  array('page' => 1)); => /blog/1

Similar Routes

You can use redirect on similar route (e.g /blog -> /blog/ if /blog/ exists):, (*6)

use Routing\Router;

$host = 'http://domain.tld';

$router = new Router($host);
$router->add('home', '/', 'controller:action');
$router->add('hello', '/hello', 'static:welcome');
$router->add('profile', '/blog/', 'profile:index');

$route = $router->match('GET', '/hello/');
if($router->getMatcher()->isNeedRedirect()){
    // need redirect to /hello
    redirect($router->getMatcher()->getRedirectUrl(), 302);
}

$route = $router->match('GET', '/blog');
if($router->getMatcher()->isNeedRedirect()){
    // need redirect to /blog/
    redirect($router->getMatcher()->getRedirectUrl(), 302);
}

Cache Compiled Data

You can cache compiled rules to files for performance:, (*7)

use Routing\Router;
use Routing\Request;

$request = new Request();

$router = new Router($request->getHTTPHost());
$router->useCache(__DIR__.'/matcher.cache.php', __DIR__.'/generator.cache.php');
$router->add('home', '/', 'controller:action');
// ...
$route = $router->match($request->getMethod(), $request->getPathInfo());

Request Class Helper

You can use simple request class to find pathInfo:, (*8)

use Routing\Router;
use Routing\Request;

$request = new Request();

$router = new Router($request->getHTTPHost());

$router->add('home', '/', 'controller:action');
$router->add('hello', '/hello', 'static:welcome', 'GET');
$router->add('profile', '/user(id:num)', 'profile:index', 'GET|POST');

$route = $router->match($request->getMethod(), $request->getPathInfo());

Resources

You can run the unit tests with the following command:, (*9)

$ cd path/to/php-routing/
$ composer.phar install
$ phpunit

The Versions

17/01 2015

dev-master

9999999-dev https://github.com/itlessons/php-routing

PHP Routing Library

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

url routing router uri

17/01 2015

0.0.1

0.0.1.0 https://github.com/itlessons/php-routing

PHP Routing Library

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

url routing router uri