tailored-tunes/php-router

This package is abandoned and no longer maintained. No replacement package was suggested.

A lightweight router solution

2.0.2 2015-06-28 14:44 UTC

This package is not auto-updated.

Last update: 2021-11-13 01:25:14 UTC


README

A small routing library for php that decouples controllers from routing. The routing table only acts as a translation table between paths and intentions.

Installation

Install via composer. Installation help and versions at Packagist

Usage

Set up routes

use TailoredTunes\Router;

$router = new Router();
$router->addRoutes(
	[
		["/" => "Home#index"],
		["/login" => "Home#login"],
		["/logout" => "Home#logout"],
		["/privacy" => "Home#privacy"],
		["/magic/:var1/:var2" => "Home#magic"]
    ]
);

Variables in paths

You can match path variables by adding :var notions to the route table, as above. Those parameters will translate to the respective variable name in the parameters array of the handler.

Handle routes

The reason behind not handling the request internally is that you might want to put a controller factory between the routing and the serving. This way the router only tells you what the intention of the route was, then it's up to you to map it to a controller.

// $uri = the request uri
// $method = the request method
// $params = parameters for the request.

$requestParamBuilder = new RequestParamBuilder();
$requestParamBuilder->withEnv($_ENV)
	->withRequest($_REQUEST)
	->withCookies($_COOKIE)
	->withFiles($_FILES)
	->withServer($_SERVER)
	->withSession($_SESSION);

$params = new RequestParams($requestParamBuilder);

$handler = $router->handle($uri, $method, $params);

// serve request
call_user_func_array(array($handler->controller(), $handler->action()), $handler->parameters());