Php 如何为Laravel route参数提供选项
我试图实现的是,我有一个路由,它接受一个名为Php 如何为Laravel route参数提供选项,php,laravel,routes,Php,Laravel,Routes,我试图实现的是,我有一个路由,它接受一个名为type的参数,现在它接受任何值。但我想提供一些选择,以接受来自的值,如果匹配的路由有效,否则它将抛出NotFound错误 选择包括: 转发 宠儿 推特 代码: Route::get('/activity/{type}/status','ActivitiesController@status'); ActivitiesController.php 类活动控制器扩展控制器 { 公共功能状态($type,Request$Request) { 返回$req
type
的参数,现在它接受任何值。但我想提供一些选择,以接受来自的值,如果匹配的路由有效,否则它将抛出NotFound错误
选择包括:
Route::get('/activity/{type}/status','ActivitiesController@status');
ActivitiesController.php
类活动控制器扩展控制器
{
公共功能状态($type,Request$Request)
{
返回$request->all();
}
}
这将检查$type
。如果不合适,它将:
备选方案:
Route::get('/activity/{type}/status', function()
{
if ($type == 'retweet' || $type == 'favorite' || $type == 'tweet') {
$app = app();
$controller = $app->make('ActivitiesController');
$controller->callAction($app, $app['router'], 'status', $parameters = array());
}
});
备选方案2:
Route::get('/activity/{type}/status', function()
{
if ($type == 'retweet' || $type == 'favorite' || $type == 'tweet') {
$app = app();
$controller = $app->make('ActivitiesController');
$controller->callAction($app, $app['router'], 'status', $parameters = array());
}
});
仅当您只有3种类型,并且以后不会添加任何类型时。这是一个愚蠢的问题,但更容易阅读和维护:
Route::get('/activity/retweet/status','ActivitiesController@status');
Route::get('/activity/favorite/status','ActivitiesController@status');
Route::get('/activity/tweet/status','ActivitiesController@status');
你也可以。选择权在你。) 可以使用正则表达式约束,如:
Route::get('/activity/{type}/status','ActivitiesController@status')
->where('type', '(retweet|favorite|tweet)');
我知道这种方法,但我想在routes站点上过滤请求,因为我还有其他函数,它们对相同的路由语法发出post请求,我从中获取
$type
参数,如果我遵循这种模式,我将不得不在每个函数中进行检查。