Laravel 4 除了控制器构造函数中的过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17076011/
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 07:57:09  来源:igfitidea点击:

Laravel 4 except filter in controller constructor

phplaravellaravel-4

提问by JasonMortonNZ

Currently I have an AdminContoller with a construct method handling some of the before filters. Is there a way to do a before filter on all controller methods except one?

目前我有一个 AdminContoller,它有一个构造方法来处理一些之前的过滤器。有没有办法对除一个以外的所有控制器方法进行前置过滤?

I'm using Entrust for Roles and Permissions, but this code is throwing me into an infinite redirect loop. I'm not logged in as a user at all. So this code should redirect me to the /admin/login url which is attached to an unfiltered AdminController@adminLogin method. But it doesn't?

我将 Entrust 用于角色和权限,但此代码使我陷入无限重定向循环。我根本没有以用户身份登录。所以这段代码应该将我重定向到 /admin/login url,它附加到一个未过滤的 AdminController@adminLogin 方法。但它没有?

// AdminController.php file

// AdminController.php 文件

class AdminController extends BaseController {

    function __construct() {

        // Is something like this possible?
        $this->beforeFilter('admin', array('except' => array('adminLogin')));
        $this->beforeFilter('csrf', array('on' => 'post'));
    }

    public function index()
    {
        return "Admin - Index";
    }

    public function adminLogin()
    {
        return "Admin Login Form";
    }

    // ... and many more methods
}

// Filter.php file

//过滤器.php文件

Route::filter('admin', function()
{
    if( !Entrust::hasRole('admin') ) // Checks the current user
    {
        return Redirect::to('/admin/login');
    }
});

// Routes.php file

// Routes.php 文件

Route::resource('admin', 'AdminController');

Route::get('/admin/login', 'AdminController@adminLogin');

采纳答案by Thomas Clarkson

As you've added a new method into a resourceful controller, you should register the new method first, before the resource.

当您将新方法添加到资源丰富的控制器中时,您应该先注册新方法,然后再注册资源。

E.g.

例如

<?php // Routes.php

Route::get('/admin/login', 'AdminController@adminLogin');
Route::resource('admin', 'AdminController');

This way your before filters should work as you have then like this:

这样你的前过滤器应该像你那样工作:

<?php // AdminController.php
   class AdminController extends BaseController {
     function __construct() {
       $this->beforeFilter('admin', array('except' => array('adminLogin')));
      $this->beforeFilter('csrf', array('on' => 'post'));
    }
}

回答by The Alpha

Yes, it's possible, because there is an public $except;and public $only;property in Filterclass in vendor/laravel/framework/src/Illuminate/Routing/Controllers/Filter.phpfile and you can also use onlyinstead of exceptto use a filter only on specific methods.

是的,这是可能的,因为文件中的类中有一个public $except;andpublic $only;属性,您也可以使用而不是仅在特定方法上使用过滤器。Filtervendor/laravel/framework/src/Illuminate/Routing/Controllers/Filter.phponlyexcept

From L4docs for only

来自L4文档only

 $this->afterFilter('log', array('only' => array('fooAction', 'barAction')));

So, this should work too

所以,这也应该有效

$this->beforeFilter('log', array('except' => array('fooAction', 'barAction')));

In L3I've used

L3我用过

$this->filter('before', 'auth')->except(array('login', 'login_ajax', 'fb_login'));

but didn't use in L4but it should work, according to source code and the doc.

但没有使用 inL4但它应该可以工作,根据源代码和文档。