0

I'd like to use getModuleDir to get my module's view directory and use it in my template file. Which file do I add the getModuleDir function to, and how do I call this function in my template?

  • Vendor = Martens
  • Module = Showroom
  • Template = showroom.phtml

I have this code but do not know how to use or alter it...

class CustomModel
{
/**
 * @var \Magento\Framework\Module\Dir\Reader
 */
protected $moduleReader;

/**
 * @param \Magento\Framework\Module\Dir\Reader $moduleReader
 */
public function __construct(
    \Magento\Framework\Module\Dir\Reader $moduleReader
) {
    $this->moduleReader = $moduleReader;
}

public function getDirectory()
{
    $viewDir = $this->moduleReader->getModuleDir(
        \Magento\Framework\Module\Dir::MODULE_VIEW_DIR,
        'Vendor_Module'
    );
    return $viewDir . '/frontend/web/images';
}
}
0

Try this code with FileSystem(\Magento\Framework\Filesystem):

class CustomModel
   {
     /**
     * @var \Magento\Framework\Filesystem
     */
     protected $moduleReader;

     /**
      * @param \Magento\Framework\Filesystem $moduleReader
      */
     public function __construct(
        \Magento\Framework\Filesystem $moduleReader
     ) {
         $this->moduleReader = $moduleReader;
     }

     public function getDirectory()
    {
         $viewDir = $this->moduleReader->getDirectoryRead(
             \Magento\Framework\Module\Dir::MODULE_VIEW_DIR,
             'Vendor_Module'
         );
         return $viewDir . '/frontend/web/images';
    }
}
| improve this answer | |
  • Thank you but this doesn't answer my question. Which file holds this code and how do I use it in my template file? – Bas Nov 15 '17 at 12:02
  • Where do I put this code? In which file or folder? – Bas Nov 23 '17 at 11:29
  • put this code into your custom class – Ronak Chauhan Nov 23 '17 at 11:39
  • As in "Namespace\Module\Model\MyClass.php" ? – Bas Nov 23 '17 at 12:37
  • no in block file which you used for your template – Ronak Chauhan Nov 23 '17 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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