TransWikia.com

Fatal error: Uncaught Error: Call to undefined method getDirectoryRead()

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!

2 Answers

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

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP