<?php
/**
* 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) {
echo '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
$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';
}
|