Laravel 5 Auth在Controller构造中不是对象 [英] Laravel 5 Auth is non object in Controller construct

查看:182
本文介绍了Laravel 5 Auth在Controller构造中不是对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

app\Http\Controller中,有一个名为Controller的主控制器类,所有其他控制器都继承该类.在任何其他控制器中,以下代码都可以正常工作:

In app\Http\Controller there is a master controller class named Controller that all other controllers inherits. In any other controller the following code works fine:

class OtherController extends Controller{

   public function index(){
      dd(\Auth::user()->id);
   }
} 

但是,在Controller.php即父类中,当我尝试在construct方法中执行类似的操作时,我得到了试图获取非对象的属性错误:

However, in the Controller.php i.e the parent class, when I try to do something like that in the construct method I have got Trying to get property of non-object error:

class Controller extends BaseController {

    use AuthorizesRequests,
        DispatchesJobs,
        ValidatesRequests;
    public $foo = 'null';

    public function __construct() {
        $this->middleware('auth');
        dd(\Auth::user()->id);//Error is here.
        dd(\Route::getCurrentRoute()->action['controller']);

    }

}

我该如何解决这个问题?

How could I solve this issue?

推荐答案

编辑以添加实际答案

尝试声明此方法并将代码放入parent::call_action()行之前:

Try declaring this method and putting your code in it, before the parent::call_action() line:

public function callAction($method, $parameters)
{
    // Put your code right here

    // then do this last
    return parent::callAction($method, $parameters);
}

来自页面底部.简而言之,总是在调用目标方法之前立即调用callAction,但在中间件运行之后也将其称为之后,因此您将能够正确访问auth外观.很有可能,您在构造函数中尝试执行的任何操作都将按您希望的方式在此方法中起作用.

Taken from the bottom of this page. The short of it is that callAction is always called right before the destination method is called, but it is also called after the middleware runs, so you will be able to properly access your auth facade. In all likelihood whatever you were trying to do in the constructor will work as you wanted it to in this method.

原始答案

自从我在laravel 5.4中进行研究以来,已经有点过了,但是如果我没记错的话,中间件直到控制器构造函数之后才会启动,这意味着您不能做您正在尝试做什么.直到构造控制器之后,但在调用controller方法之前,auth中间件才使用登录的用户填充Auth外观(这就是第一个示例起作用的原因).

It's been a bit since I've poked around in laravel 5.4, but if I recall correctly the middleware doesn't fire until after the controller constructor, which means that you specifically can't do what you are trying to do. The auth middleware doesn't populate the Auth facade with the logged in user until after the controller is constructed, but before the controller method is called (which is why your first example works).

我相信这是预期的行为,因此不太可能改变.

I believe that this is the intended behavior, so it is unlikely to change.

它的缺点是(除非有更多知识渊博的人纠正我),您将必须找到另一种方法来做您想做的事情.

The short of it is (unless someone more knowledgeable corrects me) that you will have to find another way to do whatever it is you are trying to do.

编辑以添加

的确,这个问题清楚地表明我的怀疑是正确的.中间件在控制器构造函数之后触发 ,这意味着您无法访问控制器构造函数内部的登录用户.回想起来,显然是这种情况,因为您可以在控制器构造函数中执行的一件事是指定系统应运行的中间件.如果中间件已经运行,那么您将无法执行该操作.因此,它的不足之处是您不能(实际上)从控制器构造函数中访问Auth外观. laravel团队有意这样做,因此不会很快改变.您将不得不找到另一种方式.

Indeed, this question makes it clear that my suspicions were correct. Middleware fires after the controller constructor, which means that you cannot access the logged in user inside the controller constructor. In retrospect this is obviously the case because one thing you can do from within the controller constructor is specify which middlewares the system should run. If the middleware had already run, then you wouldn't be able to do that. So the short of it is that you can't (practically) access the Auth facade from within the controller constructor. This is intentional on the part of the laravel team, so it won't change anytime soon. You will have to find another way to do it.

这篇关于Laravel 5 Auth在Controller构造中不是对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆