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 Vendor\Module\Controller\Adminhtml\UpdateTable;
class Index extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
protected $_postFactory;
protected $_filesystem;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\NewcastleMotorcycles\AdminMenu\Model\PostFactory $postFactory,
\Magento\Framework\App\Filesystem\DirectoryList $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 Magento\Framework\App\Filesystem\DirectoryList::getDirectoryRead()
I'm obviously missing somtehing, any advise would be great!
4 Answers 4
Instead of using direct object manager, use It like
use Magento\Framework\App\Filesystem\DirectoryList;
protected $_filesystem;
public function __construct(
\Magento\Framework\Filesystem $_filesystem,
)
{
$this->_filesystem = $_filesystem;
}
Now you can media path by,
$mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images/');
-
I have tried with 'use' and get this error: Fatal error: Trait '[VENDOR][MODULE]\Controller\Adminhtml\UpdateTable\Magento\Framework\App\Filesystem\DirectoryList' not found in C:\xampp\htdocs\magento\app\code[VENDOR][MODULE]\Controller\Adminhtml\UpdateTable\Index.php on line 6Bossman– Bossman2018年06月13日 11:52:32 +00:00Commented Jun 13, 2018 at 11:52
-
can you share your code please ?user68116– user681162018年06月13日 11:54:43 +00:00Commented Jun 13, 2018 at 11:54
-
the error should avoid using
use Magento\Framework\App\Filesystem\DirectoryList;which described by Prashant.2018年06月13日 11:55:30 +00:00Commented Jun 13, 2018 at 11:55 -
I have added the 'use' correctly now and have it working, almost. I get Almost, i get Not allowed to load local resource: file:///C:/xampp/htdocs/magento/pub/media/images/thumb_slider2f.jpg Is their something i need to set with Magento? Like i say folder permissions are fine if i manually enter the path?Bossman– Bossman2018年06月13日 12:05:06 +00:00Commented Jun 13, 2018 at 12:05
Try this:
<?php
namespace Vendor\Module\Controller\Adminhtml\UpdateTable;
use Magento\Framework\App\Filesystem\DirectoryList;
class Index extends \Magento\Backend\App\Action
{
protected $_pageFactory;
protected $_postFactory;
protected $_filesystem;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\NewcastleMotorcycles\AdminMenu\Model\PostFactory $postFactory,
\Magento\Framework\Filesystem $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
-
Almost, i get Not allowed to load local resource: file:///C:/xampp/htdocs/magento/pub/media/images/thumb_slider2f.jpg Is their something i need to set with Magento? Like i say folder permissions are fine if i manually enter the path?Bossman– Bossman2018年06月13日 12:03:39 +00:00Commented Jun 13, 2018 at 12:03
-
Please give permission to your folderAbdul– Abdul2018年06月13日 12:04:39 +00:00Commented Jun 13, 2018 at 12:04
-
That's what i mean. The permissions are fine. This controller can write and read from that folder if i manually add the path like this: $mediaImages = '/magento/pub/media/images/';Bossman– Bossman2018年06月13日 12:06:32 +00:00Commented Jun 13, 2018 at 12:06
-
can you please share your output => echo $mediaImages = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images'); die;Abdul– Abdul2018年06月13日 12:11:43 +00:00Commented Jun 13, 2018 at 12:11
-
C:/xampp/htdocs/magento/pub/media/images/Bossman– Bossman2018年06月13日 12:14:49 +00:00Commented Jun 13, 2018 at 12:14
Change this:
\Magento\Framework\App\Filesystem\DirectoryList $filesystem
to this:
\Magento\Framework\Filesystem $fileSystem
It will solve the problem.
Use \Magento\Framework\Filesystem $filesystem instead of \Magento\Framework\App\Filesystem\DirectoryList $filesystem.
Updated code should looks like this :
<?php
namespace Vendor\Module\Controller\Adminhtml\UpdateTable;
use Magento\Framework\App\Filesystem\DirectoryList;
class Index extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
protected $_postFactory;
protected $_filesystem;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\NewcastleMotorcycles\AdminMenu\Model\PostFactory $postFactory,
\Magento\Framework\Filesystem $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/');
}
}
Explore related questions
See similar questions with these tags.