7
votes

I know i can do this

Route::get('foo/bar', array('before' => 'filter', 'uses' => 'Controller@bar'));

to apply routes some filter. I am aware of Route::group() method too. Anyway, if i want to define a controller in this way

Route::controller('foo/{id}/bar', 'Controller');

i can not pass an array as the 2nd argument.

The question: how to apply filters to the following route?

Route::controller('foo/{id}/bar', 'Controller');

=== EDIT

I want to code this in my route.php, not inside a controller constructor.

2

2 Answers

14
votes

In the constructor of your controller you may use

public function __construct()
{
    $this->beforeFilter('auth');
}

Also, you can use

Route::group(array('before' => 'auth'), function() {
    Route::controller(...);
});
1
votes

Blockquote The controller method accepts two arguments. The first is the base URI the controller handles, while the second is the class name of the controller. Next, just add methods to your controller, prefixed with the HTTP verb they respond to.

The Route::controller is responsible of creating a group of route using REST naming conventions. Is thought for creating RESTFull services.

Blockquote Filters may be specified on controller routes similar to "regular" routes:

Because this function only allows two params, you can apply controller filters in the constructor. For example:

class RoutedController extends Controller
{
    public function __construct()
    {
       //Apply Auth filter to all controller methods
       $this->filter('before', 'auth');
    }
}

You can read about the controller filters in the Laravel docs: http://laravel.com/docs/controllers#controller-filters