7

I have created a custom module in which I want to check if a (image)file exists in my view/frontend/web/images folder.

I have checked this answer but in only shows how to get Magento's directory paths.

How can I get full path for my module's directory which will support with Magento 2's static content deploy feature?

EDIT

As per Rakesh's answer I can use below code.

$mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::APP)->getAbsolutePath();
$modulePath = echo $mediapath.'code/Packagename/Modulename/view/frontend/web/images';

Is there any other alternative to this approach?

  • Alternativa way is look like pass path in function, $mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::APP)->getAbsolutePath('code/Packagename/Modulename/view/frontend/web/images'); – Rakesh Jesadiya Jun 23 '17 at 5:59
  • @RakeshJesadiya, I am looking something like we use to get URL e.g. $block->getViewFileUrl('Vendor_Module/images/blank.png'); – John Jun 23 '17 at 6:04
  • Are you looking for path or url? – Rakesh Jesadiya Jun 23 '17 at 6:06
  • I am looking for path. I just provided example how Magento gets URL. So is there any code like that which can give us path just like getting URL. – John Jun 23 '17 at 6:08
  • Check answer with example, rakeshjesadiya.com/… – Rakesh Jesadiya Nov 21 at 12:12
14

Use code below to get module directory path

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';
    }
}

but if you need to check content static file I'd recommend you to use this way...

class CustomModel
{
/**
     * @var \Magento\Framework\View\Asset\Repository
     */
    protected $assetRepository;

    /**
     * @param \Magento\Framework\View\Asset\Repository $assetRepository
     */
    public function __construct(
        \Magento\Framework\View\Asset\Repository $assetRepository
    ) {
        $this->assetRepository = $assetRepository;
    }

    public function getMyFilePath()
    {
        $fileId = 'Vendor_Module::images/myimage.png';
        $params = [
            'area' => 'frontend'
        ];
        $asset = $this->assetRepository->createAsset($fileId, $params);
        try {
            return $asset->getSourceFile();
        } catch (\Exception $e) {
            return null;
        }
    }
}
  • Thanks Nicholas. This seems a better approach than using full directory path. – John Jun 23 '17 at 6:05
  • 1
    if you need to check content static file, I'd recommend another one way. Added to answer. – Nicholas Miller Jun 23 '17 at 6:16
  • Sorry for delay, I was just implementing it with my code. Your answer is exactly what I was looking for. Thanks for sharing. – John Jun 23 '17 at 6:33
1

First just get your App folder path after getting app path you can got it by add your sub path,

  use Magento\Framework\App\Filesystem\DirectoryList;

    protected $filesystem;

    public function __construct(
        \Magento\Framework\Filesystem $filesystem
    )
    {
        $this->_filesystem = $filesystem;
    }

    public function getPathofmodule(){
        $mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::APP)->getAbsolutePath();
        $modulePath = echo $mediapath.'code/Packagename/Modulename/view/frontend/web/images';
    }
  • Thanks Rakesh. This is what I thought too. But isn't there any other, better way to do it instead of using full directory path? – John Jun 23 '17 at 5:40
  • @RakeshJesadiya How to access this image in web,after getting the absolute path ? – Aditya Shah Dec 28 '18 at 6:11
  • @AdityaShah rakeshjesadiya.com/… answer in details. – Rakesh Jesadiya Nov 21 at 12:12
1

You can get module's images path like this: $block->getViewFileUrl('VendorName_ModuleName::images/image.jpg).

1

You can also use Magento\Framework\Module\Dir Class to get Directory path of the module:

EXample:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $dir = $objectManager->get('Magento\Framework\Module\Dir');
    /*Retrieve full path to a directory of certain type within a module*/
    echo $dir->getDir('Magento_Customer');
    echo $dir->getDir('Magento_Customer','Controller');

Syntax:

    getDir('Module_Name','Directories');                                      
    /*getDir method not allow to read type of module's directory except these*/
    const MODULE_ETC_DIR = 'etc';
    const MODULE_I18N_DIR = 'i18n';
    const MODULE_VIEW_DIR = 'view';
    const MODULE_CONTROLLER_DIR = 'Controller';

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.