expand_less

Request Lifecycle

Lifecycle Overview

First Steps

The public/index.php file serves as the entry point for all requests to a Dreamfork application. Your web server configuration (Apache/Nginx) directs all requests to this file. While the index.php file isn't extensive in code, it serves as the initial step for loading the remainder of the framework.

Within the index.php file, the Composer-generated autoloader definition is loaded. Subsequently, an instance of the Dreamfork application is obtained from bootstrap/app.php. Dreamfork's initial action involves creating an instance of the application / service container.

HTTP / Console Kernels

Once a request arrives, it is directed to the HTTP Kernel, which triggers the bootstrap process of the previously created application instance. After the bootstrap, the Kernel takes charge of handling the request by passing it to the Router in the handle method. The handle method is a signature method for the Kernel and is straightforward in its operation. It takes a Request as input and returns a Response, which is subsequently sent to the user.

The framework also incorporates a Console Kernel, which is invoked solely from the terminal command line and is not accessible via HTTP. It is utilized by dfork, a program similar to Laravel's Artisan. Dfork facilitates the execution of commands that streamline work with the framework, such as maintenance mode, symbolic linking of storage, or generating skeletons for models/controllers.

Routing

Routing is a crucial aspect of the framework, wherein an incoming request handled by the Kernel is directed to the Router, responsible for further processing. The Router leverages the provider called RouterServiceProvider, which loads available routes into the application and determines the utilized interface (web or api).

Upon receiving a request, the router checks the available routes to determine where the request should be directed. The router then dispatches the request to the appropriate route and controller, from which it subsequently receives a response. This response is transformed by the router into either a Symfony Response or Symfony JsonResponse, depending on the utilized interface and the data the response is meant to send. The prepared response is then returned to the Kernel.

Finishing Up

The prepared response is sent back to the HTTP Kernel, specifically its handle method. This method returns a response object, and in the index.php file, the send method is invoked on the returned response. The send method facilitates the transmission of the response content to the user's web browser.