2

I want to be able to make Magento aware of an extra folder in my root called private_files. I thought I could do this by adding the following to my di.xml:

<!--
    Make Magento aware of an extra directory:
-->
<type name="Magento\Framework\Filesystem\DirectoryList">
    <arguments>
        <argument name="config" xsi:type="array">
            <item name="private_files" xsi:type="array">
                <item name="path" xsi:type="string">private_files</item>
            </item>
        </argument>
    </arguments>
</type>

So I could get the directory in my code like so:

$mediaDirectory = $this->fileSystem->getDirectoryRead('private_files');

Unfortunately, this doesn't work. Why is this? I noticed that if I make errors in my XML on purpose they don't get picked up if I put a die() in Magento\Framework\Filesystem\DirectoryList::assertCode().

Can magento/framework-classes and settings not be manipulated with di.xml?

1 Answer 1

0

The allowed folders are listed in the following file: vendor/magento/framework/App/Filesystem/DirectoryList.php

    public static function getDefaultConfig()
{
    $result = [
        self::ROOT => [parent::PATH => ''],
        self::APP => [parent::PATH => 'app'],
        self::CONFIG => [parent::PATH => 'app/etc'],
        self::LIB_INTERNAL => [parent::PATH => 'lib/internal'],
        self::VAR_DIR => [parent::PATH => 'var'],
        /** @deprecated  */
        self::VAR_EXPORT => [parent::PATH => 'var/export'],
        self::CACHE => [parent::PATH => 'var/cache'],
        self::LOG => [parent::PATH => 'var/log'],
        self::DI => [parent::PATH => 'generated/metadata'],
        self::GENERATION => [parent::PATH => Io::DEFAULT_DIRECTORY],
        self::SESSION => [parent::PATH => 'var/session'],
        self::MEDIA => [parent::PATH => 'pub/media', parent::URL_PATH => 'media'],
        self::STATIC_VIEW => [parent::PATH => 'pub/static', parent::URL_PATH => 'static'],
        self::PUB => [parent::PATH => 'pub', parent::URL_PATH => ''],
        self::LIB_WEB => [parent::PATH => 'lib/web'],
        self::TMP => [parent::PATH => 'var/tmp'],
        self::UPLOAD => [parent::PATH => 'pub/media/upload', parent::URL_PATH => 'media/upload'],
        self::TMP_MATERIALIZATION_DIR => [parent::PATH => 'var/view_preprocessed/pub/static'],
        self::TEMPLATE_MINIFICATION_DIR => [parent::PATH => 'var/view_preprocessed'],
        self::SETUP => [parent::PATH => 'setup/src'],
        self::COMPOSER_HOME => [parent::PATH => 'var/composer_home'],
        self::GENERATED => [parent::PATH => 'generated'],
        self::GENERATED_CODE => [parent::PATH => Io::DEFAULT_DIRECTORY],
        self::GENERATED_METADATA => [parent::PATH => 'generated/metadata'],
        self::VAR_IMPORT_EXPORT => [parent::PATH => 'var', parent::URL_PATH => 'import_export']
    ];
    return parent::getDefaultConfig() + $result;
}

If we need to add some additional folder in the root folder of Magento, we must add the folder name in the array $result.

But this method cannot be overwritten because it is static. We will have to look at where it is used and perform some rewriting there.

Remember to give the folder write permission for the user or group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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