How to override this core file because of real path issue


Can you suggest how can we override this isPathInDirectoriesfunction in this file

  • Have you tried the preference in di.xml? – Amit Naraniwal Feb 21 at 6:44
  • why do you need to override the method isPathInDirectoriesfunction? – Amit Bera Feb 21 at 7:55

You can override vendor\magento\framework\View\Element\Template\File\Validator.php Using preference

Just add below code to your di.xml file

<preference for="Magento\Framework\View\Element\Template\File\Validator" type="[VENDOR]\[EXTENSION]\View\Element\Template\File\Validator" />

now create the class [VENDOR]\[EXTENSION]\View\Element\Template\File\Validator that extends Magento\Framework\View\Element\Template\File\Validator

This is done.


I have making the two files for that (I have override this core file)


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\View\Element\Template\File\Validator" type="Custome\Customelink\Block\Validator" />

Validator.php file

namespace Custome\Customelink\Block;
use Magento\Framework\Filesystem\Driver\File as FileDriver;

//use Magento\Framework\Filesystem\Driver\File;
class Validator extends \Magento\Framework\View\Element\Template\File\Validator
    protected $filePath;

    protected function isPathInDirectories($path, $directories)
        //my codes


You can override any of the core file of Magento2 by creating a di.xml file in your module/etc folder.

Here you can find some useful details as per your requirement.

Custom Link

Magento link


I think you should use plugin(Interceptors) instead of override whole class.

please read magento 2 dev docs for more information

Dev Docs

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.