Regarding the problem of not being able to perform screen transition after register (user registration) in Ravel 5.

Asked 2 years ago, Updated 2 years ago, 101 views

I would like to transfer to the profile screen after registering with Ravel, but the registration process has been completed, and the URL I want to redirect after POSTing properly with Chrome's developer tool has been returned in Response, but for some reason the screen remains unchanged.

I have also changed the redirect destination of the properties, but no matter how much I look into the cause, does anyone know why?

Tried

Re Change the description of RegisterController.php as follows

before change:

protected$redirectTo='/home';

after modification:

protected$redirectTo='/mypage/profile';

ReCreate the following redirectTo method in RegisterController.php

protected function redirectTo()
    {
        return route('mypage.prof');
    }

ChTried using a browser other than Chrome

vI set up a server with vagrant and homestead, so I tried phpartisan service

pI tried clearing the cache with the command of phpartisan cache clearing system

ChRemove cookies from Chrome settings

The behavior did not change when I tried the above.I tried it as long as I could, but there were no errors and I don't know the cause at all.Are there any other possible causes?

By the way, as shown in the image below, the URL of the transition destination is GET and the response is returned properly.

Enter a description of the image here

Related Source Code

Network

**register**

Request URL: http://homestead.test/register
Request Method: POST
Status Code: 302 Found
Remote Address: 192.168.20.10:80
Referrer Policy: no-referrer-when-downgrade

**profile**

Request URL: http://homestead.test/mypage/profile
Request Method:GET
Status Code: 200 OK
Remote Address: 192.168.20.10:80
Referrer Policy: no-referrer-when-downgrade

web.php

Route::get('/mypage/profile', 'UserController@showProf') ->name('mypage.prof');

UserController.php

//View profile screen
    public function showProf()
    {
        $user=Auth::user();
        US>return view('user.prof',[
            'user' = > $user,
        ]);
    }

Router.php

public function auth(array$options=[])
    {
        // Authentication Routes...
        $this->get('login', 'Auth\LoginController@showLoginForm') ->name('login');
        $this->post('login', 'Auth\LoginController@login');
        $this->get('logout', 'Auth\LoginController@showLogout') ->name('logout');
        $this->post('logout', 'Auth\LoginController@logout');

        // Registration Routes...
        if($options['register']??true){
            $this->get('register', 'Auth\RegisterController@showRegistrationForm') ->name('register');
            $this->post('register', 'Auth\RegisterController@register');
        }
        $this->get('withdraw', 'Auth\RegisterController@showWithdraw') ->name('withdraw');


        // Password Reset Routes...
        if($options['reset']??true){
            $this->resetPassword();
        }

        // Email Verification Routes...
        if($options['verify']??false){
            $this->emailVerification();
        }
    }

RegisterController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
useApp\Http\Controllers\Controllers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extensions Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation.By default this controller uses a trace to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected$redirectTo='/mypage/profile';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function__construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param array $data
     * @return\Illuminate\Contracts\Validation\Validator
     */
    protected function validator (array$data)
    {
        return Validator::make($data,[
            'email' = > ['required', 'string', 'email', 'max:255', 'unique:users',
            'password' = > ['required', 'string', 'min:6', ],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param array $data
     * @return\App\User
     */
    protected function create (array$data)
    {
        return User::create([
            'email'=>$data['email',
            'password' = > Hash::make ($data['password']),
        ]);
    }
}

RegistersUsers.php

<?php

namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;

trace RegistersUsers
{
    use RedirectsUsers;

    /**
     * Show the application registration form.
     *
     * @return\Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    /**
     * Handle a registration request for the application.
     *
     * @param\Illuminate\Http\Request$request
     * @return\Illuminate\Http\Response
     */
    public function register (Request$request)
    {
        $this->validator($request->all())->validate();

        event(newRegistered($user=$this->create($request->all()));

        $this->guard()->login($user);

        return$this->registered($request,$user)
            ?: redirect($this->redirectPath());

    }

    /**
     * Get the guard to be used during registration.
     *
     * @return\Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard();
    }

    /**
     * The user has been registered.
     *
     * @param\Illuminate\Http\Request$request
     * @param mixed$user
     * @return mixed
     */
    protected function registered(Request$request,$user)
    {
        //
    }

}

register.blade.php

@extends('layouts.app2')

@section('title', 'membership registration')

@section('content')
    <divid="app">
        <user-register></user-register>
    </div>
@endsection

UserRegister.vue

<template>
    <div class="l-form p-form">
        <h2class="l-form__head p-form_head">
            new membership registration
        </h2>

        <div class="l-form__body p-form_body">
            <div class="l-form-container">
                <div class="form-group">
                    <label for="email" class="l-form__label p-form__label">mail address</label>
                    <span class="label-require">Required</span>
                    <input id="email" class="l-form__input p-form__input": class="{hasErr:errors.email}" type="text" v-model="email" placeholder="can be either a PC or a mobile phone">
                    <div class="area-msg">
                        {{ errors.email}}
                    </div>
                </div>


                <div class="form-group">
                    <label for="password" class="l-form__label p-form__label">password</label>
                    <span class="label-require">Required</span>
                    <input id="password" class="l-form__input p-form__input":class="{hasErr:errs.password}"type="password"v-model="password"placeholder="6 or more half-width alphanumeric characters">
                    <div class="area-msg">
                        {{ errors.password}}
                    </div>
                </div>

                <button type="button" class="p-btn btn-primary"@click="register">Register </button>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data:function(){
            US>return{
                email:',
                password:',
                errors:'
            }
        },
        methods: {
            register:function(){

                This.errors={};

                varself = this;
                var url = '/register';
                var params = {
                    email:this.email,
                    password —This.password,
                };
                axios.post(url,params)
                .then(function(response){
                    self.email=';
                    self.password=';
                })
                .catch(function(error){

                    var responseErrors = error.response.data.errors;
                    variables={};

                    for (var key in responseErrors) {

                        errors [key] = responseErrors [key][0];

                    }

                    self.errors=errors;
                });
            }
        },
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

php laravel laravel-5

2022-09-29 21:45

1 Answers


not from a browser For XMLHttpRequest, 302 redirects do not forward anything in XMLHttpRequest.
XMLHttpRequest is intended to send HTTP requests without screen transition, so we have to forward them ourselves.

There are several ways to fix it, but
I think it would be good to think about which one is better for the application specifications.

·POST in Form using HTML instead of Ajax
·Check the control of axios, catch 302 and have it transferred
·Stop transferring with 302 redirect

Personally
·Stop transferring with 302 redirect
I think it's better.
This is probably because the register method is only a new registration function. Why don't you leave the transfer to the client?
From an API perspective, it's simple and functions can be distinguished

Specifically,
RegisterController.php
Stop Redirecting

return$this->registered($request,$user)
    ?: If $this->redirectPath();//200 can be returned, any response will be fine.OK, or the URL of the transfer destination.

UserRegister.vue

.then(function(response){
                self.email=';
                self.password=';
                // This is considered to be a success
                location.href = "Transfer destination URL"; // If the transfer destination is different for each request, you can include it in the response.If it's fixed, you can write it down.
            })

Is it like that?
すいません I'm sorry, but I don't have a Ravel environment at hand, so I probably won't be able to get through.Recognize it as an implementation image and rewrite it accordingly.


2022-09-29 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.