はじめに
最近、Laravelの学習を進める中で、フレームワークの内部構造を理解することの重要性を感じるようになりました。そこで今回は、実際にコードを読みながら、その仕組みを理解していこうと思います。とはいえ、目的を持たずに読もうとすると非効率で終わりが見えないので、毎回読む範囲を決めようと思います。一番最初はLaravelアプリケーションのエントリポイントであるpublic/index.phpを見ていこうと思います。
index.phpとは何か?
Laravelではindex.phpがプログラムの開始点となっています。Webサーバからのリクエストが最初に到達し、アプリケーションの起動処理が始まります。それでは実際にそれぞれの処理を見ていきましょう。
コードの全体像
コードの全体像は以下のようになります。今回はこの中でも大事なところを見ていきます。
index.php
<?php
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/
if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
require $maintenance;
}
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/
require __DIR__.'/../vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$response = $kernel->handle(
$request = Request::capture()
)->send();
$kernel->terminate($request, $response);
各コードの説明
if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
require $maintenance;
}
これはLaravelがメンテナンスモードになっている時にメンテナンス表示をするためのコードです。ここでは指定されているパスでmaintenance.phpが存在するかを確認しています。実際にメンテナンスモードになっていればファイルが作成されています。
require __DIR__.'/../vendor/autoload.php';
これはComposerのオートローダを読みとるコードです。これによって、Laravelアプリケーションで必要なクラスが自動的に読み込まれ、手動でrequire文を記述する必要がなくなります。
$app = require_once __DIR__.'/../bootstrap/app.php';
ここではLaravelのアプリケーションインスタンスを生成しています。これは、アプリケーションの初期設定をしており、Illuminate\Foundation\Applicationクラスのインスタンスを返します。このインスタンスはLaravelのサービスコンテナとして機能し、アプリケーション内のさまざまなサービスや依存関係を管理・解決します。
$kernel = $app->make(Kernel::class);
$response = $kernel->handle(
$request = Request::capture()
)->send();
$kernel->terminate($request, $response);
ここではサービスコンテナを利用して、HTTPカーネルのインスタンスを取得し、現在のリクエストに対する処理をします。handleメソッドは、リクエストを受け取り、適切なレスポンスを生成します。最終的にはリクエスト後のクリーンアップ処理などを行います。
おわりに
実際にコードリーディングをすると今までなんとなくでしていた処理が実際にどのように行われているのかがわかるのでとてもいいですね。引き続き他の部分に関しても続けていきたいと思います。最後まで読んでいただきありがとうございました。もし、間違いなどがあれば指摘してください。
参考記事