将当前路由与 laravel 中允许的路由数组进行比较

mho*_*321 3 php compare laravel-4 laravel-routing

我不确定执行此操作的最佳方法,因此将电流与数组进行比较正是我目前的想法。

我想让一组路由执行一个过滤器,然后根据该路由获得与该过滤器不同的结果。类似的东西

if(in_array($currentRoute, $allowedRoutes){
    do action1
}
else{
    do action2
}
Run Code Online (Sandbox Code Playgroud)

就 uri 而言,我有多种不同的可能性

Route::get('/content','ContentController@index')
Route::post('/dynamic/{dynamic}','DynamicController@store')
Route::delete('dynamic/{dynamic}/content/{content}','ContentController@destroy')
Run Code Online (Sandbox Code Playgroud)

以上所有内容都可能有查询字符串,并且都有许多 HTTP 方法。解决这个问题的最佳方法是什么?

SUB*_*0DH 5

您可以为每个路由分配别名,这样无论查询字符串如何,您都可以识别当前路由名称。例如:

Route::get('/content', array('uses'=>'ContentController@index', 'as'=>'content'))
Route::post('/dynamic/{dynamic}', array('uses'=>'DynamicController@store', 'as'=>'dynamic.show'))
Route::delete('dynamic/{dynamic}/content/{content}', array('uses'=>'ContentController@destroy', 'as'=>'dynamic.destroy'))
Run Code Online (Sandbox Code Playgroud)

现在在过滤器中,您可以执行以下操作:

$allowedRoutes = array('content');
$currentRoute = Route::currentRouteName();

if (in_array($currentRoute, $allowedRoutes)) {
    // do action1
} else {
    // do action2
}
Run Code Online (Sandbox Code Playgroud)

注意,这个过滤器需要是一个after过滤器,而不是一个before过滤器。