N
N
nasvek2020-12-28 17:25:42
Laravel
nasvek, 2020-12-28 17:25:42

Why does a POST request to register in Laravel 8.x return a 404 error?

Tell me why when sending a POST request with registration data after adding a user record to the database, it returns a 404 error
Laravel is on Virtual Box + Vagrant
The situation is this

There is a template with a form

<form method="POST" action="{{ route('register_post') }}">
                        @csrf
                            @error('user_lastname')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror

                            @error('user_patronymic')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror

                            @error('user_patronymic')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror

                        <div class="form-group row">
                            <label for="user_firstname" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
                            <div class="col-md-6">
                                <input id="user_firstname" type="text" class="form-control @error('user_firstname') is-invalid @enderror" name="user_firstname" value="{{ old('user_firstname') }}" required autocomplete="name" autofocus>
                                @error('user_firstname')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="user_email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="user_email" type="email" class="form-control @error('user_email') is-invalid @enderror" name="user_email" value="{{ old('user_email') }}" required autocomplete="email">

                                @error('user_email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="user_password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
                            <div class="col-md-6">
                                <input id="user_password" type="password" class="form-control @error('user_password') is-invalid @enderror" name="user_password" required autocomplete="new-password">

                                @error('user_password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="user_password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

                            <div class="col-md-6">
                                <input id="user_password-confirm" type="password" class="form-control" name="user_password_confirmation" required autocomplete="new-password">
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Register') }}
                                </button>
                            </div>
                        </div>
                    </form>


In route - web.php this line:
Route::get('register', [RegisterController::class, 'showRegistrationForm'])->name('register');
Route::post('register', [RegisterController::class, 'register'])->name('register_post');


With a GET request, the form is displayed normally, when you click on the register button, the following logic is enabled:
public function register(Request $request)
    {
        $this->validator($request->all())->validate();
        
        event(new Registered($user = $this->create($request->all())));
        $this->guard()->login($user);

        if ($response = $this->registered($request, $user)) {

            return $response;
        }

        return $request->wantsJson()
                    ? new JsonResponse([], 201)
                    : redirect($this->$redirectTo);
    }

    protected function create(array $data)
    {
        return User::create([
            'user_usertypeid' => 1,
            'user_email' => $data['user_email'],
            'user_firstname' => $data['user_firstname'],
            'user_password' => Hash::make($data['user_password']),
        ]);
    }


And it turns out that after the create method, the record is added to the Database and the work of the register function is interrupted and a 404 page is displayed at site.ru/register

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sanes, 2020-01-03
@Sanes

In Laravel 8, Fortify is used for authorization.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question