使用 Laravel 8(和 Jetstream)将自定义数据传递到 login.blade.php

Posted

技术标签:

【中文标题】使用 Laravel 8(和 Jetstream)将自定义数据传递到 login.blade.php【英文标题】:Passing custom data into login.blade.php with Laravel 8 (and Jetstream) 【发布时间】:2020-12-28 05:07:47 【问题描述】:

没有实现我自己的登录控制器,有没有办法使用 Laravel 8 和 Jetstream (Livewire) 将数据传递到 views/auth/login.blade.php

在 Laravel 7 中,LoginController 有一个 showLoginForm() 方法,你可以重写它并返回一个带有附加数据的视图。 Jetstream 似乎并不那么简单。

【问题讨论】:

【参考方案1】:

这就是我解决它的方法:(通过 laravel 的管道概念)

public function showLoginForm()
    
        return view('admin.auth.login');
    

    public function logout()
    
        Session::flush();
        Auth::logout();
        return redirect('admin/login');
    

    public function login(Request $request)
    
        return $this->loginPipeline($request)->then(function ($request) 
            return app(LoginResponse::class);
        );
    

    protected function loginPipeline($request)
    
        if (Fortify::$authenticateThroughCallback) 
            return (new Pipeline(app()))->send($request)->through(array_filter(
                call_user_func(Fortify::$authenticateThroughCallback, $request)
            ));
        

        if (is_array(config('fortify.pipelines.login'))) 
            return (new Pipeline(app()))->send($request)->through(array_filter(
                config('fortify.pipelines.login')
            ));
        

        return (new Pipeline(app()))->send($request)->through(array_filter([
            config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
            RedirectIfTwoFactorAuthenticatable::class,
            AttemptToAuthenticate::class,
            PrepareAuthenticatedSession::class,
        ]));
    

如果您想自定义字段,可以说“状态”=>“活动”。然后您需要将 RedirectIfTwoFactorAuthenticatable 复制到操作文件夹。并做这样的事情:

 return tap($model::where(Fortify::username(), $request->Fortify::username())
            ->where('status', 'active')
            ->first(), 
            function ($user) use ($request) 
            if (!$user || !Hash::check($request->password, $user->password)) 
                $this->throwFailedAuthenticationException($request);
            
        );

享受:)

【讨论】:

【参考方案2】:

Recent updates 到 Jetstream/Fortify 已经解决了这个问题。

【讨论】:

满足我们对定制的需求

以上是关于使用 Laravel 8(和 Jetstream)将自定义数据传递到 login.blade.php的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 8 JetStream 引导程序 4

Laravel 8 jetstream 热重载和浏览器同步不起作用

Laravel 8 Jetstream Inertia 没有渲染视图组件

CSS 不会在 XAMPP 上的 Laravel 8 + Jetstream 中加载

Jetstream 与 Liviwere - Laravel 8 - 运行 npm install 时的节点漏洞

如何使用 laravel 8 +jetstream + spatie 为注册用户分配角色