PHP code example of bybzmt / router
1. Go to this page and download the library: Download bybzmt/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/ */
bybzmt / router example snippets
// Require composer autoloader
uter = new \Bybzmt\Router\Router();
// Define routes
// ...
// Run it!
$router->run();
$router->handle('GET|POST', 'pattern', function() { … });
//or
$router->handle('GET|POST', 'pattern', ':Class.Method');
$router->get('pattern', function() { /* ... */ });
$router->post('pattern', function() { /* ... */ });
$router->put('pattern', function() { /* ... */ });
$router->delete('pattern', function() { /* ... */ });
$router->options('pattern', function() { /* ... */ });
$router->patch('pattern', function() { /* ... */ });
//all methods
$router->all('pattern', function() { /* ... */ });
// Bad
$router->get('/hello/\w+', function($name) {
echo 'Hello ' . htmlentities($name);
});
// Good
$router->get('/hello/(\w+)', function($name) {
echo 'Hello ' . htmlentities($name);
});
$router->get('/(\d+)', ':User.showProfile:id');
$router->set404(function() {
header('HTTP/1.1 404 Not Found');
// ... do something special here
});
$router = new \Bybzmt\Router\Router();
$router->get('/news/(\d+)', ':news.show:id');
$tool = new \Bybzmt\Router\Tool($router->getRoutes());
$data = $tool->convertReverse();
$reverse = new \Bybzmt\Router\Reverse($data);
//echo /news/1234
echo $reverse->buildUri('news.show', ['id'=>1234]);
$router = new \Bybzmt\Router\Router();
$router->get('/a1', ':example.test');
$router->get('/a2/(\d+)', ':example.test:k1');
$router->get('/a3/(\d+)/(\d+)', ':example.test:k1:k2');
$tool = new \Bybzmt\Router\Tool($router->getRoutes());
//Cache Router Data
$code = $tool->exportRoutes();
file_put_contents('routes_cache.php', $code);
//Cache Reverse Router Data
$code = $tool->exportReverse();
file_put_contents('reverse_cache.php', $code);
//Recovery Router Data
$router = new \Bybzmt\Router\Router(ter\Reverse(