Home » Php » php – Laravel 4, how to apply filters on Route::controller()

php – Laravel 4, how to apply filters on Route::controller()

Posted by: admin July 12, 2020 Leave a comment


I know i can do this

Route::get('foo/bar', array('before' => 'filter', 'uses' => '[email protected]'));

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.

How to&Answers:

In the constructor of your controller you may use

public function __construct()

Also, you can use

Route::group(array('before' => 'auth'), function() {


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