Laravelで新規登録後にプロフィール画面に遷移させたいのですが、登録の処理は出来ているし、ChromeのデベロッパーツールでNetworkで通信を確認してもちゃんとPOSTされた後にリダイレクトしたいURLもResponseで返ってきているのですが、何故か画面は切り替わらず登録画面のままです。
プロパティのリダイレクト先も変更したのですが、原因がいくら調べてもわかりません何故なのか分かる方いらっしゃいますか。
試したこと
①RegisterController.phpの記述を以下の通り変更
変更前:
protected $redirectTo = '/home';
変更後:
protected $redirectTo = '/mypage/profile';
②RegisterController.phpに下記のredirectToメソッドを作成
protected function redirectTo()
{
return route('mypage.prof');
}
③Chrome以外のブラウザで試した
④vagrantとhomesteadでサーバーを立てていたので、php artisan serveの方でやってみた
⑤php artisan キャッシュクリア系のコマンドでキャッシュクリアしてみた
⑥Chromeの設定でCookieを削除
上記のことを試しても挙動が変わりませんでした。自分で調べられる限りは試してみたのですがエラーも出ていないしさっぱり原因がわかりません。他に考えうる原因はあるのでしょうか?
ちなみに下記画像のように遷移先のURLはGETでResponseがちゃんと返ってきています。
関連するソースコード
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
// プロフィール画面表示
public function showProf()
{
$user = Auth::user();
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;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait 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;
trait 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(new Registered($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', '会員登録')
@section('content')
<div id="app">
<user-register></user-register>
</div>
@endsection
UserRegister.vue
<template>
<div class="l-form p-form">
<h2 class="l-form__head p-form__head">
新規会員登録
</h2>
<div class="l-form__body p-form__body">
<div class="l-form-conteiner">
<div class="form-group">
<label for="email" class="l-form__label p-form__label">メールアドレス</label>
<span class="label-require">必須</span>
<input id="email" class="l-form__input p-form__input" :class="{ hasErr: errors.email }" type="text" v-model="email" placeholder="PC・携帯どちらでも可">
<div class="area-msg">
{{ errors.email }}
</div>
</div>
<div class="form-group">
<label for="password" class="l-form__label p-form__label">パスワード</label>
<span class="label-require">必須</span>
<input id="password" class="l-form__input p-form__input" :class="{ hasErr: errors.password }" type="password" v-model="password" placeholder="6文字以上の半角英数字">
<div class="area-msg">
{{ errors.password }}
</div>
</div>
<button type="button" class="p-btn btn-primary" @click="register">登録する</button>
</div>
</div>
</div>
</template>
<script>
export default {
data: function() {
return {
email: '',
password: '',
errors: ''
}
},
methods: {
register: function () {
this.errors = {};
var self = 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;
var errors = {};
for(var key in responseErrors) {
errors[key] = responseErrors[key][0];
}
self.errors = errors;
});
}
},
mounted() {
console.log('Component mounted.')
}
}
</script>