Laravel 10 入門トップページ


目次

  1. 全体の概要
  2. Laravel によるユーザ認証
  3. ユーザ認証を備えたコメント掲示板の開発
  4. マルチ認証の実現
    1. 概要
    2. 準備作業
    3. コントローラの作成
    4. ガードの追加
    5. モデルの作成
    6. データベースのマイグレーション
    7. シーダによる管理者・教員データの登録
    8. コントローラの生成
    9. ガードごとに認証不要のダッシュボードを作成
    10. ログイン機能実装の手順
    11. モデルを認証必須に
    12. リクエストの作成
    13. ログインコントローラの作成
    14. ルートの定義
    15. ログインフォームの作成
    16. コンポーネントの作成
    17. レイアウトの作成
    18. ナビゲーションの作成
    19. コントローラの編集
    20. ビューの作成
    21. ミドルウェアの修正
    22. ルートの定義
    23. 管理者ログインの動作確認
    24. 管理者と教員ページのデザイン変更

Laravel でユーザ認証とマルチ認証を実現する

マルチ認証の実現

ガードごとに認証不要のダッシュボードを作成

システム管理者用のダッシュボード /admin/dashboard と教員用のダッシュボード /professor/dashboard を作成します.まずは,認証不要で誰でも閲覧できるようにして,その後に認証を求めるようにしていきます.

システム管理者用と教員用のコントローラに index 関数を作成します.

app/Http/Controllers/Admin/DashboardController.php<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function index()
    {
        dd("admin dashboard");
    }
}
app/Http/Controllers/Professor/DashboardController.php<?php

namespace App\Http\Controllers\Professor;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function index()
    {
        dd("professor dashboard");
    }
}

次に,routes ディレクトリに admin.php と professor.php を作成し,それぞれのルートを定義します.いずれも prefix を指定していることに注意して下さい.したがって,Admin の場合は,ルートを /dashboard と定義していますが,実際には /admin/dashboard になり,Professor の場合は,/professor/dashboard になります.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\DashboardController;

// Admin 認証不要
Route::group(['prefix' => 'admin'], function() {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard');
});
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Professor\DashboardController;

// Professor 認証不要
Route::group(['prefix' => 'professor'], function() {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('professor.dashboard');
});

いま作成した2つのルート定義ファイルを web.php で読み込むようにします.

<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\User\DashboardController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

// Route::get('/dashboard', function () {
//     return view('dashboard');
// })->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

Route::middleware('auth', 'verified')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
});

require __DIR__.'/auth.php';
require __DIR__.'/admin.php';
require __DIR__.'/professor.php';

ルートが定義されているか確認します.

vagrant@ubuntu2204 laravelAuth $ php artisan route:list

  GET|HEAD  / .......................................................
  POST      _ignition/execute-solution ignition.executeSolution › Sp…
  GET|HEAD  _ignition/health-check ignition.healthCheck › Spatie\Lar…
  POST      _ignition/update-config ignition.updateConfig › Spatie\L…
  GET|HEAD  admin/dashboard admin.dashboard › Admin\DashboardControl…
  GET|HEAD  api/user ................................................
  GET|HEAD  confirm-password password.confirm › Auth\ConfirmablePass…
  POST      confirm-password Auth\ConfirmablePasswordController@store
  GET|HEAD  dashboard .... dashboard › User\DashboardController@index
  POST      email/verification-notification ....... verification.send
  GET|HEAD  email/verify ........................ verification.notice
  GET|HEAD  email/verify/{id}/{hash} ............ verification.verify
  GET|HEAD  forgot-password password.request › Auth\PasswordResetLin…
  POST      forgot-password password.email › Auth\PasswordResetLinkC…
  GET|HEAD  login login › Auth\AuthenticatedSessionController@create
  POST      login ......... Auth\AuthenticatedSessionController@store
  POST      logout logout › Auth\AuthenticatedSessionController@dest…
  PUT       password password.update › Auth\PasswordController@update
  GET|HEAD  professor/dashboard professor.dashboard › Professor\Dash…
  GET|HEAD  profile ........... profile.edit › ProfileController@edit
  PATCH     profile ....... profile.update › ProfileController@update
  DELETE    profile ..... profile.destroy › ProfileController@destroy
  GET|HEAD  register register › Auth\RegisteredUserController@create
  POST      register ............ Auth\RegisteredUserController@store
  POST      reset-password password.store › Auth\NewPasswordControll…
  GET|HEAD  reset-password/{token} password.reset › Auth\NewPassword…
  GET|HEAD  sanctum/csrf-cookie sanctum.csrf-cookie › Laravel\Sanctu…
  GET|HEAD  verify-email verification.notice › Auth\EmailVerificatio…
  GET|HEAD  verify-email/{id}/{hash} verification.verify › Auth\Veri…

                                                  Showing [29] routes

vagrant@ubuntu2204 laravelAuth $

ルートの定義を変更したので Web サーバを再起動します.

vagrant@ubuntu2204 laravelAuth $ php artisan serve --host=192.168.56.101 --port=8000 ⏎

   INFO  Server running on [http://192.168.56.101:8000].

  Press Ctrl+C to stop the server

管理者のダッシュボードページ /admin/dashboard にアクセスします.

laravel10-2023-auth-103.png

教員のダッシュボードページ /professor/dashboard にアクセスします.

laravel10-2023-auth-104.png

目次に戻る