Magento 2 has, infamously, two index.php files. The first is in the root folder


the second is in a pub folder


A Magento developer needs to chose which file they're going to point to as their webroot. This creates some problems related to file paths and URL paths. Magento solves this by bootstrapping index.php like this

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

and bootstrapping pub/index.php like this

$params = $_SERVER;
    DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
    DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'media'],
    DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'static'],
    DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

i.e. -- Magento adds values to the array that's the second argument to create.

This solution, however, creates another problem. The Magento CLI also need to bootstrap an enviornment. It seems to do so here

#File: vendor/magento/framework/Console/Cli.php
$bootstrapParam = new ComplexParameter(self::INPUT_KEY_BOOTSTRAP);
$params = $bootstrapParam->mergeFromArgv($_SERVER, $_SERVER);
$params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
$bootstrap = Bootstrap::create(BP, $params);

There's (Seemingly?) no opportunity to add additional parameters, which means the CLI console will always think you're running from the main index file, and any code that needs the DirectoryList params may not function correctly.

Is there any way (short of editing Cli.php directly or replacing it via a composer autoload) to inject these parameters into the CLI bootstrap? Looking for known solutions, not theories on what might work. Thank you!

  • since you do not look for theories and also might have already tried this, I#ll just leave a comment. You might be able to manipulate the $_SERVER array (not sure if that works) with a compose autoload file (same way as registration.php). I really think there is no "nice" solution except magento just removing the {root}/index.php file – David Verholen Dec 22 '16 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.