【问题标题】:Compare current route to array of allowed routes in laravel将当前路线与 laravel 中允许的路线数组进行比较
【发布时间】:2014-05-04 12:10:51
【问题描述】:

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

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

if(in_array($currentRoute, $allowedRoutes){
    do action1
}
else{
    do action2
}

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

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

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

【问题讨论】:

    标签: php laravel-4 compare laravel-routing


    【解决方案1】:

    您可以为每个路由分配别名,以便您可以识别当前路由名称,而不管查询字符串。例如:

    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'))
    

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

    $allowedRoutes = array('content');
    $currentRoute = Route::currentRouteName();
    
    if (in_array($currentRoute, $allowedRoutes)) {
        // do action1
    } else {
        // do action2
    }
    

    请注意,此过滤器必须是 after 过滤器,而不是 before 过滤器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-13
      • 2017-07-23
      • 2015-12-04
      • 2021-04-05
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      相关资源
      最近更新 更多