Magento application initialization follows a specific sequence to ensure the environment is correctly set up before handling requests. The process is typically orchestrated via index.php and app/bootstrap.php.
The Bootstrapping Sequence
- Initialization:
app/bootstrap.php is included to perform essential routines like error handling, autoloader initialization, setting the default timezone, and configuring profiling options. - Bootstrap Instance: An instance of
\Magento\Framework\App\Bootstrap is created. This object requires initialization parameters (typically the $_SERVER super-global). - Application Instance: A Magento application instance (implementing
\Magento\Framework\App\AppInterface) is created using the bootstrap object. - Execution: The bootstrap object runs the application and sends the resulting response.
Bootstrap Run Logic
The \Magento\Framework\App\Bootstrap object follows this internal algorithm:
- Initializes the error handler.
- Creates the Object Manager and basic shared services, injecting environment parameters.
- Asserts that maintenance mode is not enabled (terminates if it is).
- Asserts that the Magento application is installed (terminates if it is not).
- Starts the application.
- Sends the response.
If an uncaught exception occurs during launch, it is passed to the catchException() method. If this method returns true, Magento has handled the exception; if it returns false or nothing, the bootstrap object performs default exception handling.
<?php
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);