0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravel ルーティングとWebページが表示される流れ

Posted at

#Webページが表示される流れ
スクリーンショット 2021-12-15 21.39.46.png

#####1.public/index.phpにまずアクセスが集まる。

require __DIR__ . '/../vendor/autoload.php';  //クラスを全て読み込む

$app = require_once __DIR__ . '/../bootstrap/app.php'; //サービスコンテナなど、さまざまな機能を読み込む

#####2.ミドルウェアで認証しているかどうかの確認が入る
config/app.php

'providers' => [
        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        ....
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        ....
    ],

 'aliases' => [
        'Auth' => Illuminate\Support\Facades\Auth::class,
        ....
    ],

#####3.ルーティングファイルに渡る
このなかで、どのURLにアクセスするかを判断する。
ControllerからViewファイルにわたり、Viewファイルが表示される。
DBのデータを扱う場合はModelを経由する。

routes/web.php

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


//ログインしてるかどうかの確認、してたらdashboardを表示する 
Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');

require __DIR__ . '/auth.php';   //呼び出し

routes/auth.php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\ConfirmablePasswordController;
....
use Illuminate\Support\Facades\Route;  //Routeを読み込む

//'/指定したURL',  []でコントローラ名、メソッド名
Route::get('/register', [RegisteredUserController::class, 'create'])
                ->middleware('guest')  // middleware guestだったら
                ->name('register');   //名前付きルート

Route::post('/register', [RegisteredUserController::class, 'store'])
                ->middleware('guest');

Route::get('/login', [AuthenticatedSessionController::class, 'create'])
                ->middleware('guest')
                ->name('login');

Route::post('/login', [AuthenticatedSessionController::class, 'store'])
                ->middleware('guest');

Route::get('/forgot-password', [PasswordResetLinkController::class, 'create'])
                ->middleware('guest')
                ->name('password.request');

Route::post('/forgot-password', [PasswordResetLinkController::class, 'store'])
                ->middleware('guest')
                ->name('password.email');

Route::get('/reset-password/{token}', [NewPasswordController::class, 'create'])
                ->middleware('guest')
                ->name('password.reset');

Route::post('/reset-password', [NewPasswordController::class, 'store'])
                ->middleware('guest')
                ->name('password.update');

Route::get('/verify-email', [EmailVerificationPromptController::class, '__invoke'])
                ->middleware('auth')
                ->name('verification.notice');

Route::get('/verify-email/{id}/{hash}', [VerifyEmailController::class, '__invoke'])
                ->middleware(['auth', 'signed', 'throttle:6,1'])
                ->name('verification.verify');

Route::post('/email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
                ->middleware(['auth', 'throttle:6,1'])
                ->name('verification.send');

Route::get('/confirm-password', [ConfirmablePasswordController::class, 'show'])
                ->middleware('auth')
                ->name('password.confirm');

Route::post('/confirm-password', [ConfirmablePasswordController::class, 'store'])
                ->middleware('auth');

Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])
                ->middleware('auth')
                ->name('logout');

https://readouble.com/laravel/8.x/ja/authentication.html
の、「認証済みユーザー取得」を参照。

ターミナルでもルーティングのリストを確認できる。
*ターミナル上だと読みにくいのでtxtファイルにして吐き出す

$  php artisan route:list > route.txt

例えばloginの場合は、
Route::get('/login', [AuthenticatedSessionController::class, 'create'])
->middleware('guest')
->name('login');
となっているので、
App\Http\Controllers\Auth\AuthenticatedSessionController
こちらを確認すると、

    public function create()
    {
        return view('auth.login');   //auth.loginに遷移
    }

この流れをindex.php→ミドルウェア→ルーティングファイル→Controller→Viewを通して毎度行われる。

_________________________________________

参照:https://www.udemy.com/course/laravel-multi-ec/

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?