亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤:當我直接在控制器的構造函數中嘗試基于閉包的中間件時,嘗試獲取非對象的屬性“標頭”。

錯誤:當我直接在控制器的構造函數中嘗試基于閉包的中間件時,嘗試獲取非對象的屬性“標頭”。

PHP
Helenr 2022-09-03 17:29:03
在我的Laravel 5.2中。在構造函數中public function __construct(){         $this->data['user'] =  Auth::guard('customer')->user();            $this->middleware('customer');}因此,我可以在控制器中的任何位置使用“$this->data['user']”。但在Laravel 5.3或更高版本中,我們無法訪問構造函數中經過身份驗證的用戶。當我嘗試在構造函數中使用基于閉包的中間件時。public function __construct(){        $this->data['user'] =  Auth::guard('customer')->user();    $this->middleware('customer');}然后我收到錯誤:錯誤異常 嘗試獲取非對象 http://localhost/dhruv/intranet-v6.0/public/profile 的屬性“標頭”
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

試試這個方式


public function __construct()

{

    $this->middleware('customer');

    $this->middleware(function ($request, $next) {

        $this->data['user'] = Auth::guard('customer')->user();


        return $next($request);

    });

}


查看完整回答
反對 回復 2022-09-03
  • 1 回答
  • 0 關注
  • 78 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號