PHP Classes


Recommend this page to a friend!
  Classes of Laudir Bispo   Easy PHP Router Class   Download  
Role: Example script
Content type: text/markdown
Description: Example script
Class: Easy PHP Router Class
Handle requests with controllers for given routes
Author: By
Last change:
Date: 5 years ago
Size: 1,836 bytes


Class file image Download


This is a simple class for you to start working with routes in your applications.


Install the latest version with

$ composer require laudirbispo/easy-route

Basic Usage

use laudirbispo\EasyRouter\EasyRouter;
use laudirbispo\EasyRouter\Exceptions\RouterException;

$router = new EasyRouter();

 * @param $pattern - string|array routes - accept multiple routes
 * @param $calback - class with method string|closure
$router->add($pattern, $callback);

    ['/admin/login', '/admin/login?(.*)'], 
// (.*) that is to say that it accepts any parameter after "?"
// example - /admin/login?return=other_url

// Closures and Regular expressions
$router->add(['/admin/users/group/edit/([-\w]{36}$)'], function ($groupId){
  $controller = new app\IdentityAccess\Application\Controllers\AdminGroups;
// For example, this regular expression, accepts only string in the format "4d428391-8975-4158-b68a-9e3054e3df2c" 
// "4d428391-8975-4158-b68a-9e3054e3df2c" is uuid4 string

// Other example
$router->add('news/(\w+)/(\d+)', function($category, $year){
  $controller = new app\News\Application\Controllers\News;
  $controller->getNewsByYear($category, $year);

// Check if router exists
$router->has(string $pattern);

// Execute 

// Exeptions 
// RouterException
// ControllerDoesNotExists
// InvalidRoute
// MethodDoesNotExists
// RouteNotFound


Laudir Bispo - <> - <><br />


EasyRouter is licensed under the MIT License - see the LICENSE file for details Free Software, Hell Yeah!