jenwachter/router-exchange

A collection of PHP interfaces and adapters to make swapping out router library dependencies quick and easy.

Installs: 20

Dependents: 0

Stars: 1

Open Issues: 1

Language: PHP

dev-master 2013-09-13 13:29 UTC

README

A collection of PHP interfaces and adapters to make swapping out router library dependencies quick and easy.

Included adapters

  1. Slim Framework

Please feel free to fork the repo and create more adapters.

Basic Usage

In this example, RouterExchange and Slim Framework have been preloaded via Composer.

  1. Instantiate a new router adapter:
$slim = new \Slim\Slim();
$router = new \RouterExchange\Adapters\Slim\Router($slim);
  1. Create some routes:
$router->get("/:page", function ($page) {
    echo "You are on the {$page} page.";
});
  1. Run the application:
$router->run();

Methods

Settings

setDebug()

Enables or disables router debug mode.

Usage
$router->setDebug($bool);
Parameters
  1. $bool: (boolean) Required. TRUE to turn debug mode on; FALSE to turn it off. By default, debug mode is turned off.
Example
$router->setDebug(true);

HTTP

get(), post(), put(), delete(), options()

Maps a callback method to a matching URI requested by the given HTTP method.

Usage
$router->get($pattern, $callback);
Parameters
  1. $pattern: (string) Required. Route pattern to match. See documentation for specific adapter for details.
  2. __$callback: (string) Required. Callable function that is executed when a route is matched. See documentation for specific adapter for details.
Example
$router->get(":/page", function($page) {
    echo "This is the {$page} page."
});

Route Methods

name()

Assigns a name to a route. Always chain to an HTTP method.

Usage
$router->get($pattern, $callback)->name($name);
Parameters
  1. $name: (string) Required. Name of the route.
Example
$router->get(":/page", function($page) {
    echo "This is the {$page} page."
})->name("Page");
condition()

Assigns a regular expression condition to a route parameters. Always chain to an HTTP method.

Usage
$router->get($pattern, $callback)->condition($param, $regex);
Parameters
  1. $param: (string) Required. Route parameter to assign the condition to.
  2. $regex: (string) Required. Regular expression.
Example
$router->get(":/page", function($page) {
    echo "This is the {$page} page."
})->condition("page", "[a-z]+");

Middleware

before()

Assigns middleware(s) to run before the route callback. Multiple before middlewares can be assigned and will be executed in the order in which they were declared.

Usage
$router->get($pattern, $callback)->before($callable);
Parameters
  1. $callable: (function) Required. The function to run.
Example
$router->get(":/page", function($page) {
    echo "This is the {$page} page."
})->before(function() {
    echo "This is the before middleware.";
});
after()

Assigns middleware(s) to run after the route callback. Multiple after middlewares can be assigned and will be executed in the order in which they were declared.

Usage
$router->get($pattern, $callback)->after($callable);
Parameters
  1. $callable: (function) Required. The function to run.
Example
$router->get(":/page", function($page) {
    echo "This is the {$page} page."
})->after(function() {
    echo "This is the before middleware.";
});

Router methods

redirect()

Used within a route callable to redirect another page.

Usage
$router->redirect($uri);
Parameters
  1. $uri: (string) URI to redirect a user to. The above example would redirect / to /hello.
abort()

Aborts the current request.

Usage
$router->abort($code, $message);
Parameters
  1. $code: (int) HTTP status code; for example, 404.
  2. $message: (string) Error message to display to the user.
hook()

Register a callback method to a hook.

Usage
$router->hook($name, $callable);
Parameters
  1. $name: (string) Required. Name of the hook.
  2. $callable: (function) Required. The function to run.

Error

error()

Specify a custom error handler.

Usage
$router->error($callable);
Parameters
  1. $callable: (string) Required. Callable function that is executed when a route is matched. See documentation for specific adapter for details.

Run

run()

Runs the application.

Usage
$rotuer->run();