Magento 2 – Check if Using Pub Directory

directorymagento2static-content

How can we check if the website is using pub directory or not, independent of its mode?

Is there any Magento function present to check the root document?

Best Answer

And finally after many testing, I found the most relevant solution to handle this. Code is as below:

use Magento\Framework\App\Filesystem\DirectoryList;

protected $_directoryList;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    DirectoryList $directoryList,
    array $data = []
) {
    $this->_directoryList = $directoryList;

    parent::__construct($context, $data);
}

And now the pub directory can be checked by the condition

$pub = $this->_directoryList->getUrlPath("pub");
if ($pub == "pub") {
    //pub is not being used
} else {
    //pub directory is being pointed
}
Related Topic