当用户注册时,将向用户电子邮件发送一封电子邮件,其中包含链接到此功能的激活链接(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);
}
导致此错误:
当我在 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/