PHP Classes

File: example/index.php

Recommend this page to a friend!
  Classes of devg   Router Lite   example/index.php   Download  
File: example/index.php
Role: Example script
Content type: text/plain
Description: Usage example
Class: Router Lite
Route HTTP requests by URL pattern to callbacks
Author: By
Last change:
Date: 6 years ago
Size: 1,758 bytes


Class file image Download
 * Example Application with RouterLite

// Include Composer autoloading.
require 'vendor/autoload.php';

// Or include class directly.
// Uncomment line below if you are not using Composer.
// require 'routerlite/src/Router.php';

// Instantiate Router class
$router = RouterLite\Router::getInstance();

 * 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!';

// :any will be passed as $var
$router->add('/hello/:any', function ($var) {
'Hello, '.$var.'!';

// Or/and define routes as array.

// Wildcards will be passed as function params in handler
$routes = array(
'/sample' => 'SampleController@indexAction', // executes SampleController::indexAction()
'/blog/:num/:any' => 'SampleController@viewPostAction', // executes SampleController::viewPost($num, $any)
'/blog/:any' => 'SampleController@viewAction' // executes SampleController::viewAction($any)

// Add routes array

// Start route processing
$handler = $router->dispatch();

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