iextra / router
router
dev-master
2020-06-21 19:19 UTC
Requires
- php: >=7.1
Requires (Dev)
- iextra/dumper: ^1.0
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2025-03-22 06:01:20 UTC
README
Adding route
Route takes 2 parameters.
The first is the URL pattern. The second is the 'action' (to be performed)
use Extra\Routing\Route; Route::get('/', function(){ return 'Hello World'; });
Available methods for adding routes
use Extra\Routing\Route; Route::any('/', 'action'); Route::get('/news', 'action'); Route::post('/news/{id}', 'action'); Route::put('/news/{id}', 'action'); Route::delete('/news/{id}', 'action'); Route::some(['head', 'options', 'connect', 'trace', 'patch'], '/test', 'action');
Available 'actions' for route
use Extra\Routing\Route; // Function name Route::get('/', 'FunctionName'); // The class name and method name are specified through the @ separator Route::get('/', 'ClassName@method'); // Callback function Route::get('/', function(){ return 'Hello World'; });
Search and execute matching route
use Extra\Routing\Router; try { $router = Router::getInstance()->includeRoutesFile('path_to_file_with_routes.php'); $route = $router->match($requestUri, $requestMethod); $result = $route->execute($requestUri); } catch (\Exception $e){ die($e->getMessage()); }
Execute route
In order for the parameter to be passed to the function,
it is necessary that the name of the variable that the function accepts
has the same name as indicated in the URL {id}
use Extra\Routing\Router; $route = Route::get('/news/{id}', function($id){ return 'Id of the news: ' . $id; }); $requestUrl = '/post/86'; $result = $route->execute($requestUrl); echo $result; // 'Id of post: 86'
Setting route name
use Extra\Routing\Route; Route::get('/news/', 'NewsController@list')->name('news.list'); Route::get('/news/{id}', 'NewsController@detail')->name('news.detail');
Setting route rules
use Extra\Routing\Route; // One rule Route::delete('/news/{id}', 'NewsController@delete')->rule('id', '[0-9]+'); // Few rules Route::post('/catalog/{section}/{id}', 'CatalogController@detail')->rules([ 'section' => '(cars|motorbikes)', 'id' => '\d+' ]); // To indicate that the parameter is optional, // you need to put a question mark in front of it {?page} Route::get('/news/{section}/{?page}', function($section, $page = 1){ return 'Section: ' . $section . ' | Page: ' . $page; });
Search route by name
use Extra\Routing\Router; $router = Router::getInstance(); $route = $router->getRouteByName('news.list');
Generate route URL
use Extra\Routing\Router; use Extra\Routing\Route; $router = Router::getInstance(); Route::get('/news/{id}', 'action')->name('news.detail'); // Way 1 (using router) $url = $router->generateRouteUrl('news.detail', ['id' => 86]); // Way 2 (using route) $route = $router->getRouteByName('news.detail'); $url = $route->generateUrl(['id' => 86]); echo $url; // '/news/86';