I'm trying to get a public folder path in shopware 6 in order to create a directory inside using the filesystem. Does anyone have an idea about it?
You could have a look at the service
shopware.filesystem.public
which is defined in
vendor/shopware/core/Framework/DependencyInjection/filesystem.xml
as
<service class="League\Flysystem\FilesystemInterface" id="shopware.filesystem.public" public="true">
<factory service="Shopware\Core\Framework\Adapter\Filesystem\FilesystemFactory" method="factory"/>
<argument>%shopware.filesystem.public%</argument>
</service>
When you inject this service, you can call
$publicFileSystem->createDir('folder')
to create your folder.
That does not exactly answer the question on how to get the path - but you usually don't need that, as you woult not interact directly with the filesystem, but via Flysystem
classes instead.
If you really need the path, you could try something like
$publicFileSystem->getMetaData('.')['path']
This is untested by me and as written before, you might just not need the path. Also keep in mind, that the underlying filesystem might not even be a local storage.
$fileSystem must not be accessed before initialization
. Do you know what am I missing? –
Mccain Maybe not exactly bulletproof, but most of the time you can also use getcwd();
https://www.php.net/manual/en/function.getcwd.php
I mostly use it for writing debug-files like error_log(print_r($data, true)."\n", 3, getcwd().'/error.log');
Instead of trying to get the public path through the file-system, you can also simply inject the according config from it via:
'%shopware.filesystem.public.config.root%'
This will give you the path to the public-directory.
© 2022 - 2024 — McMap. All rights reserved.