|
| 1 | +--- |
| 2 | +group: configuration-guide |
| 3 | +title: Magento application initialization and bootstrap |
| 4 | +functional_areas: |
| 5 | + - Configuration |
| 6 | + - System |
| 7 | + - Setup |
| 8 | +--- |
| 9 | + |
| 10 | +## Overview of bootstrapping {#config-boot-overview} |
| 11 | + |
| 12 | +To run the Magento application, the following actions are implemented in [pub/index.php]({{ site.mage2bloburl }}/{{ page.guide_version }}/pub/index.php){:target="_blank"}: |
| 13 | + |
| 14 | +* Include [app/bootstrap.php]({{ site.mage2bloburl }}/{{ page.guide_version }}/app/bootstrap.php){:target="_blank"} which performs essential initialization routines, such as error handling, initializing the autoloader, setting profiling options, setting the default timezone, and so on. |
| 15 | +* Create an instance of [\Magento\Framework\App\Bootstrap]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/App/Bootstrap.php){:target="_blank"}. <!-- It requires initialization parameters to be specified in constructor. Normally, the $_SERVER super-global variable is supposed to be passed there. --> |
| 16 | +* Create a Magento application instance ([\Magento\Framework\AppInterface]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/AppInterface.php){:target="_blank"}). |
| 17 | +* Run Magento |
| 18 | + |
| 19 | +## Bootstrap run logic {#config-boot-logic} |
| 20 | + |
| 21 | +[The bootstrap object]({{ site.mage2bloburl }}/{{ page.guide_version }}/app/bootstrap.php){:target="_blank"} uses the following algorithm to run the Magento application: |
| 22 | + |
| 23 | +1. Initializes the error handler. |
| 24 | +1. Creates the [object manager]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/ObjectManager){:target="_blank"} and basic shared services that are used everywhere and are affected by the environment. The environment parameters are injected properly into these objects. |
| 25 | +1. Asserts that maintenance mode is *not* enabled; otherwise, terminates. |
| 26 | +1. Asserts that the Magento application is installed; otherwise, terminates. |
| 27 | +1. Starts the Magento application. |
| 28 | + |
| 29 | + 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 either `true` or `false`: |
| 30 | + |
| 31 | + * If `true`: Magento handled exception successfully. No need to do anything else. |
| 32 | + * If `false` (or any other empty result): Magento did not handle the exception. The bootstrap object performs the default exception handling subroutine. |
| 33 | + |
| 34 | +1. Sends the response provided by the application object. |
| 35 | + |
| 36 | + {:.bs-callout-info} |
| 37 | +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. |
| 38 | + |
| 39 | +Sample entry point script that modifies the bootstrap object: |
| 40 | + |
| 41 | +```php |
| 42 | +<?php |
| 43 | +use Magento\Framework\App\Bootstrap; |
| 44 | +require __DIR__ . '/app/bootstrap.php'; |
| 45 | + |
| 46 | +$params = $_SERVER; |
| 47 | +$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = true; // default false |
| 48 | +$params[Bootstrap::PARAM_REQUIRE_IS_INSTALLED] = false; // default true |
| 49 | +$bootstrap = Bootstrap::create(BP, $params); |
| 50 | + |
| 51 | +/** @var \Magento\Framework\App\Http $app */ |
| 52 | +$app = $bootstrap->createApplication('Magento\Framework\App\Http'); |
| 53 | +$bootstrap->run($app); |
| 54 | +``` |
| 55 | + |
| 56 | +## Default exception handling {#config-boot-exception} |
| 57 | + |
| 58 | +The bootstrap object specifies how the Magento application handles uncaught exceptions as follows: |
| 59 | + |
| 60 | +* In [developer mode]({{ page.baseurl }}/config-guide/bootstrap/magento-modes.html#developer-mode), displays the [exception](https://glossary.magento.com/exception) as-is. |
| 61 | +* In any other mode, attempts to log exception and display a generic error message. |
| 62 | +* Terminates Magento with error code `1` |
| 63 | + |
| 64 | +## Entry point applications {#config-boot-entry} |
| 65 | + |
| 66 | +We have the following entry point applications (that is, applications defined by Magento that are used by the web server as a directory index): |
| 67 | + |
| 68 | +* [HTTP entry point](#config-boot-entry-http) |
| 69 | +* [Static resource entry point](#config-boot-entry-static) |
| 70 | +* [Media resource entry point](#config-boot-entry-media) |
| 71 | + |
| 72 | +### HTTP entry point {#config-boot-entry-http} |
| 73 | + |
| 74 | +[\Magento\Framework\App\Http]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/App/Http){:target="_blank"} operates as follows: |
| 75 | + |
| 76 | +1. Determines the [application area]({{ page.baseurl }}/architecture/archi_perspectives/components/modules/mod_and_areas.html). |
| 77 | +1. Starts the front controller and routing systems in order to find and execute a controller action. |
| 78 | +1. Uses an HTTP response object to return result obtained from the controller action. |
| 79 | +1. Error handling (in the following priority order): |
| 80 | + |
| 81 | + 1. If you're using [developer mode]({{ page.baseurl }}/config-guide/bootstrap/magento-modes.html#developer-mode): |
| 82 | + * If the Magento application is not installed, redirect to Setup Wizard. |
| 83 | + * If the Magento application is installed, display an error and HTTP status code 500 (Internal Server Error). |
| 84 | + 1. If the Magento application is in maintenance mode, display a user-friendly "Service Unavailable" landing page with HTTP status code 503 (Service Temporary Unavailable). |
| 85 | + 1. If the Magento application is *not* installed, redirect to Setup Wizard. |
| 86 | + 1. If the session is invalid, redirect to the home page. |
| 87 | + 1. If there is any other application initialization error, display a user-friendly "Page Not Found" page with HTTP status code 404 (Not Found). |
| 88 | + 1. 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. |
| 89 | + |
| 90 | +### Static resource entry point {#config-boot-entry-static} |
| 91 | + |
| 92 | +[\Magento\Framework\App\StaticResource]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/App/StaticResource.php){:target="_blank"} 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. |
| 93 | + |
| 94 | + {:.bs-callout-info} |
| 95 | +The entry point for static view files is not used in [production mode]({{ page.baseurl }}/config-guide/bootstrap/magento-modes.html#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. |
| 96 | + |
| 97 | +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`. |
| 98 | +When the request is redirected to the entry point, the Magento application parses the requested [URL](https://glossary.magento.com/url) based on retrieved parameters and finds the requested resource. |
| 99 | + |
| 100 | +* 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. |
| 101 | +* In [default]({{ page.baseurl }}/config-guide/bootstrap/magento-modes.html#default-mode) mode, the retrieved resource is published so it is accessible by the previously requested URL. |
| 102 | + |
| 103 | + 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. |
| 104 | + |
| 105 | +### Media resource entry point {#config-boot-entry-media} |
| 106 | + |
| 107 | +[Magento\MediaStorage\App\Media]({{ site.mage2bloburl }}/{{ page.guide_version }}/app/code/Magento/MediaStorage/App/Media.php){:target="_blank"} 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](https://glossary.magento.com/media-storage). |
| 108 | + |
| 109 | +`\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. |
| 110 | + |
| 111 | +{:.ref-header} |
| 112 | +Related topics |
| 113 | + |
| 114 | +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: |
| 115 | + |
| 116 | +* [Customize base directory paths (MAGE_DIRS)]({{ page.baseurl }}/config-guide/bootstrap/mage-dirs.html) |
| 117 | +* [Set the mode (MAGE_MODE)]({{ page.baseurl }}/config-guide/bootstrap/magento-modes.html) |
| 118 | +* [Enable an HTML profiler (MAGE_PROFILER)]({{ page.baseurl }}/config-guide/bootstrap/mage-profiler.html) |
0 commit comments