Pregunta

I'm not sure the best way to go about doing this, so comparing current to an array is just where my mind is currently at.

I'd like to have a group of routes execute a filter and then based on the route a different outcome from that filter. Something along the lines of

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

I have a number of different possibilities as far as uri's are concered

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

All of the above may have query strings and all have a number of HTTP methods. What is the best way to go about this?

¿Fue útil?

Solución

You can assign aliases to each route, so that you can identify the current route name despite the query strings. For example:

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

Now in a filter, you can do the following:

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

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

Note that this filter needs to be an after filter, not a before filter.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top