03 Explore Routing - ct-laravel/lumen GitHub Wiki
The Lumen framework uses nikic/FastRoute for its route manager, but it seems to be a subset of its features.
Explore closure routes
- Explore static route
$app->get('/fast', function () {
return 'Faster';
});
- Explore route with placeholder using matching regexp
{id:\d+}
for ids
$app->get('/fast/{id:\d+}', function ($id) {
return 'Faster with id:' . $id;
});
Explore route group
- Explore route group attributes
- Resource groups using
prefix
- Namespace of controller using
namespace
- Resource groups using
- Explore route attributes
- Named Route using
as
- Controller method using
uses
- Named Route using
$app->group(['prefix' => 'resources', 'namespace' => 'App\Http\Controllers'], function ($app) {
$app->get('/', ['as' => 'resources.index', 'uses' => 'ResourceController@index']);
$app->post('/', ['as' => 'resources.store', 'uses' => 'ResourceController@store']);
$app->get('/{resource:\d+}', ['as' => 'resources.show', 'uses' => 'ResourceController@show']);
$app->put('/{resource:\d+}', ['as' => 'resources.replace', 'uses' => 'ResourceController@replace']);
$app->patch('/{resource:\d+}', ['as' => 'resources.update', 'uses' => 'ResourceController@update']);
$app->delete('/{resource:\d+}', ['as' => 'resources.destroy', 'uses' => 'ResourceController@destroy']);
$app->options('/', ['as' => 'resources.options', 'uses' => 'ResourceController@options']);
});
Explore Route Cache
- Explore
setDispatcher
usingFastRoute/cachedDispatcher
- Explore
cachedDispatcher
optionscacheFile
It is required - it wil be loaded instead of parsed each time.cacheDisable
It is optional - useful for developer/production mode
$app->setDispatcher(FastRoute\cachedDispatcher(function (FastRoute\RouteCollector $r) use ($app) {
foreach ($app->getRoutes() as $route) {
$r->addRoute($route['method'], $route['uri'], $route['action']);
}
}, [
'cacheFile' => __DIR__ . '/route.cache', /* required */
'cacheDisabled' => env('IS_DEBUG_ENABLED', true), /* optional, enabled by default */
]));