Laravel 5.3 Auth Check in构造函数返回false [英] Laravel 5.3 auth check in constructor returning false

查看:84
本文介绍了Laravel 5.3 Auth Check in构造函数返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.3,并且尝试通过constructor方法获取已认证用户的id,因此我可以按以下方式按指定的公司来过滤用户:

I'm using Laravel 5.3 and I'm trying to get the authenticated user's id in the constructor method so I can filter the user by assigned company as follows:

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(),但它不起作用.

However this doesn't return the user id. I've even tried Auth::check() and it doesn't work.

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

If I move the Auth::check() out of the __construct() method then this works as follows:

<?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的构造方法中,此会失败

However this fails if I put this in the construct method in the HomeController too!

有什么想法为什么会失败?

Any ideas why this is failing?

推荐答案

文档

您无法访问自己的会话或经过身份验证的用户 控制器的构造函数,因为中间件尚未运行.

you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.

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

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:

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);
        });
    }
}

这篇关于Laravel 5.3 Auth Check in构造函数返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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