ログイン後のリダイレクトが上手くいきません。
現在こちらを参考にマルチログインの実装を行っています。
リンク
「目次:guardメソッドの上書き」
再度/employee/registerからユーザを登録し、/employee/homeにリダイレクトされれば設定はうまく行われています。
現状、ここまでやって/registerから/homeへのリダイレクトができなかった感じです。
/serviceuser/registerでログイン画面の表示はされますが、登録内容記入後にregisterボタンをクリックしても/employee/homeへいかず、再度/serviceuser/registerの画面に戻ると言った感じです(記入内容は保存されている模様)。
web.php
1//Userログイン 2Route::get('/', function () { 3 return view('welcome'); 4}); 5 6Auth::routes(); 7Route::get('/home', 'HomeController@index')->name('home'); 8 9//ServiceUserログイン 10Route::prefix('serviceuser')->namespace('ServiceUser')->name('serviceuser.')->group(function () { 11 Auth::routes(); 12 Route::get('/serviceuser/home', 'ServiceUserHomeController@index')->name('serviceuser_home'); 13});
RegisterController.php
1<?php 2 3namespace App\Http\Controllers\ServiceUser\Auth; 4 5use App\ServiceUser; 6use App\Http\Controllers\Controller; 7use App\Providers\RouteServiceProvider; 8use Illuminate\Foundation\Auth\RegistersUsers; 9use Illuminate\Support\Facades\Auth; 10use Illuminate\Support\Facades\Hash; 11use Illuminate\Support\Facades\Validator; 12 13class RegisterController extends Controller 14{ 15 use RegistersUsers; 16 17 //ServiceUserログイン画面 18 public function showRegistrationForm() 19 { 20 return view('serviceuser.auth.register'); 21 } 22 //RegistersUsersのguardメソッドの上書き 23 protected function guard() 24 { 25 return Auth::guard('serviceuser'); 26 } 27 28 public function __construct() 29 { 30 $this->middleware('guest'); 31 } 32 33 protected function validator(array $data) 34 { 35 return Validator::make($data, [ 36 'company' => ['required', 'string', 'max:255'], 37 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 38 'password' => ['required', 'string', 'min:8', 'confirmed'], 39 ]); 40 } 41 protected function create(array $data) 42 { 43 return ServiceUser::create([ 44 'company' => $data['company'], 45 'email' => $data['email'], 46 'password' => Hash::make($data['password']), 47 ]); 48 } 49}
ServiceUserHomeController.php
1<?php 2 3namespace App\Http\Controllers\ServiceUser; 4 5use App\Http\Controllers\Controller; 6use Illuminate\Http\Request; 7 8class ServiceUserHomeController extends Controller 9{ 10 //ユーザのみアクセス可能 11 public function __construct() 12 { 13 $this->middleware('auth:serviceuser'); 14 } 15 //serviceuser_homeの呼び出し 16 public function index() 17 { 18 return view('serviceuser.home'); 19 } 20}
RouteServiceProvider.php
1<?php 2 3namespace App\Providers; 4 5use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 6use Illuminate\Support\Facades\Route; 7 8class RouteServiceProvider extends ServiceProvider 9{ 10 protected $namespace = 'App\Http\Controllers'; 11 //Userログイン後の画面 12 public const HOME = '/home'; 13 //ServiceUserログイン後の画面 14 public const SERVICEUSER_HOME = '/serviceuser/home'; 15 16 public function boot() 17 { 18 // 19 20 parent::boot(); 21 } 22 23 public function map() 24 { 25 $this->mapApiRoutes(); 26 27 $this->mapWebRoutes(); 28 29 // 30 } 31 32 protected function mapWebRoutes() 33 { 34 Route::middleware('web') 35 ->namespace($this->namespace) 36 ->group(base_path('routes/web.php')); 37 } 38 39 protected function mapApiRoutes() 40 { 41 Route::prefix('api') 42 ->middleware('api') 43 ->namespace($this->namespace) 44 ->group(base_path('routes/api.php')); 45 } 46}
やりたいこと:
/employee/registerからユーザを登録し、/employee/homeにリダイレクトされるようにしたいです。
よろしくお願いします。
使用バージョン
Laravel Framework 6.20.44
あなたの回答
tips
プレビュー