イントロダクション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
次に、アプリケーションインスタンスのhandleRequest
メソッドまたはhandleCommand
メソッドを使い、アプリケーションが受け取るリクエストのタイプに応じて、HTTPカーネルまたはコンソールカーネルへリクエストが送られます。これら2つのカーネルは、すべてのリクエストが流れる中心的な場所として機能します。今回は、Illuminate\Foundation\Http\Kernel
インスタンスであるHTTPカーネルへ注目してみましょう。Next, the incoming request is
sent to either the HTTP kernel or the console
kernel, using the handleRequest
or
handleCommand
methods of the
application instance, depending on the type of
request entering the application. These two kernels
serve as the central location through which all
requests flow. For now, let's just focus on the HTTP
kernel, which is an instance of
Illuminate\Foundation\Http\Kernel
.
HTTPカーネルはリクエストを処理する前に実行する、
初期起動処理(bootstrappers
)の配列を定義しています。これらの初期起動処理は、エラー処理の設定、ログの設定、アプリケーション環境の検出など、リクエストを実際に処理する前に行う必要のあるタスクを実行します。通常、これらのクラスは、あなたが心配する必要のないLaravel内部の設定を処理します。The HTTP kernel 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セッションの読み書き、アプリケーションがメンテナンスモードかどうかの判断、CSRFトークンの検証などを処理します。これらについてはこのあとに、詳しく説明します。The HTTP kernel is also responsible for passing the request through the application's middleware stack. 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つは、アプリケーションのサービスプロバイダをロードすることです。サービスプロバイダは、データベース、キュー、バリデーション、ルーティングコンポーネントなど、フレームワークのさまざまなコンポーネントをすべて初期起動処理する役割を担っています。One of the most important kernel bootstrapping actions is loading the service providers[/docs/{{version}}/providers] for your application. Service providers are responsible for bootstrapping all of the framework's various components, such as the database, queue, validation, and routing components.
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初期起動プロセス全体の最も重要な側面です。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.
フレームワークは内部的に何十ものサービスプロバイダを使いますが、あなた自身のものを作るオプションもあります。bootstrap/providers.php
ファイルで、アプリケーションが使用しているユーザー定義またはサードパーティーのサービス・プロバイダのリストを見つけることができます。While the framework internally
uses dozens of service providers, you also have the
option to create your own. You can find a list of
the user-defined or third-party service providers
that your application is using in the
bootstrap/providers.php
file.
ルートRouting
アプリケーションが初期起動され、すべてのサービスプロバイダが登録されると、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には、アプリケーションのユーザーが認証済みかを確認するミドルウェアがあります。ユーザーが認証されていない場合、ミドルウェアはユーザーをログイン画面にリダイレクトします。しかし、ユーザーが認証済であれば、ミドルウェアはリクエストをアプリケーションへ進めます。ミドルウェアの中には
PreventRequestsDuringMaintenance
のように、アプリケーション内のすべてのルートに割り当てられるものもあれば、特定のルートやルートグループにのみ割り当てられるものもあります。ミドルウェアの詳細については、ミドルウェアのドキュメントを読んでください。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
PreventRequestsDuringMaintenance
, 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
メソッドはレスポンスオブジェクトをアプリケーションインスタンスのhandleRequest
へ返し、このメソッドは返されたレスポンスに対してsend
メソッドを呼び出します。send
メソッドはレスポンスの内容をユーザーのウェブブラウザに送信します。これで、Laravelのリクエストライフサイクル全体の旅が終わりました!Finally, once the response
travels back through the middleware, the HTTP
kernel's handle
method returns the
response object to the handleRequest
of
the application instance, and this method calls the
send
method on the returned response.
The send
method sends the response
content to the user's web browser. We've now
completed 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 user-defined 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.