windows安装magento2.3出现的问题和解决办法

阅读导航

前言:

最近看网站的搜索引擎关键词记录,发现一个问题搜索的特别多,就是Windows安装magento2.3。
搜索指定版本是2.3的。

一直在虚拟机里面测试magento,所以都没碰到过这个问题。自己亲自测试了下,安装过程中一切正常,操作跟magento2其他版本一样。但打开首页就开始报如下错误:

1 exception(s):
Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-theme/view/frontend/templates/page/js/require_js.phtml' in module: '' block's name: 'require.js'

Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-theme/view/frontend/templates/page/js/require_js.phtml' in module: '' block's name: 'require.js'
#0 D:\wwwroot\magento2.top\vendor\magento\framework\View\Element\Template.php(301): Magento\Framework\View\Element\Template->fetchView('D:/wwwroot/mage...')
#1 D:\wwwroot\magento2.top\vendor\magento\framework\View\Element\AbstractBlock.php(668): Magento\Framework\View\Element\Template->_toHtml()
#2 D:\wwwroot\magento2.top\vendor\magento\framework\View\Result\Page.php(249): Magento\Framework\View\Element\AbstractBlock->toHtml()

查看日记文件 var/log/system.log 也有如下的报错信息

[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-theme/view/frontend/templates/messages.phtml' in module: '' block's name: 'messages' [] []
[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-customer/view/frontend/templates/account/authentication-popup.phtml' in module: 'Magento_Customer' block's name: 'authentication-popup' [] []
[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-customer/view/frontend/templates/js/section-config.phtml' in module: 'Magento_Customer' block's name: 'customer.section.config' [] []
[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-customer/view/frontend/templates/js/customer-data.phtml' in module: 'Magento_Customer' block's name: 'customer.customer.data' [] []
[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml' in module: 'Magento_Customer' block's name: 'customer.data.invalidation.rules' [] []
[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/amzn/amazon-pay-and-login-with-amazon-core-module/view/frontend/templates/config.phtml' in module: 'Amazon_Core' block's name: 'amazon_config' [] []
[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-page-cache/view/frontend/templates/javascript.phtml' in module: 'Magento_PageCache' block's name: 'pageCache' [] []
[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-catalog/view/frontend/templates/product/compare/sidebar.phtml' in module: '' block's name: 'catalog.compare.sidebar' [] []
[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-wishlist/view/frontend/templates/sidebar.phtml' in module: 'Magento_Wishlist' block's name: 'wishlist_sidebar' [] []
[2019-03-28 15:50:06] main.CRITICAL: Invalid template file: 'D:/wwwroot/magento2.top/vendor/magento/module-newsletter/view/frontend/templates/subscribe.phtml' in module: 'Magento_Newsletter' block's name: 'form.subscribe' [] []


问题产生原因

大概就是说magento2.3后不建议也不支持在windows上面部署
但开发还是用windows比较多的,所以问题很早就有人碰到了。解决起来也简单

解决办法

找到并修改类文件 Magento\Framework\View\Element\Template\File\Validator

注意:官网下载的打包程序和Git上获取的程序文件路径不一样。

    protected function isPathInDirectories(path,directories)
    {
        if (!is_array(directories)) {directories = (array)directories;
        }realPath = this->fileDriver->getRealPath(path);

        // 添加下面这一行
        realPath = str_replace('\\', '/',realPath);

        foreach (directories asdirectory) {
            if (0 === strpos(realPath,directory)) {
                return true;
            }
        }
        return false;
    }

修改后测试验证

  • 设置开发模式
php bin/magento deploy:mode:set developer
  • 清除/generated/目录下的文件

  • 制生成静态资源文件

由于我现在没有配置本地的开发环境,这里强制生成静态资源文件

php bin/magento setup:static-content:deploy -f

到这里,打开网站就能正常了。

参考资料 https://magento.stackexchange.com/questions/251946/magento-2-3-problems-on-local-server