PHP code example of zonuexe / simple-routing
1. Go to this page and download the library: Download zonuexe/simple-routing library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zonuexe / simple-routing example snippets
// Method Path ReturnValue Param => RegExp extension (format)
$routing_map = [
['GET', '/', 'index' ],
['GET|POST', '/search', 'search' ],
['GET', '/article/:id', 'article', ['id' => '/\A(\d+)\z/'], '?ext' => ['', 'txt']],
['GET', '/info', 'feed' , '?ext' => ['rss', 'rdf', 'xml']],
'#404' => 'not_found' // special
];
$router = new \Teto\Routing\Router($routing_map);
$action = $router->match($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
// Shorthand (but, do not use reverse routing)
$action = \Teto\Routing\Router::dispatch($routing_map, $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
// Name Method Path ReturnValue Param => RegExp extension (file format)
$routing_map = [
'root' => ['GET', '/', 'index' ],
'search' => ['GET|POST', '/search', 'search' ],
'article' => ['GET', '/article/:id', 'article', ['id' => '/\A(\d+)\z/'], '?ext' => ['', 'txt']],
'info' => ['GET', '/info', 'feed' , '?ext' => ['rss', 'rdf', 'xml']],
'#404' => 'not_found' // special
];
$router = new \Teto\Routing\Router($routing_map);
$router->makePath('root'); //=> '/'
$router->makePath('search'); //=> '/search'
$router->makePath('article', ['id' => 123]); //=> '/article/123'
$router->makePath('info', ['?ext' => 'rss']); //=> '/info.rss'
$re_num_id = '/\A(\d+)\z/';
$re_user_name = '/\A@([a-z]+)\z/';
$routing_map = [
'root' => ['GET', '/', 'index' ],
'search' => ['GET', '/search', 'search' ],
'article' => ['GET', '/article/:id', 'article', ['id' => $re_num_id], '?ext' => ['', 'txt']],
'#404' => 'not_found' // special
];