Laravel 4,如何在Route::controller() 上应用过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21583931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:59:04  来源:igfitidea点击:

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

phplaravellaravel-4

提问by brazorf

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::group() 方法。无论如何,如果我想以这种方式定义控制器

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.

我想在我的 route.php 中对此进行编码,而不是在控制器构造函数中。

回答by The Alpha

In the constructorof your controller you may use

constructor您的控制器中,您可以使用

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

Also, you can use

此外,您可以使用

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

回答by Fran Mu?oz

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.

Blockquote 控制器方法接受两个参数。第一个是控制器处理的基本 URI,而第二个是控制器的类名。接下来,只需将方法添加到您的控制器,并以它们响应的 HTTP 动词为前缀。

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

Route::controller 负责使用 REST 命名约定创建一组路由。被认为是为了创建 RESTFull 服务。

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

可以在类似于“常规”路由的控制器路由上指定 Blockquote 过滤器:

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

您可以在 Laravel 文档中阅读有关控制器过滤器的信息:http://laravel.com/docs/controllers#controller-filters