php - Laravel Auth::user 在其他 Controller 中找不到

标签 php mysql laravel

当用户注册时,将向用户电子邮件发送一封电子邮件,其中包含链接到此功能的激活链接(auth_code):

public function confirmUser($authentication_code)
{
    if (!$authentication_code) {
        return 'auth code not found!';
    }

    $user = User::where('authentication_code', '=', $authentication_code)->first();

    if (!$user) {
        return 'user not found!';
    }

    $user->active = 1;
    $user->save();

    Session::put('user_id', $user->id);

    Auth::login($user);

    return view('user.setpassword', ['user' => $user]);
}

因此用户将登录。

现在我的问题来了。通过 UserConstructor 它将导致 CompanyController

//UserController
public function  __construct(User $user, CompaniesController $companies, UserTypeController $userType, AttributeController $attributes)
{
    $cid = Auth::user()->company_id;

    if (Auth::user()->usertype_id == 7) {
        $this->user = $user;
    }
    else
    {
        $array_company_ids = $companies->getCompany_ids($cid);
        $this->user = $user->whereIn('company_id', $array_company_ids);
    }

}

//CompanyController
public function __construct(Company $company)
{
    if (Auth::user()->usertype_id == 7) {
        $this->company = $company;
    } else {
        $this->company_id = Auth::user()->company_id;
        $this->company = $company->Where(function ($query) {
            $query->where('id', '=', $this->company_id)
                ->orWhere('parent_id', '=', $this->company_id);
        });
    }

    $this->middleware('auth');

    $page_title = trans('common.companies');
    view()->share('page_title', $page_title);
}

导致此错误:enter image description here

当我在 CompanyController 中执行 Auth::check() 时,它将返回 false,因此它会以某种方式在此过程中注销用户,这是怎么回事?

(confirmUser 中的 Auth::check() 将给出 true 作为结果)

最佳答案

从我读到的。您正在使用参数 CompanyController 实例化 UserController。

此实例化是在您实际发送 Auth::login() 调用之前完成的。

当您在 userController 上运行 confirmUser 之前使用 __construct 实例化公司 Controller 时,对象 companyController 在 Auth 之前存在: :login() 调用。

关于php - Laravel Auth::user 在其他 Controller 中找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31074295/

相关文章:

php - 从 MySQL 表中获取 PHP 测验结果

PHP-MySQL 链接检索正常,但在表中移动链接

mysql - 即使使用错误的凭据也能成功登录吗?

c# - 使用 C# 同步 Mysql 数据库的最佳方法

php - Laravel 5.2 + 上传文件并在数据库中保存名称

Php递归函数返回null而变量具有值

php - 如何在 eloquent 中执行依赖于表最后一行的更新和保存操作?

php - 此缓存存储不支持 Laravel 8 标记

php - Laravel 4 在数据库中存储索引而不是值

Php 说找不到已经包含的类