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

▼魔方 西西 提交于 2019-12-08 19:47:59

问题


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.


回答1:


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(...);
});



回答2:


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



来源:https://stackoverflow.com/questions/21583931/laravel-4-how-to-apply-filters-on-routecontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!