Laravel Auth ::在其他控制器

时间:2015-06-26 13:21:22

标签: 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)

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

我确实用隐藏的输入字段发送了它。尝试这样做:

<input id="email" type="hidden" name="email" value="{{ Auth::user()->email }}" required> 

我的工作非常完美。