Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 993
brazorf Avatar asked Feb 05 '14 17:02

brazorf


2 Answers

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(...);
});
like image 151
The Alpha Avatar answered Nov 15 '22 08:11

The Alpha


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

like image 41
Fran Muñoz Avatar answered Nov 15 '22 08:11

Fran Muñoz