9

In pub/index.php, the following code is used to set change URLs for media and static assets to omit "/pub":

$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = [
    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);

Unfortunately in CLI scripts (bin/magento) there is nothing like this.

I found the class Magento\Setup\Mvc\Bootstrap\InitParamListener with the following code:

$initParams = $application->getServiceManager()->get(self::BOOTSTRAP_PARAM);
$directoryList = $this->createDirectoryList($initParams);

But I don't understand where this Zend Service Manager retrieves the data and how it might be possible to influence it.

My concrete problem is that an indexer has to generate full URLs and currently does it like http://magento.local/pub/media/..., where I need it to be http://magento.local/media/...

How can I tell the CLI that my virtual host points to pub and "pub" should not be part of URLs?

Ideally this should work without having to rely on additional CLI parameters or environment variables.

  • 2
    This has recently been reported as a bug: github.com/magento/magento2/issues/8868 – Scott Buchanan Apr 5 '17 at 20:21
  • 1
    I found that you can set this using an argument like --magento-init-params=MAGE_DIRS[media][uri]=media. This actually works for all commands (evident by stepping through code), but commands outside setup: namespace incorrectly give an error that the option does not exist. – Scott Buchanan Apr 5 '17 at 20:23
  • @ScottSB Not exactly what I was hoping for, but still an interesting find, thanks for that! – Fabian Schmengler Apr 5 '17 at 20:29
5

After much travail, I found a solution using environment variables, which I know isn't what you wanted. You can set the parameters using environment variables like this (replacing $APP_ROOT with your actual Magento root):

env \
    "MAGE_DIRS[base][path]=$APP_ROOT" \
    "MAGE_DIRS[pub][uri]=" \
    "MAGE_DIRS[media][uri]=media" \
    "MAGE_DIRS[static][uri]=static" \
    "MAGE_DIRS[upload][uri]=media/upload" \
    bin/magento indexer:reindex

Note that it's necessary to specify the base path as above because when you use environment variables, your MAGE_DIRS definition overwrites the initial default that includes that. It will, however, get merged with the fuller default config in \Magento\Framework\App\Filesystem\DirectoryList.

Credit to this answer from the chief architect himself for putting me on the scent:

How to change Magento's "var" directory to other place in Magento 2?

  • How do you use environment variables? Are these getting added into VHOST like the referenced answer? – harri Jun 5 '18 at 15:48
  • @harri I don't understand your question. Because this talking about a CLI command, a vhost isn't relevant, as the web server isn't involved. The environment variables are passed right on the command line as illustrated (arguments to env in the format FOO=bar get set as environment variables for the command at the end, in this case bin/magento). – Scott Buchanan Jun 5 '18 at 19:11
  • Ah for my i'm getting this issue with a method run via cron so not sure if there is a way to wack this in with my bin/magento cron:run in crontab? – harri Jun 6 '18 at 7:17
  • Just take what I have in the answer and replace indexer:reindex with cron:run (and still update $APP_ROOT too of course). Then drop that whole thing into the crontab. Not sure if crontab will like the newlines, so you could just replace them with spaces (removing the trailing backslashes in that case). – Scott Buchanan Jun 6 '18 at 23:02
3

Another, simpler option is to simply hard-code the media path in the store config under Stores > Configuration > Web to:

{{unsecure_base_url}}media/

and

{{secure_base_url}}media/

Due credit to https://github.com/magento/magento2/issues/9111#issuecomment-292002922

2

I found another way to set MAGE_DIRS via CLI, just copy

/vendor/magento/framework/App/Filesystem/DirectoryList.php

to

/lib/internal/Magento/Framework/App/Filesystem/DirectoryList.php 

And now you can change default directory of all internal files.

  • thx this helped me to change the cache folder for cli and webapp ;D – ZFNerd Oct 22 '18 at 12:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.