如何将与Laravel Livewire收集的数据发送到Fortify? [英] How to send data colected with Laravel Livewire to Fortify?

查看:73
本文介绍了如何将与Laravel Livewire收集的数据发送到Fortify?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全不熟悉Vue.js,因此使用Livewire找到了很好的替代品.

I'm not familiar with Vue.js at all, so found a good replacement using Livewire.

我要解决的挑战是使用Fortify + Livewire在我的网站上进行用户友好的注册.注册过程是一个多步骤的过程,取决于用户做出的选择,它将加载相关字段.

The challenge that I've got to solve is to have a user friendly registration on my website using Fortify + Livewire. The registration process is a multistep one and depends on the choices that the user makes it will load the relative fields.

到目前为止,我通过在 FortifyServiceProvider.php 文件中添加以下代码来设置Fortify视图:

So far I set up the Fortify views by adding in the FortifyServiceProvider.php file the following code:

Fortify::loginView(function () {
    return view('auth.login');
});

Fortify::registerView(function () {
    return view('auth.register');
});

auth/login.blade.php 视图加载了livewire组件,该组件基本上是一种形式:

The auth/login.blade.php view loading the livewire component which is basically a form:

<form action="{{ route('register') }}" method="POST" wire:submit.prevent="submit">
    /**
    * Here would go the inputs that must be shown depends on what users choice 
    * (is it an ordinar user or a company)
    */
    <button type="submit">Save<button/>
</form>

通过将 $ step 属性添加到 Register.php 类中,可以解决多种形式的挑战:

The multiform challenge would be resolved by adding $step property into the Register.php class:

class RegisterForm extends Component
{
    public $step;

    public function mount()
    {
        $this->step = 0;
    }

    public function submit()
    {
        if ($this->step < 3) {
            $this->step++;           
        } else {
            // pass all the data to the fortify register method
            // <-- Here is my trouble!
        }
    }
}

,将通过传递每个注册步骤($ this-> step ++)来递增.

which will be incremented by passing each of the registration steps ($this->step++).

对我而言,最重要的事情是如何防止表单提交进行验证+表单更改,最后阻止所有数据集通过Fortify注册​​过程?

The most important thing that is quite complicated for me is how to prevent form submission to have the validation + form changes and by the end all the set of the data to pass trough Fortify registration process?

推荐答案

查看要注册的​​强化控制器

Look at the fortify Controller for register

public function store(Request $request, CreatesNewUsers $creator): RegisterResponse
    {
        event(new Registered($user = $creator->create($request->all())));
        $this->guard->login($user);
        return app(RegisterResponse::class);
    }

T

use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Auth;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Livewire\Component;

class Register extends Component
{
    public $name;
    public $email;
    public $password;
    public $password_confirmation;

    public function submit(CreatesNewUsers $creator)
    {
        event(new Registered($user = $creator->create([
            'name' => $this->name,
            'email' => $this->email,
            'password' => $this->password,
            'password_confirmation' => $this->password_confirmation,
        ])));

        Auth::guard()->login($user);

        $this->redirect('home');
    }

    public function render()
    {
        return view('livewire.register');
    }
}

类似的情况适用于您的用例.

Something like this will work for your use case.

您仍在使用强化操作并仍在触发事件

You are still using the fortify Action and Still Firing the Event

这篇关于如何将与Laravel Livewire收集的数据发送到Fortify?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆