1. Go to this page and download the library: Download heropoo/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/ */
heropoo / routing example snippets
oon\Routing\Router;
use Moon\Routing\UrlMatchException;
$router = new Router([
'namespace'=>'app\\controllers', //support controller namespace
'middleware'=>[ //support middleware
'startSession',
'verifyCSRFToken',
'auth'
],
'prefix'=>'' //support prefix
]);
// action also can be a Closure
$router->get('/', function(){
return 'Welcome \( ^▽^ )/';
});
//route parameter
$router->get('/hello/{name}', function($name){ // auto pick route param to Closure
return 'Hello '.$name;
});
$router->get('/login', 'UserController::login', 'login'); // name your route
$router->post('login', 'UserController::post_login');
//use route group
$router->group(['prefix'=>'user'], function(Router $router){
$router->post('delete/{id:\d+}', 'UserController::delete'); // {param:type pattern}
});
// match GET or POST request method
$router->match(['get', 'post'], '/api', 'ApiController::index');
// match all request method
$router->any('/other', 'ApiController::other');
// get all routes
var_dump($router->getRoutes());
/**
* match request
* @param string $path Request path, eg: /user/list
* @param string $method Request method, 'GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS''GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'
* @return array If not matched throw a UrlMatchException
* return [
* 'route' => $route, // Route
* 'params' => $params // array
* ];
*
*/
$res = $router->dispatch($path, $method);
var_dump($res);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.