PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of devg  >  Router Lite  >  README.md  >  Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Router Lite
Route HTTP requests by URL pattern to callbacks
Author: By
Last change: Update README.md
Date: 5 years ago
Size: 2,125 bytes
 

Contents

Class file image Download

> 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.

For more information send a message to info at phpclasses dot org.