PHP Classes

Easy PHP Router Class: Handle requests with controllers for given routes

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 130 All time: 9,358 This week: 455Up
Version License PHP version Categories
easy-router 1.0.0MIT/X Consortium ...5HTTP, PHP 5, Language, Design Patterns
Description 

Author

This package can handle requests with controllers for given routes.

It can register the patterns of HTTP request URIs that when matched will call a given controller class.

The package can take the current request URI and will call the registered controller class for the respective URI pattern that is matched.

Picture of Laudir Bispo
  Performance   Level  
Name: Laudir Bispo <contact>
Classes: 7 packages by
Country: Brazil Brazil
Age: 32
All time rank: 2247151 in Brazil Brazil
Week rank: 170 Up12 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 3x

Example

EasyRouter

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

Installation

Install the latest version with

$ composer require laudirbispo/easy-route

Basic Usage

<?php
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);

//Examples
$router->add(
    ['/admin/login', '/admin/login?(.*)'], 
    'app\IdentityAccess\Application\Controllers\AccountController::pageLogin'
);
// (.*) 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;
  $controller->pageEditGroup($groupId);
});
// 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 
$router->execute($_SERVER['REQUEST_URI']);

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

Author

Laudir Bispo - <laudirbispo@outlook.com> - <https://twitter.com/laudir_bispo><br />

License

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


  Files folder image Files (8)  
File Role Description
Files folder imagesrc (1 file, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Example Example script

  Files folder image Files (8)  /  src  
File Role Description
Files folder imageExceptions (5 files)
  Plain text file EasyRouter.php Class Class source

  Files folder image Files (8)  /  src  /  Exceptions  
File Role Description
  Plain text file ControllerDoesNotExists.php Class Class source
  Plain text file InvalidRoute.php Class Class source
  Plain text file MethodDoesNotExists.php Class Class source
  Plain text file RouteNotFound.php Class Class source
  Plain text file RouterException.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
Downloadeasy-router-2019-02-26.zip 5KB
Downloadeasy-router-2019-02-26.tar.gz
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
Get PHP Class Name Download .zip .tar.gz get controller name and namespace Required
 Version Control Unique User Downloads Download Rankings  
 100%
Total:130
This week:0
All time:9,358
This week:455Up