PHP code example of windwalker / router
1. Go to this page and download the library: Download windwalker/router 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/ */
windwalker / router example snippets php
use Windwalker\Router\Router;
$router = new Router;
php
use Windwalker\Route\Route;
// Route with name
$router->addRoute(new Route('sakura', 'flower/(id)/sakura', array('_controller' => 'SakuraController')));
// Route without name
$router->addRoute(new Route(null, 'flower/(id)/sakura', array('_controller' => 'SakuraController')));
php
$route = $router->match('flower/12/sakura');
$variables = $route->getVariables(); // Array([_controller] => SakuraController)
// Use variables
$class = $variables['_controller'];
$controller = new $class;
php
$route = new Route(
'name',
'pattern/of/route/(id).(format)',
// Default Variables
array(
'id' => 1,
'alias' => 'foo-bar-baz',
'format' => 'html'
),
// Allow methods
array('GET', 'POST'),
// Options
array(
'host' => 'windwalker.io',
'scheme' => 'http', // Only http & https
'port' => 80,
'sslPort' => 443,
'ed variables back to http request
$_REQUEST = array_merge($_REQUEST, $variables);
// Extra is the optional variables but we won't want to merge into request
$extra = $router->getExtra();
print_r($variables);
print_r($extra);
php
$router->addRoute(new Route('sakura', 'flower/(id)/sakura', array('_controller' => 'SakuraController')));
$uri = $router->build('sakura', array('id' => 30)); // flower/30/sakura
echo '<a href="' . $uri . '">Link</a>';
php
$router->addMap('flower/(id)/sakura', array('_controller' => 'SakuraController', 'id' => 1));
$variables = $router->match('flower/30/sakura');
php
new Route(null, 'flower/(id)-(alias)');
php
new Route(null, 'flower(/id)');
php
new Route(null, 'flower(/year,month,day)');
php
// Match 'king/john/troilus/and/cressida'
new Route(null, 'flower/(*tags)');
php
use Windwalker\Router\Matcher\SequentialMatcher;
$router = new Router(array(), new SequentialMatcher);
php
use Windwalker\Router\Matcher\BinaryMatcher;
$router = new Router(array(), new BinaryMatcher);
php
use Windwalker\Router\Matcher\TrieMatcher;
$router = new Router(array(), new TrieMatcher);
flower/*tags
php
$router->addMap('flower/(id)/(alias)', 'FlowerController');
$controller = $router->match('flower/25/sakura');
$_REQUEST = array_merge($_REQUEST, $router->getVariables());
echo (new $controller)->execute();
php
$router->addMap('flower/(id)/(alias)', 'FlowerController::indexAction');
$matched = $router->match('flower/25/sakura');
$_REQUEST = array_merge($_REQUEST, $router->getVariables());
list($controller, $action) = explode('::', $matched);
echo (new $controller)->$action();
php
$router->addMap('flower/(id)/(alias)', 'Flower\\Controller\\');
$controller = $router->match('flower/25/sakura', 'POST'); // Get Flower\\Controller\\Create
(new $controller)->execute();
php
$router->setHttpMethodSuffix('POST', 'SaveController');