简体   繁体   English

致命错误:未被捕获的错误:找不到类'Twig_Autoloader'

[英]Fatal error: Uncaught Error: Class 'Twig_Autoloader' not found

Recently I installed Twig2.0 via Composer for PHP7.2 and when running the code I'm getting these errors, 最近,我通过Composer for PHP7.2安装了Twig2.0,运行代码时出现这些错误,

( ! ) Fatal error: Uncaught Error: Class 'Twig_Autoloader' not found in C:\\wamp64\\www\\php-twig\\example.php on line 4 (!)致命错误:未捕获的错误:在第4行的C:\\ wamp64 \\ www \\ php-twig \\ example.php中找不到类'Twig_Autoloader'

( ! ) Error: Class 'Twig_Autoloader' not found in C:\\wamp64\\www\\php-twig\\example.php on line 4 (!)错误:在第4行的C:\\ wamp64 \\ www \\ php-twig \\ example.php中找不到类'Twig_Autoloader'

I go through the issues in GitHub. 我在GitHub中讨论了这些问题。

Here is my PHP code, 这是我的PHP代码,

<?php

require 'vendor/autoload.php';
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem('templates');

$options = array(
    'name' => 'Sumithran',
);

$twig = new Twig_Environment($loader, $options);

And index.twig 和index.twig

<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Twig test</title>
</head>
<body>

    <h1>Hello world</h1>

    <p>And hello {{ name }}</p>

</body>
</html>

How to solve this? 如何解决呢?
Thanks in Advance! 提前致谢!

Twig_Autoloader was deprecated in version 1.21. Twig_Autoloader在版本1.21中已弃用。 You are using version 2.0, so you must use: 您正在使用2.0版,因此必须使用:

$loader = new \Twig\Loader\FilesystemLoader('templates');

$options = array(
    'name' => 'Sumithran',
);

$twig = new \Twig\Environment($loader, $options);

More details at Twig Docs - Twig for Developers . Twig Docs-Twig for Developers上有更多详细信息。

Twig version 2+ introduced the use of namespaces and the class-structure is a bit different now. Twig版本2+引入了名称空间的使用,并且类结构现在有所不同。

For instance instead of in Twig_Loader_Filesystem the filesystem-loader is located at Twig\\Loader\\FilesystemLoader . 例如,文件系统加载器位于Twig\\Loader\\FilesystemLoader而不是在Twig_Loader_Filesystem

You can also use rector to change all the namespaces to version 2 at once. 您还可以使用rector一次将所有名称空间更改为版本2。

Tomas Votruba describes the process more detailed in this blog-post . Tomas Votruba在此博客文章中更详细地描述了该过程。

TLDR; TLDR; - Run the following commands to upgrade to namespaces seamlessly. -运行以下命令以无缝升级到名称空间。

composer require rector/rector --dev # make sure you have version 0.4.10+ at least
vendor/bin/rector process src --level twig-underscore-to-namespace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM