PHP Classes
elePHPant
Icontem

Router Lite: Route HTTP requests by URL pattern to callbacks

Recommend this page to a friend!
  Info   View files Documentation   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-08-12 (14 hours ago) RSS 2.0 feedStarStarStarStar 69%Total: 105 This week: 7All time: 8,691 This week: 103Up
Version License PHP version Categories
routerlite 1.2.1MIT/X Consortium ...5.5HTTP, PHP 5, Utilities and Tools, Des...
Description Author

This class can route HTTP requests by URL pattern to callbacks.

It can register URL patterns to match and associates callback functions to each pattern.

When the current request URL matches one of the patterns, the respective callback function is called to handle the request.

  Performance   Level  
Name: devg <contact>
Classes: 1 package by
Country: Kazakhstan Kazakhstan

Details

> This package deprecated and no more develop. See bit55/litero instead (https://github.com/bit55/litero).

RouterLite

Extremely simple router for small web applications. Small footprint, no overhead.

Router class in this package can route HTTP requests by URL pattern to callbacks.

It can register URL patterns to match and associates callback functions to each pattern.

When the current request URL matches one of the patterns, the respective callback function is called to handle the request.

Installing

You may install this component via Composer:

composer require devgkz/routerlite

Or download this repository and include file Router.php directly and place in your application directory.

Your webserver must point to the index.php file for any URI entered. See .htaccess for Apache and Nginx config in example directory.

Usage

Get Instance of Router class.

$router = RouterLite\Router::getInstance();

Add your route rules. Routes may contents exact or wildcard-rules.

Windcards sample: * /page/:any - any characters, like /page/qwerty or /page/123 * /page/:num - digits only, like /page/123

Route handler may be any callable (function name, closure) or string with controller class name and action method.

Router instantiate controller and execute action method automatically.

Note if you using Composer, add your controller classes to autoloading.

Add single rule with Closure handler:

$router->add('/', function () {
    // Any output 
    // or yor template processing
    echo 'Hello!';
});

Or/and define routes as array.

$routes = array(
    '/sample'         => 'SampleController@indexAction',
    '/blog/:num/:any' => 'SampleController@viewPostAction',
    '/blog/:any'      => 'SampleController@viewAction'
);

$router->add($routes);

Start route processing.

$handler = $router->dispatch();

if ($handler) {
    // Execute handler
    $router->execute($handler, $router->vars);
} else {
    // Show not found page
    http_response_code(404);
    echo '404 Not found';
}

Usage example see in example folder.

  Files folder image Files  
File Role Description
Files folder imageexample (2 files)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file nginx-host-sample.conf Data Nginx config file
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file .htaccess Data Apache host config
  Accessible without login Plain text file index.php Example Usage example

  Files folder image Files  /  src  
File Role Description
  Plain text file Router.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:105
This week:7
All time:8,691
This week:103Up
User Ratings User Comments (1)
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:93%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:422