Symfony “文件”;“Twig.xml”;不存在
我正在学习symfony2教程,并试图在第5章中创建细枝过滤器扩展。但是,我不确定我做了什么,但当我现在加载页面时,出现以下错误: InvalidArgumentException:文件“Twig.xml”不存在(位于:/var/www/tester/symfony/vendor/symfony/symfony/src/symfony/Bundle/TwigBundle/DependencyInjection//Resources/config中) 你知道为什么twig.xml不存在,以及如何重新创建它吗 我首先将Symfony “文件”;“Twig.xml”;不存在,symfony,filter,twig,xml-twig,Symfony,Filter,Twig,Xml Twig,我正在学习symfony2教程,并试图在第5章中创建细枝过滤器扩展。但是,我不确定我做了什么,但当我现在加载页面时,出现以下错误: InvalidArgumentException:文件“Twig.xml”不存在(位于:/var/www/tester/symfony/vendor/symfony/symfony/src/symfony/Bundle/TwigBundle/DependencyInjection//Resources/config中) 你知道为什么twig.xml不存在,以及如何重
{{comment.created | created_ago}}
添加到我的sidebar.html.twig文件中。然后,我在/src/tester/TestBundle/中创建了一个“Twig”文件夹,然后在此文件夹中创建了一个“Extensions”文件夹。然后,我使用以下代码在“Extensions”中创建了/testerTestBundleExtension.php:
namespace tester\TestBundle\Twig\Extensions;
class testerTestExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
'created_ago' => new \Twig_Filter_Method($this, 'createdAgo'),
);
}
public function createdAgo(\DateTime $dateTime)
{
$delta = time() - $dateTime->getTimestamp();
if ($delta < 0)
throw new \InvalidArgumentException("createdAgo is unable to handle dates in the future");
$duration = "";
if ($delta < 60)
{
// Seconds
$time = $delta;
$duration = $time . " second" . (($time > 1) ? "s" : "") . " ago";
}
else if ($delta <= 3600)
{
// Mins
$time = floor($delta / 60);
$duration = $time . " minute" . (($time > 1) ? "s" : "") . " ago";
}
else if ($delta <= 86400)
{
// Hours
$time = floor($delta / 3600);
$duration = $time . " hour" . (($time > 1) ? "s" : "") . " ago";
}
else
{
// Days
$time = floor($delta / 86400);
$duration = $time . " day" . (($time > 1) ? "s" : "") . " ago";
}
return $duration;
}
public function getName()
{
return 'tester_test_extension';
}
}
namespace tester\TestBundle\Twig\Extensions;
类testerTestExtension扩展\Twig\u扩展
{
公共函数getFilters()
{
返回数组(
'created_ago'=>new\Twig_Filter_方法('this'createdAgo'),
);
}
公共函数createdAgo(\DateTime$DateTime)
{
$delta=time()-$dateTime->getTimestamp();
如果($delta<0)
抛出new\InvalidArgumentException(“createdAgo无法处理将来的日期”);
$duration=“”;
如果($delta<60)
{
//秒
$time=$delta;
$duration=$time.“秒”。($time>1)?“s”:“)“之前”;
}
如果($delta 1)?“s”:“)”之前;
}
如果($delta 1)?“s”:“)”之前;
}
其他的
{
//日子
$time=底价($delta/86400);
$duration=$time.“day.”($time>1)?“s”:“)“ago”;
}
返回$duration;
}
公共函数getName()
{
返回“tester_test_extension”;
}
}
更新:
现在,我已删除供应商目录并重新运行“php composer.phar install”,现在我收到以下错误:
ServiceNotFoundException:服务“twig”依赖于不存在的服务“assetic.twig\u扩展”
另外,我的app/autoload.php是否正确
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
如果在symfony中创建细枝扩展,则必须将其标记为服务
例如,在tester/TestBundle/Resources/config/services.yml中
services:
test_bundle.twig.tester_test_extension:
class: tester\TestBundle\Twig\Extensions\testerTestExtension
tags:
- { name: twig.extension }
有关更多信息,请查看文档您的composer.json似乎不正确。而不是
"twig/twig": "1.*",
这条线应该是:
"twig/extensions": "1.0.*",
请发一些代码。可能是打字错误或其他问题。你使用的是哪种版本的Sf?IIRC该教程是针对Sf2.0的,在2.1中有一些破坏BC的更改。@taveo-代码添加到原始帖子中。运行php composer.phar update
并请给我们您的composer.json当我运行php composer.phar update时,我得到以下信息:“twig”服务依赖于一个不存在的服务“assetic.twig_extension”。谢谢。我现在使用上面的示例将其标记为服务,但是仍然收到错误。我需要重新安装twig extension吗?此外,如果我尝试清除缓存,我会收到错误-[InvalidArgumentException]文件“Twig.xml”不存在(位于:/var/www/tester/symfony/vendor/symfony/symfony/src/symfony/Bundle/TwigBundle/DependencyInjection/。/Resources/config)。
"twig/extensions": "1.0.*",