当用户注册时,会向用户电子邮件发送一封带有激活链接 (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,因此它会以某种方式将用户注销,这是出了什么问题?( Auth::check() 在 confirmUser 中将给出 true 作为结果)
最佳答案
从我读到的。您正在使用参数 CompanyController 实例化 UserController。
此实例化在您实际发送 Auth::login() 调用之前完成。
当您在 __construct
上运行 confirmUser
之前使用 userController
实例化公司 Controller 时,对象 companyController 在进行 Auth::login()
调用之前存在。
关于php - 在其他 Controller 中找不到 Laravel Auth::user,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31074295/