18

I have recently installed magento latest version i.e. Magento 2.3.0 in local wamp machine with php 7.2.4

Installed it using command line interface.

But when i tired to run it show me error like

Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'D:/wamp64/www/mage23/vendor/magento/module-theme/view/frontend/templates/page/js/require_js.phtml' in module: '' block's name: 'require.js'
robsch
  • 311
  • 2
  • 18
MageLerner
  • 508
  • 1
  • 4
  • 17

11 Answers11

52

Yes, This is the problem with windows. Windows uses "\" as separator, the array "directories" contains entries with "/" as separator, so the check will always fail. So you need to fix this by replacing the separator in core file:

Magento\Framework\View\Element\Template\File\Validator

function isPathInDirectories replace below code in isPathInDirectories function

$realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));
Rakesh Donga
  • 5,344
  • 2
  • 24
  • 57
Saurabh Dwivedi
  • 1,472
  • 10
  • 13
  • This is a very common issue, Many people are not aware of the fact that Magento does not officially support Windows servers! some hacks and un-official modifications such as this one needs to be done in order to make it work on a Windows machine, If you visit the below link "Magento 2.3.x technology stack requirements" you can see that the only supported OS is "Linux x86-64". https://devdocs.magento.com/guides/v2.3/install-gde/system-requirements-tech.html – Yacoub Oweis Feb 02 '19 at 08:50
  • for windows system what should be the actual code ? i am trying '' line in first but this single back-slash is not allowed ... – Flutterer Feb 20 '19 at 08:10
  • 5
    Okay, so I get that they don’t officially support Windows, but couldn’t they just use DIRECTORY_SEPARATOR like the rest of the world and not have this specific problem which seems like the only thing from working it in Windows? – ACJ Feb 25 '20 at 14:29
22

For me, solution worked is by going to the file \vendor\magento\framework\View\Element\Template\File\Validator.php and replacing the below function definition as below:

 protected function isPathInDirectories($path, $directories) {
     if (!is_array($directories)) {
         $directories = (array)$directories;
     }
     $realPath = $this->fileDriver->getRealPath($path);
     $realPath = str_replace('\\', '/', $realPath); // extra code added
     foreach ($directories as $directory) {
         if (0 === strpos($realPath, $directory)) {
             return true;
         }
     }
     return false; }

PS: This is windows specific issue.

Anas Mansuri
  • 2,627
  • 1
  • 11
  • 28
Tejas Vyas
  • 800
  • 8
  • 21
  • An even better solution is this one as it doesn't break linux compat: https://mage2.pro/t/topic/5774 – Syffys May 29 '20 at 11:03
6

Magento 2.3 does not support windows. You can find my solution here: enter link description here

Vincent
  • 61
  • 1
4

In Magento 2.2.9 replace /vendor/magento/framework/View/Element/Template/File/Validator.php isPathInDirectories function code with this code

protected function isPathInDirectories($path, $directories)
{
    if (!is_array($directories)) {
        $directories = (array)$directories;
    }
    foreach ($directories as $directory) {
        if (0 === strpos(str_replace('\\', '/', $this->fileDriver->getRealPath($path)), $directory)) {
            return true;
        }
    }
    return false;
}
Sanaullah Ahmad
  • 587
  • 1
  • 5
  • 23
3

It's the core issue of not just Magento 2.3.0, but I faced that problem in Magento 2.2.7 as well. In order to make the code work on windows instead of using realpath just use the $path argument passed to the method

Go to path /vendor/magento/framework/View/Element/Template/File/Validator.php and instead of the line

if (0 === strpos($realPath, $directory)) {

use

if (0 === strpos($path, $directory)) {

Or follow this discussion https://github.com/magento/magento2/issues/19480

Magebuddies
  • 147
  • 2
  • 12
  • This fixes the same problem that I've got on a Linux machine. However, I really don't understand why, since $path and $realpath is always exactly the same! I've added some var_dumps to find that out. Do you have any idea why this works? – robsch Aug 03 '20 at 14:53
  • Okay, that has probably something to do with this. – robsch Aug 03 '20 at 15:34
2

As mentioned the issue is Windows compatability. But I would recommend to change it a bit differently to work even when migrating systems, e.g. for local development on Windows and later deployment on a Linux server. So that only in case you are operating on Windows you will adjust the path.

In

\vendor\magento\framework\View\Element\Template\File\Validator.php

in

function isPathInDirectories()

replace

$realPath = $this->fileDriver->getRealPath($path);

with:

a) PHP >= 7.2:

if (PHP_OS_FAMILY === 'Windows')
  $realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));
else
  $realPath = $this->fileDriver->getRealPath($path);

b) PHP < 7.2:

if (strtolower(substr(PHP_OS, 0, 3)) === 'win')
  $realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));
else
  $realPath = $this->fileDriver->getRealPath($path);
Christian
  • 21
  • 3
1

Please See that,It should be double slash i.e "\\"

$realPath = str_replace('\\\', '/', $this->fileDriver->getRealPath($path));
Magento_Bhurio
  • 940
  • 5
  • 14
zuber bandi
  • 1,062
  • 1
  • 11
  • 22
1

This probably happens when developing under Windows System.

Go to line 140 in file Path /vendor/magento/framework/View/Element/Template/File/Validator.php Replace this code of line

$realPath = $this->fileDriver->getRealPath($path);

With

$realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));

Beware of this line of code

$realPath = str_replace('\', '/', $this->fileDriver->getRealPath($path));

This won't probably work because of the php backslash scape. you have to do double backslash to tell PHP explicitly that it is not dealing with new line here but a backslash.

Mubo
  • 121
  • 4
0

In my situation I set only one colon in xml file in path for template like this Vendor_Modulename:product/view/template.phtml but the the write way is to Vendor_Modulename::product/view/template.phtml. It may be invisible error

0

in magento 2.4 Validator.php path is:

....\lib\internal\Magento\Framework\View\Element\Template\File\Validator.php

0

for the latest magento 2.4.5

if you want to run with windows you have to change in below file C:\xampp8\htdocs\magento244\lib\internal\Magento\Framework\View\Element\Template\File\Validator.php

 $realPath = $this->fileDriver->getRealPath($path);

replace with

$realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));

after above change function look like below

protected function isPathInDirectories($path, $directories)
{
    if (!is_array($directories)) {
        $directories = (array)$directories;
    }
    //$realPath = $this->fileDriver->getRealPath($path);
    $realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));
    foreach ($directories as $directory) {
        if ($directory !== null && 0 === strpos($realPath, $directory)) {
            return true;
        }
    }
    return false;
}