PHP Classes

Router Lite: Route HTTP requests by URL pattern to callbacks

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 69%Total: 158 All time: 8,997 This week: 673Up
Version License PHP version Categories
routerlite 1.2.1MIT/X Consortium ...5.5HTTP, PHP 5, Tools, Design Patterns
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.

Picture of devg
  Performance   Level  
Name: devg <contact>
Classes: 1 package by
Country: Kazakhstan Kazakhstan
Age: 42
All time rank: 42618 in Kazakhstan Kazakhstan
Week rank: 170 Up1 in Kazakhstan Kazakhstan Up

Documentation

> 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 (7)  
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 (7)  /  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 (7)  /  src  
File Role Description
  Plain text file Router.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
 Version Control Unique User Downloads Download Rankings  
 100%
Total:158
This week:0
All time:8,997
This week:673Up
User Ratings User Comments (1)
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:93%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:340