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? Feb 21, 2019 at 6:44
  • why do you need to override the method isPathInDirectoriesfunction?
    – Amit Bera
    Feb 21, 2019 at 7:55

4 Answers 4


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 made the two files for that (I have overridden 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 and acknowledge you have read our privacy policy.

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