1. Go to this page and download the library: Download timetoogo/rapid-route 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/ */
timetoogo / rapid-route example snippets
use RapidRoute\CompiledRouter;
use RapidRoute\RouteCollection;
use RapidRoute\MatchResult;
$compiledRouterPath = __DIR__ . '/path/to/compiled/router.php';
$router = CompiledRouter::generate(
$compiledRouterPath,
function (RouteCollection $routes) {
// Route definitions...
}
);
$result = $router($httpMethod, $uri);
switch ($result[0]) {
case MatchResult::NOT_FOUND:
// 404 Not Found...
break;
case MatchResult::HTTP_METHOD_NOT_ALLOWED:
// 405 Method Not Allowed...
$allowedMethods = $result[1];
break;
case MatchResult::FOUND:
// Matched route, dispatch to associated handler...
$routeData = $result[1];
$parameters = $result[2];
break;
}
// Could not match route
[MatchResult::NOT_FOUND]
// Matched route but disallowed HTTP method
[MatchResult::HTTP_METHOD_NOT_ALLOWED, [<allowed HTTP methods>]]
// Found matching route
[MatchResult::FOUND, <associated route data>, [<matched route parameters>]]
use RapidRoute\Router;
use RapidRoute\RouteCollection;
use RapidRoute\MatchResult;
$compiledRouterPath = __DIR__ . '/path/to/compiled/router.php';
$router = new Router(
$compiledRouterPath,
function (RouteCollection $routes) {
// Route definitions...
}
);
// If true the router will be recompiled every request
$router->setDevelopmentMode($developmentMode);
// Or you can manually call when appropriate
// $router->clearCompiled();
$result = $router->match($httpMethod, $uri);
if($result->isNotFound()) {
// 404 Not Found...
} elseif ($result->isDisallowedHttpMethod()) {
// 405 Method Not Allowed...
$allowedMethods = $result->getAllowedHttpMethods();
} elseif ($result->isFound()) {
// Matched route, dispatch to associated handler...
$routeData = $result->getRouteData();
$parameters = $result->getParameters();
}
// Or if preferred
switch ($result->getStatus()) {
// case MatchResult::* as above
}
// This is a static route, it will extactly match '/shop/product'
'/shop/product'
// A dynamic route can be defined using the {...} parameter syntax
// This will match urls such as '/shop/product/123' or '/shop/product/abcd'
'/shop/product/{id}'
// If a route parameter must match a specific format you can define it
// by passing an array with a regex in the following format
['/shop/product/{id}', 'id' => '\d+']
// Or, if you prefer, you can use the predefined patterns using RapidRoute\Pattern
['/shop/product/{id}', 'id' => Pattern::DIGITS]
// More complex routes patterns are supported
[
'/shop/category/{category_id}/product/search/{filter_by}:{filter_value}',
'category_id' => Pattern::DIGITS,
'filter_by' => Pattern::ALPHA_LOWER
]
// You can also inline the parameter regexps using the following syntax
// The following is equivalent to the previous route definition
'/shop/category/{category_id:\d+}/product/search/{filter_by:[a-z]+}:{filter_value}'
function (RouteCollection $routes) {
$routes->add('GET', '/', ['name' => 'home']);
// There are also shortcuts for the standard HTTP methods
// the following is equivalent to the previous call
$routes->get('/', ['name' => 'home']);
// Or if any HTTP method should be allowed:
$routes->any('/contact', ['name' => 'contact']);
}