Readouble

Laravel 8.x リクエストのライフサイクル

イントロダクションIntroduction

「現実の世界」でツールを使用するとき、そのツールがどのように機能するかを理解していれば、より自信を感じられます。アプリケーション開発も例外ではありません。開発ツールがどのように機能するかを理解すると、それらをより快適に、自信を持って使用できるようになります。When using any tool in the "real world", you feel more confident if you understand how that tool works. Application development is no different. When you understand how your development tools function, you feel more comfortable and confident using them.

このドキュメントの目的は、Laravelフレームワークがどのように機能するかについての概要を説明することです。フレームワーク全体をよりよく理解することで、すべてが「魔法」ではなくなり、アプリケーションの構築に自信が持てるようになります。すべての用語をすぐに理解できない場合でも、戸惑う必要はありません。何が起こっているのかという基本的な部分を把握しておけば、ドキュメントの他のセクションを探索するにつれて知識が深まります。The goal of this document is to give you a good, high-level overview of how the Laravel framework works. By getting to know the overall framework better, everything feels less "magical" and you will be more confident building your applications. If you don't understand all of the terms right away, don't lose heart! Just try to get a basic grasp of what is going on, and your knowledge will grow as you explore other sections of the documentation.

ライフサイクル概論Lifecycle Overview

最初のステップFirst Steps

Laravelアプリケーションへのすべてのリクエストのエントリポイントはpublic/index.phpファイルです。すべてのリクエストは、Webサーバ(Apache/Nginx)設定によってこのファイルに送信されます。index.phpファイルには多くのコードが含まれていません。むしろ、フレームワークの残りの部分をロードするための開始点と言えるでしょう。The entry point for all requests to a Laravel application is the public/index.php file. All requests are directed to this file by your web server (Apache / Nginx) configuration. The index.php file doesn't contain much code. Rather, it is a starting point for loading the rest of the framework.

index.phpファイルは、Composerで生成したオートローダー定義をロードし、Laravelアプリケーションのインスタンスをbootstrap/app.phpから取得します。Laravel自体が取る最初のアクションは、アプリケーション/サービスコンテナのインスタンスを作成することです。The index.php file loads the Composer generated autoloader definition, and then retrieves an instance of the Laravel application from bootstrap/app.php. The first action taken by Laravel itself is to create an instance of the application / service container[/docs/{{version}}/container].

HTTP/コンソールカーネルHTTP / Console Kernels

次に、アプリケーションへ入ってきたリクエストのタイプに応じて、受信リクエストをHTTPカーネルかコンソールカーネルのいずれかに送信します。これらの2つのカーネルは、すべてのリクエストが通る中心的な場所として機能します。ここでは、app/Http/Kernel.phpにあるHTTPカーネルに注目しましょう。Next, the incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application. These two kernels serve as the central location that all requests flow through. For now, let's just focus on the HTTP kernel, which is located in app/Http/Kernel.php.

HTTPカーネルはIlluminate\Foundation\Http\Kernelクラスを拡張します。このクラスはリクエストが実行される前に実行されるbootstrappersの配列を定義しています。これらのブートストラッパーは、エラー処理の構成、ログの構成、アプリケーション環境の検出、およびリクエストが実際に処理される前に実行する必要のあるその他のタスクを実行します。通常、これらのクラスは、皆さんが関わる必要のないLaravelの内部設定を処理します。The HTTP kernel extends the Illuminate\Foundation\Http\Kernel class, which defines an array of bootstrappers that will be run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment[/docs/{{version}}/configuration#environment-configuration], and perform other tasks that need to be done before the request is actually handled. Typically, these classes handle internal Laravel configuration that you do not need to worry about.

HTTPカーネルは、すべてのリクエストがアプリケーションによって処理される前に通過する必要があるHTTPミドルウェアのリストも定義します。これらのミドルウェアは、HTTPセッションの読み取りと書き込み、アプリケーションがメンテナンスモードであるかどうかの判断、CSRFトークンの確認などを処理します。これらはこの後すぐに詳しく説明します。The HTTP kernel also defines a list of HTTP middleware[/docs/{{version}}/middleware] that all requests must pass through before being handled by the application. These middleware handle reading and writing the HTTP session[/docs/{{version}}/session], determining if the application is in maintenance mode, verifying the CSRF token[/docs/{{version}}/csrf], and more. We'll talk more about these soon.

HTTPカーネルのhandleメソッドの引数は非常に単純です。Requestを受け取り、Responseを返します。カーネルをアプリケーション全体を表す大きなブラックボックスであると考えてください。HTTPリクエストを取り込み、HTTPレスポンスを返します。The method signature for the HTTP kernel's handle method is quite simple: it receives a Request and returns a Response. Think of the kernel as being a big black box that represents your entire application. Feed it HTTP requests and it will return HTTP responses.

サービスプロバイダService Providers

最も重要なカーネル初期起動処理アクションの1つは、アプリケーションのサービスプロバイダをロードすることです。アプリケーションのすべてのサービスプロバイダは、config/app.php構成ファイルのproviders配列で構成されます。One of the most important kernel bootstrapping actions is loading the service providers[/docs/{{version}}/providers] for your application. All of the service providers for the application are configured in the config/app.php configuration file's providers array.

Laravelはこのプロバイダのリストを繰り返し処理し、それぞれをインスタンス化します。プロバイダをインスタンス化した後、すべてのプロバイダのregisterメソッドを呼び出します。次に、すべてのプロバイダを登録し、各プロバイダでbootメソッドを呼び出します。これは、bootメソッドが実行されるまでに、すべてのコンテナバインディングが登録され、利用可能になっていることに、サービスプロバイダが依存しているからです。Laravel will iterate through this list of providers and instantiate each of them. After instantiating the providers, the register method will be called on all of the providers. Then, once all of the providers have been registered, the boot method will be called on each provider. This is so service providers may depend on every container binding being registered and available by the time their boot method is executed.

サービスプロバイダは、データベース、キュー、バリデーション、ルーティングコンポーネントなど、フレームワークのさまざまなコンポーネントすべてを初期起動する責務を持っています。Laravelが提供する基本的なすべての主機能は、サービスプロバイダによって初期起動および設定されます。フレームワークによって提供される非常に多くの機能を初期起動し設定するため、サービスプロバイダはLaravel初期起動プロセス全体の最も重要な側面です。Service providers are responsible for bootstrapping all of the framework's various components, such as the database, queue, validation, and routing components. Essentially every major feature offered by Laravel is bootstrapped and configured by a service provider. Since they bootstrap and configure so many features offered by the framework, service providers are the most important aspect of the entire Laravel bootstrap process.

ルートRouting

アプリケーションで最も重要なサービスプロバイダの1つは、App\Providers\RouteServiceProviderです。このサービスプロバイダは、アプリケーションのroutesディレクトリ内に含まれるルートファイルをロードします。どうぞRouteServiceProviderコードを開いて解析し、それがどのように機能するかを確認してください!One of the most important service providers in your application is the App\Providers\RouteServiceProvider. This service provider loads the route files contained within your application's routes directory. Go ahead, crack open the RouteServiceProvider code and take a look at how it works!

アプリケーションが初期起動され、すべてのサービスプロバイダが登録されると、Requestがルータに渡されてディスパッチされます。ルータは、ルートまたはコントローラへリクエストをディスパッチし、ルート固有のミドルウェアを実行します。Once the application has been bootstrapped and all service providers have been registered, the Request will be handed off to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware.

ミドルウェアは、アプリケーションに入るHTTPリクエストをフィルタリングまたは検査するための便利なメカニズムを提供しています。たとえば、Laravelには、アプリケーションのユーザーが認証されているかを確認するミドルウェアが含まれています。ユーザーが認証されていない場合、このミドルウェアはユーザーをログイン画面にリダイレクトします。そしてユーザーが認証されている場合、ミドルウェアはリクエストをアプリケーションへ進めることを許可します。HTTPカーネルの$middlewareプロパティで定義されているもののように、アプリケーション内のすべてのルートで適用されるミドルウェアもあれば、特定のルートまたはルートグループにのみ割り当てられるミドルウェアもあります。ミドルウェアの詳細については、完全なミドルウェアのドキュメントを参照してください。Middleware provide a convenient mechanism for filtering or examining HTTP requests entering your application. For example, Laravel includes a middleware that verifies if the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application. Some middleware are assigned to all routes within the application, like those defined in the $middleware property of your HTTP kernel, while some are only assigned to specific routes or route groups. You can learn more about middleware by reading the complete middleware documentation[/docs/{{version}}/middleware].

リクエストが一致したルートへ割り当てられたすべてのミドルウェアをパスした場合は、ルートまたはコントローラメソッドが実行され、ルートまたはコントローラメソッドが返すレスポンスがルートのミドルウェアチェーンを介して返送されます。If the request passes through all of the matched route's assigned middleware, the route or controller method will be executed and the response returned by the route or controller method will be sent back through the route's chain of middleware.

最後Finishing Up

ルートまたはコントローラメソッドがレスポンスを返すと、レスポンスはルートのミドルウェアを介して外側に戻り、アプリケーションに送信レスポンスを変更または検査する機会を与えます。Once the route or controller method returns a response, the response will travel back outward through the route's middleware, giving the application a chance to modify or examine the outgoing response.

最後に、レスポンスがミドルウェアを経由して戻ると、HTTPカーネルのhandleメソッドがレスポンスオブジェクトを返し、index.phpファイルが返ってきたレスポンスに対してsendメソッドを呼び出します。sendメソッドは、レスポンスコンテンツをユーザーのWebブラウザに送信します。Laravelリクエストのライフサイクル全体の旅を終えました!Finally, once the response travels back through the middleware, the HTTP kernel's handle method returns the response object and the index.php file calls the send method on the returned response. The send method sends the response content to the user's web browser. We've finished our journey through the entire Laravel request lifecycle!

サービスプロバイダに注目Focus On Service Providers

サービスプロバイダは、Laravelアプリケーションを初期起動するための真の鍵です。アプリケーションインスタンスが作成され、サービスプロバイダが登録され、リクエストが初期起動を終えたアプリケーションに渡されます。とても簡単です!Service providers are truly the key to bootstrapping a Laravel application. The application instance is created, the service providers are registered, and the request is handed to the bootstrapped application. It's really that simple!

Laravelアプリケーションがどのように構築され、サービスプロバイダを介して初期起動されるかをしっかりと把握することは非常に価値があります。アプリケーションのデフォルトのサービスプロバイダは、app/Providersディレクトリに保存されます。Having a firm grasp of how a Laravel application is built and bootstrapped via service providers is very valuable. Your application's default service providers are stored in the app/Providers directory.

デフォルトのAppServiceProviderはほとんど空です。このプロバイダは、アプリケーション独自の初期起動処理およびサービスコンテナ結合を追加するのに最適な場所です。大規模なアプリケーションの場合、アプリケーションで使用する特定のサービスに対して、それぞれがよりきめ細かい初期処理を備えた複数のサービスプロバイダを作成することをお勧めします。By default, the AppServiceProvider is fairly empty. This provider is a great place to add your application's own bootstrapping and service container bindings. For large applications, you may wish to create several service providers, each with more granular bootstrapping for specific services used by your application.

章選択

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

Pagination和文
ペジネーション
ペギネーション
ページネーション
ページ付け
Scaffold和文
スカフォールド
スキャフォールド
型枠生成
本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作