Magento Asked by Bossman on December 2, 2021
I have a controller that works and does what i need it to do if i manually add a path like: $mediaImages = '/magento/pub/media/images/';
But i cant get the DirectoryList injection to work, it returns undefined method?
<?php
namespace VendorModuleControllerAdminhtmlUpdateTable;
class Index extends MagentoFrameworkAppActionAction
{
protected $_pageFactory;
protected $_postFactory;
protected $_filesystem;
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkViewResultPageFactory $pageFactory,
NewcastleMotorcyclesAdminMenuModelPostFactory $postFactory,
MagentoFrameworkAppFilesystemDirectoryList $filesystem
)
{
$this->_pageFactory = $pageFactory;
$this->_postFactory = $postFactory;
$this->_filesystem = $filesystem;
return parent::__construct($context);
}
public function execute()
{
$mediaImages = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images/');
}
}
The error i get is:
Fatal error: Uncaught Error: Call to undefined method
MagentoFrameworkAppFilesystemDirectoryList::getDirectoryRead()
I’m obviously missing somtehing, any advise would be great!
Try this:
<?php
namespace VendorModuleControllerAdminhtmlUpdateTable;
use MagentoFrameworkAppFilesystemDirectoryList;
class Index extends MagentoBackendAppAction
{
protected $_pageFactory;
protected $_postFactory;
protected $_filesystem;
public function __construct(
MagentoBackendAppActionContext $context,
MagentoFrameworkViewResultPageFactory $pageFactory,
NewcastleMotorcyclesAdminMenuModelPostFactory $postFactory,
MagentoFrameworkFilesystem $fileSystem
)
{
$this->_pageFactory = $pageFactory;
$this->_postFactory = $postFactory;
$this->_filesystem = $filesystem;
return parent::__construct($context);
}
public function execute()
{
$mediaImages = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images');
}
}
After run command: php bin/magento setup:di:compile
Answered by Abdul on December 2, 2021
Instead of using direct object manager, use It like
use MagentoFrameworkAppFilesystemDirectoryList;
protected $_filesystem;
public function __construct(
MagentoFrameworkFilesystem $_filesystem,
)
{
$this->_filesystem = $_filesystem;
}
Now you can media path by,
$mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images/');
Answered by user68116 on December 2, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP