Laravel 10 入門トップページ


目次

  1. 全体の概要
  2. Laravel によるユーザ認証
  3. ユーザ認証を備えたコメント掲示板の開発
    1. 概要
    2. データベースのマイグレーション
    3. シーダによるコメントデータの登録
    4. モデルとコントローラの生成
    5. ルートの定義と確認
    6. 未検証ユーザの動作検証
    7. コメントの一覧表示
    8. ナビゲーションメニューの追加
    9. コメント一覧の Tailwind CSS によるスタイリング
    10. コメントの詳細ページ
    11. リレーションシップの設定と投稿者名の表示
    12. ページネーションの作成
    13. コメントの投稿
    14. コメントの編集
    15. コメントの削除
    16. ナビゲーションのハイライトを調整
  4. マルチ認証の実現

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

ユーザ認証を備えたコメント掲示板の開発

ルートの定義と確認

新たなルートを定義する前に現時点のルートの定義を一覧で確認します.認証に関するルートを中心に27種類のルートが定義されています.

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  api/user ................................................
  GET|HEAD  confirm-password password.confirm › Auth\ConfirmablePass…
  POST      confirm-password Auth\ConfirmablePasswordController@store
  GET|HEAD  dashboard ..................................... dashboard
  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  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 [27] routes

vagrant@ubuntu2204 laravelAuth $

コメント一覧ページ /comments へのルートを定義します.このとき,コメント一覧ページにはメール検証済みのユーザがログインした状態でなければアクセスできないようにするため,middleware を利用しています.(同時に,/profile ページの middleware では auth しか指定していないので,メール検証が終わっていなくてもプロフィールの閲覧・変更ができることにも注意してください.)

routes/web.php<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CommentController;

/*
|--------------------------------------------------------------------------
| 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');
});

// 'verified' を 'auth' と一緒に利用することでメール認証後にしかログインできなくなる
Route::middleware('auth', 'verified')->group(function () {
    Route::get('/comments', [CommentController::class, 'index'])->name('comments.index');
});

require __DIR__.'/auth.php';

ルートを定義したらもう再び一覧で定義情報を確認します.定義ずみルート数が28になり,/comments へのルートが追加されたことがわかります.

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  api/user ................................................
  GET|HEAD  comments ....... comments.index › CommentController@index
  GET|HEAD  confirm-password password.confirm › Auth\ConfirmablePass…
  POST      confirm-password Auth\ConfirmablePasswordController@store
  GET|HEAD  dashboard ..................................... dashboard
  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  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 [28] routes

vagrant@ubuntu2204 laravelAuth $

任意のユーザ(たとえば user_a)でログインした後,/comments にアクセスするとコメントの一覧を取得できていることがわかります.

laravel10-2023-auth-51.png

目次に戻る