Overview of initialization and bootstrap

To run the Commerce application, the following actions are implemented in pub/index.php:

Bootstrap run logic

The bootstrap object uses the following algorithm to run the Commerce application:

  1. Initializes the error handler.

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

  3. Asserts that maintenance mode is not enabled; otherwise, terminates.

  4. Asserts that the Commerce application is installed; otherwise, terminates.

  5. Starts the Commerce application.

    Any uncaught exception during application launch is automatically passed back to Commerce in the catchException() method which you can use to handle the exception. The latter must return either true or false:

    • If true: Commerce handled exception successfully. No need to do anything else.
    • If false: (or any other empty result) Commerce did not handle the exception. The bootstrap object performs the default exception-handling subroutine.
  6. Sends the response provided by the application object.

    note info
    The assertions that the Commerce 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:

    code language-php
    use Magento\Framework\App\Bootstrap;
    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 \Magento\Framework\App\Http $app */
    $app = $bootstrap->createApplication('Magento\Framework\App\Http');

Default exception handling

The bootstrap object specifies how the Commerce 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 Commerce with error code 1

Entry point applications

We have the following entry point applications (that is, applications defined by Commerce that are used by the web server as a directory index):

HTTP entry point

\Magento\Framework\App\Http operates as follows:

  1. Determines the application area.

  2. Starts the front controller and routing systems in order to find and execute a controller action.

  3. Uses an HTTP response object to return result obtained from the controller action.

  4. Error handling (in the following priority order):

    1. If you are using developer mode:

      • If the Commerce application is not installed, redirect to Setup Wizard.
      • If the Commerce application is installed, display an error and HTTP status code 500 (Internal Server Error).
    2. If the Commerce application is in maintenance mode, display a user-friendly “Service Unavailable” landing page with HTTP status code 503 (Service Unavailable).

    3. If the Commerce application is not installed, redirect to Setup Wizard.

    4. If the session is invalid, redirect to the home page.

    5. If there is any other application initialization error, display a user-friendly “Page Not Found” page with HTTP status code 404 (Not Found).

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

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 Commerce application expects that all necessary resources exist in the <your Commerce 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 Commerce 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 is 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.