Tag Archives: php

Using Laravel with Resource Controllers and Method Filters

Using filters on routes in routes.php is as simple as:

Route::get('/something/{smth}', array('before' => 'auth', function($smth) {
     return "There is something called {$smth} ";

When using controllers filters may be specified like this:

Route::get('profile', array('before' => 'auth',
            'uses' => 'UserController@showProfile'));

However if you used to use Resource controllers like this:

Route::resource('something', 'SmthController');

Then an option is to specify filters from within your controller like this:

public function __construct()
    $this->beforeFilter('auth', array('only' =>
                    array('store', 'update', 'destroy')));

This however will not be so transparent to someone not knowing your code, so another good option is to declare a group Route in routes.php and put all secure routes there probably handled by different controllers and methods:

// secure routes
Route::group(array('before' => 'auth'), function()

    Route::resource('something', 'SmthController');

    Route::get('smthspecial', 'SmthController@get_smth_special');
    Route::any('search/(:all?)', 'SearchSmth@index');


    Route::any('edit_profile', 'profile@edit');
    Route::get('(:any)', 'profile@view');