nickstuer/easy-router

A basic and easy to use router for PHP.

v0.0.1 2015-12-09 02:38 UTC

This package is not auto-updated.

Last update: 2024-11-07 00:43:06 UTC


README

Introduction

THIS PROJECT IS NOT BEING MAINTAINED AT THE MOMENT!

This is a basic PHP router mainly created for learning purposes. This works well for when you don't want the router function of your application tied tightly to the rest of your application.

Supports PHP 7+.

If you have any suggestions or bug fixes please feel free to do modify.

Example Usage

Specify the controller and the controller method to call when a route is matched using addRoute().

To match wild cards use the following phrases in your route path:

  • (any) - Matches any combination of upper case/lower case letters and numbers.
  • (int) - Matches only integers.
  • (abc) - Matches any combination of upper case/lower case letters.

Typical Use of Easy Router

include (__DIR__ . '/vendor/autoload.php');

use \NickStuer\EasyRouter\Route as Route;

$routeManager = new \NickStuer\EasyRouter\RouteManager();
$dispatcher = new \NickStuer\EasyRouter\Dispatcher($routeManager);

$routeManager->addRoute(new Route('get', '/', 'ControllerToCall@methodToCall'));
$routeManager->addRoute(new Route('get', '/about', 'PagesController@showAbout'));
$routeManager->addRoute(new Route('get', '/contact', 'PagesController@showContact'));
$routeManager->addRoute(new Route('get', '/profile/(abc)', 'ProfileController@showProfile'));

try {
    $dispatcher->dispatch();
} catch (\NickStuer\EasyRouter\Exceptions\RouteNotFoundException $e) {
    echo "Route Not Found - 404";
    exit;
} catch (\NickStuer\EasyRouter\Exceptions\MethodNotAllowedException $e) {
    echo "Method Not Allowed - 405";
    exit;
}

$routeInfo = $dispatcher->getMatchedRoute();

/**
 * It's recommended that you use a DI container to create the controller class to
 * automatically insert the required dependencies.
 */
$controller = new $routeInfo['controller'];
$method = $routeInfo['method'];
$controller->$method($routeInfo['variables']);

License

Easy Router is licensed under the MIT license