【问题标题】:Laravel 5.3 auth check in constructor returning falseLaravel 5.3 auth 检查构造函数返回 false
【发布时间】:2017-01-04 07:15:40
【问题描述】:

我正在使用Laravel 5.3,我正在尝试在constructor 方法中获取authenticated 用户的id,以便我可以按指定公司过滤用户,如下所示:

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\View;
use App\Models\User;
use App\Models\Company;
use Illuminate\Support\Facades\Auth;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests ;

    public $user;
    public $company;


    public function __construct()
    {


        $companies = Company::pluck('name', 'id');
        $companies->prepend('Please select');
        view()->share('companies', $companies);
        $this->user = User::with('profile')->where('id', \Auth::id())->first();
        if(isset($this->user->company_id)){
            $this->company = Company::find($this->user->company_id);
            if (!isset($this->company)) {
                $this->company = new Company();
            }
            view()->share('company', $this->company);
            view()->share('user', $this->user);
        }

    }

但是这不会返回用户id。我什至尝试过Auth::check(),但它不起作用。

如果我将 Auth::check()__construct() 方法中移出,则其工作方式如下:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        dd(\Auth::check());
        return view('home');
    }
}

但是,如果我把它放在HomeController 的构造方法中,这失败

任何想法为什么会失败?

【问题讨论】:

    标签: php laravel authentication laravel-5.3


    【解决方案1】:

    它失败了,因为你在parent::__construct(); 之后调用了$this-&gt;middleware('auth');。这意味着您的身份验证中间件未正确加载。

    【讨论】:

    • 我把它放在了 parent::__construct();这并没有什么不同。我什至删除了中间件,没有区别。有代码示例吗?
    【解决方案2】:

    由于 5.3 Auth::check 不能在控制器的构造器中工作,这是未记录的更改之一。因此,您需要将其移至中间件或签入控制器方法,或将项目移至 5.2.x。

    【讨论】:

    • 真的吗?我没有意识到这一点!废话
    • @Alexey Mezenin,看来你是 laravel 大师。我需要你帮忙。看这里:stackoverflow.com/questions/41047583/…
    • 谢谢。但你是怎么知道的?
    【解决方案3】:

    docs

    您无法访问您的会话或经过身份验证的用户 控制器的构造函数,因为中间件还没有运行。

    作为替代方案,您可以直接定义基于闭包的中间件 在控制器的构造函数中。在使用此功能之前,请确保 您的应用程序正在运行 Laravel 5.3.4 或更高版本:

    class ProjectController extends Controller
    {
        /**
         * All of the current user's projects.
         */
        protected $projects;
    
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware(function ($request, $next) {
                $this->projects = Auth::user()->projects;
    
                return $next($request);
            });
        }
    }
    

    【讨论】:

    • @ABDEL-RHMAN 我的是 5.3.4,你不知道如何升级到你的版本?
    • @ABDEL-RHMAN 完全按照您所说的完成并工作,谢谢
    猜你喜欢
    • 2017-05-24
    • 2015-03-17
    • 2017-03-25
    • 2017-05-09
    • 2017-05-11
    • 2011-08-20
    • 2017-03-17
    • 2019-06-28
    相关资源
    最近更新 更多