首页 文章

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

提问于
浏览
6

我知道我能做到这一点

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

应用路由一些过滤器 . 我也知道Route :: group()方法 . 无论如何,如果我想以这种方式定义控制器

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

我无法传递数组作为第二个参数 .

问题: how to apply filters to the following route?

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

=== EDIT

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

2 回答

  • 1

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

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

    此外,你可以使用

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

    Blockquote控制器方法接受两个参数 . 第一个是控制器处理的基URI,而第二个是控制器的类名 . 接下来,只需向控制器添加方法,前缀为它们响应的HTTP谓词 .

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

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

    因为此函数只允许两个参数,所以可以在构造函数中应用控制器过滤器 . 例如:

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

    您可以在Laravel文档中了解控制器过滤器:http://laravel.com/docs/controllers#controller-filters

相关问题