Readouble

Laravel 5.4 リクエストのライフサイクル

イントロダクション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 Things

アプリケーションに対するリクエストは、全て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 simply a starting point for loading the rest of the framework.

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

HTTP/ConsoleカーネルHTTP / Console Kernels

次にアプリケーション起動のきっかけにより、送信されてきたリクエストをHTTPカーネルかコンソールカーネルのどちらかに送ります。これらのカーネルは、全リクエストフローの中心に位置し動作します。ここでは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(起動コード)の配列を定義しています。これらの起動コードはエラー処理、ログ設定、アプリケーション動作環境の決定、そのほか実際にリクエストが処理される前に行う必要のあるタスクです。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.

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.

HTTPカーネルのhandleメソッドの使い方はとてもシンプルで、Requestを受け取り、Responseをリターンします。カーネルをアプリケーション全体を表す大きなブラックボックスだと考えてください。HTTPリクエストを流し込み、HTTPレスポンスが返ってきます。The method signature for the HTTP kernel's handle method is quite simple: receive a Request and return 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

カーネル初期起動時の重要な処理の一つは、アプリケーションのサービスプロバイダをロードすることです。アプリケーションの全サービスプロバイダは、config/app.phpファイルのproviders配列で設定されています。最初に全プロバイダのregisterメソッドが呼び出され、その後に登録されている全プロバイダのbootメソッドが呼び出されます。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. First, the register method will be called on all providers, then, once all providers have been registered, the boot method will be called.

サービスプロバイダはデータベース、キュー、バリデーション、ルーティングなど、フレームワークの様々なコンポーネントの初期起動処理に責任をもちます。フレームワークにより提供されている全機能を初期化し、設定するものですから、サービスプロバイダはLaravelの初期処理の過程全体で一番重要な機能です。Service providers are responsible for bootstrapping all of the framework's various components, such as the database, queue, validation, and routing components. Since they bootstrap and configure every feature offered by the framework, service providers are the most important aspect of the entire Laravel bootstrap process.

リクエストのディスパッチDispatch Request

アプリケーションの初期処理が済み、全サービスプロバイダが登録されたら、ディスパッチ(実行制御の移行)するためにルーターへ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.

サービスプロバイダの精査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. Of course, 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. Of course, for large applications, you may wish to create several service providers, each with a more granular type of bootstrapping.

章選択

公式パッケージ

設定

明暗テーマ
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に保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作