فهرست ها

Request Lifecycle

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.

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

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.

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.

HTTP / Console Kernels

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.

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, 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.

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, determining if the application is in maintenance mode, verifying the CSRF token, and more. We'll talk more about these soon.

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

One of the most important kernel bootstrapping actions is loading the service 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 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.

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.

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

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.

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.

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.

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

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!

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.

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.

چرخه حیات درخواست (Request Lifecycle)

مقدمه

وقتی در «دنیای واقعی» از هر ابزاری استفاده می‌کنید، اگر بدانید آن ابزار چگونه کار می‌کند، اعتمادبه‌نفس بیشتری خواهید داشت. توسعه برنامه (Application development) نیز از این قاعده مستثنی نیست. وقتی درک کنید ابزارهای توسعه شما چگونه عمل می‌کنند، هنگام استفاده از آن‌ها احساس راحتی و اطمینان بیشتری خواهید داشت.

هدف این سند ارائه یک دید کلی، خوب و سطح بالا (high-level) از نحوه کارکرد فریم‌ورک لاراول (Laravel framework) است. با شناخت بهتر کل این فریم‌ورک، همه‌چیز کمتر حالت «جادویی» به خود می‌گیرد و شما با اعتمادبه‌نفس بیشتری برنامه‌های خود را خواهید ساخت. اگر در ابتدا همه اصطلاحات را متوجه نشدید، ناامید نشوید! فقط سعی کنید یک درک اولیه از آنچه رخ می‌دهد به دست آورید؛ با کاوش در سایر بخش‌های مستندات، دانش شما رشد خواهد کرد.

مرور کلی چرخه حیات

گام‌های نخستین

نقطه ورود (entry point) برای تمام درخواست‌ها به یک برنامه لاراولی، فایل public/index.php است. تمام درخواست‌ها توسط پیکربندی وب‌سرور (web server) شما (آپاچی / اِنجین‌اکس) به سمت این فایل هدایت می‌شوند. فایل index.php حاوی کد زیادی نیست؛ بلکه بیشتر یک نقطه شروع برای بارگذاری مابقی فریم‌ورک است.

فایل index.php تعاریف خودکارگذار (autoloader) تولیدشده توسط کامپوزر (Composer) را بارگذاری کرده و سپس یک نمونه (instance) از برنامه لاراول را از مسیر bootstrap/app.php بازیابی می‌کند. اولین اقدامی که توسط خود لاراول صورت می‌گیرد، ایجاد یک نمونه از برنامه / کانتینر سرویس (service container) است.

هسته‌های HTTP و کنسول

در مرحله بعد، بسته به نوع درخواستی که وارد برنامه می‌شود، درخواست ورودی (incoming request) با استفاده از متدهای handleRequest یا handleCommand در نمونه‌ی برنامه، به یکی از دو هسته (kernel) یعنی هسته HTTP یا هسته کنسول (console kernel) فرستاده می‌شود. این دو هسته به عنوان مکان مرکزی عمل می‌کنند که تمام درخواست‌ها از میان آن‌ها جریان می‌یابند. در حال حاضر، بیایید فقط روی هسته HTTP تمرکز کنیم که نمونه‌ای از کلاس Illuminate\Foundation\Http\Kernel است.

هسته HTTP آرایه‌ای از راه‌اندازها (bootstrappers) را تعریف می‌کند که قبل از اجرای درخواست اجرا خواهند شد. این راه‌اندازها کارهایی مانند پیکربندی مدیریت خطا (error handling)، پیکربندی ثبت گزارش‌ها (logging)، تشخیص محیط برنامه (application environment) و سایر کارهایی را انجام می‌دهند که باید قبل از رسیدگی واقعی به درخواست انجام شوند. معمولاً این کلاس‌ها به پیکربندی‌های داخلی لاراول رسیدگی می‌کنند و نیازی نیست شما نگران آن‌ها باشید.

هسته HTTP همچنین مسئول عبور دادن درخواست از میان پشته واسط‌های کاربری / میان‌افزارها (middleware stack) برنامه است. این میان‌افزارها کارهایی مثل خواندن و نوشتن نشست HTTP (HTTP session)، تعیین اینکه آیا برنامه در حالت تعمیر و نگهداری (maintenance mode) قرار دارد یا خیر، تأیید توکن CSRF (CSRF token) و موارد دیگر را مدیریت می‌کنند. به‌زودی بیشتر درباره این‌ها صحبت خواهیم کرد.

امضای متد (method signature) برای متد handle در هسته HTTP بسیار ساده است: یک درخواست (Request) دریافت می‌کند و یک پاسخ (Response) برمی‌گرداند. هسته را مانند یک جعبه سیاه بزرگ ، اونم بهت یه شات اسپرسو میده (خروجی). اینکه اون تو چه اتفاقی میفته و چطوری آب جوش میاد، برات مهم نیست. هسته لاراول هم همینطوریه؛ بهش درخواست میدی، پاسخ میگیری!) تصور کنید که کل برنامه شما را نشان می‌دهد؛ به آن درخواست‌های HTTP بدهید تا به شما پاسخ‌های HTTP برگرداند.

ارائه‌دهندگان سرویس

یکی از مهم‌ترین اقدامات راه‌اندازی هسته، بارگذاری ارائه‌دهندگان سرویس (service providers) برای برنامه شماست. ارائه‌دهندگان سرویس مسئول راه‌اندازی (bootstrapping) تمام کامپوننت‌های مختلف فریم‌ورک مانند پایگاه داده، صف (queue)، اعتبارسنجی (validation) و مسیریابی (routing) هستند.

لاراول در میان این لیست از ارائه‌دهندگان پیمایش کرده و از هرکدام یک نمونه می‌سازد. پس از نمونه‌سازی، متد register روی همه آن‌ها فراخوانی می‌شود. سپس، زمانی که همه ارائه‌دهندگان ثبت شدند، متد boot روی هر ارائه‌دهنده فراخوانی خواهد شد. دلیل این ترتیب آن است که ارائه‌دهندگان سرویس بتوانند به ثبت و در دسترس بودن تمام وابستگی‌های کانتینر (container bindings) در زمان اجرای متد boot خود متکی باشند.

به طور اساسی، هر قابلیت بزرگی که توسط لاراول ارائه می‌شود، توسط یک ارائه‌دهنده سرویس راه‌اندازی و پیکربندی می‌گردد. از آنجا که آن‌ها قابلیت‌های بسیار زیادی از فریم‌ورک را راه‌اندازی و پیکربندی می‌کنند، ارائه‌دهندگان سرویس مهم‌ترین جنبه از کل فرآیند راه‌اندازی لاراول به شمار می‌روند.

در حالی که فریم‌ورک در درون خود از ده‌ها ارائه‌دهنده سرویس استفاده می‌کند، شما نیز این امکان را دارید که ارائه‌دهندگان خود را بسازید. می‌توانید لیستی از ارائه‌دهندگان سرویس تعریف‌شده توسط کاربر یا شخص ثالث (third-party) را که برنامه‌تان از آن‌ها استفاده می‌کند، در فایل bootstrap/providers.php پیدا کنید.

مسیریابی

هنگامی که برنامه راه‌اندازی شد و تمام ارائه‌دهندگان سرویس ثبت شدند، درخواست (Request) جهت ارسال، به مسیریاب (router) تحویل داده می‌شود. مسیریاب درخواست را به یک مسیر (route) یا متد کنترلر (controller method) ارسال (dispatch) می‌کند و همچنین میان‌افزارهای اختصاصی آن مسیر را اجرا می‌نماید.

میان‌افزارها (Middleware) مکانیزم راحتی را برای فیلتر کردن یا بررسی درخواست‌های HTTP ورودی به برنامه شما فراهم می‌کنند. به عنوان مثال، لاراول شامل میان‌افزاری است که بررسی می‌کند آیا کاربر برنامه شما احراز هویت (authenticated) شده است یا خیر. اگر کاربر احراز هویت نشده باشد، میان‌افزار او را به صفحه ورود (login screen) هدایت (redirect) می‌کند. با این حال، اگر کاربر احراز هویت شده باشد، میان‌افزار اجازه می‌دهد تا درخواست بیشتر به درون برنامه پیش برود. برخی از میان‌افزارها به تمام مسیرهای برنامه اختصاص داده می‌شوند (مانند PreventRequestsDuringMaintenance)، در حالی که برخی دیگر تنها به مسیرها یا گروه‌های مسیر خاصی اختصاص می‌یابند. با مطالعه مستندات کامل میان‌افزار می‌توانید اطلاعات بیشتری در این باره کسب کنید.

اگر درخواست از تمام میان‌افزارهای اختصاص‌یافته به مسیرِ مطابقت‌یافته عبور کند، مسیر یا متد کنترلر اجرا می‌شود و پاسخ برگشتی توسط متد کنترلر یا مسیر، مجدداً از طریق زنجیره میان‌افزارهای آن مسیر به عقب بازگردانده می‌شود.

اتمام فرآیند

هنگامی که مسیر یا متد کنترلر یک پاسخ را برمی‌گرداند، این پاسخ مسیر خود را به سمت بیرون از طریق میان‌افزارهای مسیر طی می‌کند و این فرصت را به برنامه می‌دهد تا پاسخ خروجی (outgoing response) را تغییر داده یا بررسی کند.

در نهایت، پس از اینکه پاسخ از میان‌افزارها عبور کرد، متد handle در هسته HTTP، شیء پاسخ (response object) را به متد handleRequest در نمونه‌ی برنامه بازمی‌گرداند و این متد، متد send را روی پاسخ دریافتی فراخوانی می‌کند. متد send محتوای پاسخ را به مرورگر وب کاربر ارسال می‌کند. ما اکنون سفر خود را در کل چرخه حیات درخواست لاراول به پایان رسانده‌ایم!

تمرکز روی ارائه‌دهندگان سرویس

ارائه‌دهندگان سرویس واقعاً کلید اصلی راه‌اندازی یک برنامه لاراولی هستند. نمونه برنامه ایجاد می‌شود، ارائه‌دهندگان سرویس ثبت می‌شوند و درخواست به برنامه راه‌اندازی شده تحویل داده می‌شود. به همین سادگی!

داشتن درک درستی از نحوه ساخت و راه‌اندازی یک برنامه لاراول از طریق ارائه‌دهندگان سرویس بسیار ارزشمند است. ارائه‌دهندگان سرویس تعریف‌شده توسط کاربر در برنامه شما، در دایرکتوری app/Providers ذخیره می‌شوند.

به صورت پیش‌فرض، کلاس AppServiceProvider تا حدودی خالی است. این ارائه‌دهنده مکانی عالی برای اضافه کردن کدهای راه‌اندازی برنامه‌ی خودتان و اتصال‌های کانتینر سرویس (service container bindings) است. برای برنامه‌های بزرگ، ممکن است بخواهید چندین ارائه‌دهنده سرویس ایجاد کنید که هرکدام دارای راه‌اندازی‌های جزئی‌تر و دقیق‌تری (granular) برای سرویس‌های خاصِ مورد استفاده در برنامه‌تان باشند.