Application initialization and bootstrap
Overview of bootstrapping
To run the Magento application, the following actions are implemented in index.php:
- Include app/bootstrap.php which performs essential initialization routines, such as error handling, initializing the autoloader, setting profiling options, setting the default timezone, and so on.
- Create an instance of \Magento\Framework\App\Bootstrap.
- Create a Magento application instance (\Magento\Framework\AppInterface).
- Run Magento
Bootstrap run logic
The bootstrap object uses the following algorithm to run the Magento application:
- Initializes the error handler.
- Creates the object manager and basic shared services that are used everywhere and are affected by the environment. The environment parameters are injected properly into these objects.
- Asserts that maintenance mode is not enabled; otherwise, terminates.
- Asserts that the Magento application is installed; otherwise, terminates.
-
Starts the Magento application.
Any uncaught exception during application launch is automatically passed back to Magento in the
catchException()
method which you can use to handle the exception. The latter must return eithertrue
orfalse
:- If
true
: Magento handled exception successfully. No need to do anything else. - If
false
(or any other empty result): Magento did not handle the exception. The bootstrap object performs the default exception handling subroutine.
- If
- Sends the response provided by the application object.
The assertions that the Magento application is installed and not in maintenance mode is the default behavior of the \Magento\Framework\App\Bootstrap
class. You can modify it using an entry point script when creating the bootstrap object.
Sample entry point script that modifies the bootstrap object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Http;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = true; // default false
$params[Bootstrap::PARAM_REQUIRE_IS_INSTALLED] = false; // default true
$bootstrap = Bootstrap::create(BP, $params);
/** @var Http $app */
$app = $bootstrap->createApplication(Http::class);
$bootstrap->run($app);
Default exception handling
The bootstrap object specifies how the Magento application handles uncaught exceptions as follows:
- In developer mode, displays the exception as-is.
- In any other mode, attempts to log exception and display a generic error message.
- Terminates Magento with error code
1
Entry point applications
We have the following entry point applications (that is, applications defined by Magento that are used by the web server as a directory index):
HTTP entry point
\Magento\Framework\App\Http operates as follows:
- Determines the application area.
- Starts the front controller and routing systems in order to find and execute a controller action.
- Uses an HTTP response object to return result obtained from the controller action.
-
Error handling (in the following priority order):
- If you’re using developer mode:
- If the Magento application is not installed, display an error.
- If the Magento application is installed, display an error and HTTP status code 500 (Internal Server Error).
- If the Magento application is in maintenance mode, display a user-friendly “Service Unavailable” landing page with HTTP status code 503 (Service Temporary Unavailable).
- If the Magento application is not installed, display an error.
- If the session is invalid, redirect to the home page.
- If there is any other application initialization error, display a user-friendly “Page Not Found” page with HTTP status code 404 (Not Found).
- On any other error, display a user-friendly “Service Unavailable” page with HTTP response 503 and generate an error report and display its ID on the page.
- If you’re using developer mode:
Static resource entry point
\Magento\Framework\App\StaticResource is an application for retrieving static resources (for example, CSS, JavaScript, and images). It postpones any actions with a static resource until the resource is requested.
The entry point for static view files is not used in production mode to avoid potential exploits on the server. In production mode, the Magento application expects that all necessary resources already exist in the <your Magento install dir>/pub/static
directory.
In default or developer mode, a request for a non-existent static resource is redirected to the static entry point according to the rewrite rules specified by the appropriate .htaccess
.
When the request is redirected to the entry point, the Magento application parses the requested URL based on retrieved parameters and finds the requested resource.
- In developer mode, the content of the file is returned so that every time the resource is requested, the returned content is up to date.
-
In default mode, the retrieved resource is published so it is accessible by the previously requested URL.
All future requests for the static resource are processed by the server the same as static files; that is, without involving the entry point. If it’s necessary to synchronize published files with original ones, the
pub/static
directory should be removed; as a result, files are automatically republished with the next request.
Media resource entry point
Magento\MediaStorage\App\Media retrieves media resources (that is, any files uploaded to media storage) from the database. It is used whenever the database is configured as a media storage.
\Magento\Core\App\Media
attempts to find the media file in the configured database storage and write it into the pub/static
directory, then return its contents. On error, it returns an HTTP 404 (Not Found) status code in the header with no contents.
Related topics
This topic discussed the basics of Magento application initialization and bootstrapping. To find out how to set bootstrap environment variables, see one of the following topics: