在其他控制器中找不到Laravel Auth :: user [英] Laravel Auth::user not found in other controller
问题描述
注册用户后,电子邮件将通过激活链接(auth_code)发送到该用户电子邮件,该链接链接到此功能:
When a user is registered an email is send to the user email with an activation link (auth_code) which links to this function:
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
Now there is my problem. Via the UserConstructor
it wil lead to the 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,因此将以某种方式将用户注销,这是怎么回事?
And when I do Auth::check()
inside the CompanyController it will return false, so somehow it will log the user off in the process, what is going wrong?
(confirmUser中的Auth :: check()结果为true)
(Auth::check() in the confirmUser will give true as result)
推荐答案
从我阅读的内容中得知.您正在使用参数CompanyController实例化UserController.
From what I read. You are instantiating the UserController with the parameter CompanyController.
此实例化是在您实际发送Auth :: login()调用之前完成的.
This instantiation is done before you have actually send the Auth::login() call.
当您在userController
上运行confirmUser
之前,用__construct
实例化公司控制器时,对象CompanyController在进行Auth::login()
调用之前就已经存在.
As you are instantiating the company controller with __construct
before running confirmUser
on the userController
the object companyController exists before the Auth::login()
call is made.
这篇关于在其他控制器中找不到Laravel Auth :: user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!