文件“Twig.xml”不存在

时间:2012-11-26 16:35:38

标签: symfony filter twig xml-twig

我正在关注symfony2教程,并试图在第5章http://tutorial.symblog.co.uk/docs/customising-the-view-more-with-twig.html中创建Twig过滤器扩展,但是,我不知道我做了什么,但是当我加载页面时,我得到以下内容错误:

  

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';
    }
}

更新

我现在已删除供应商目录并重新运行`php composer.phar install',现在我收到以下错误:

  

ServiceNotFoundException:服务“twig”依赖于不存在的服务“assetic.twig_extension”。

另外,我的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;

更新

我的composer.json如下:

{
    "name": "symfony/framework-standard-edition",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.1.*",
        "doctrine/orm": ">=2.2.3,<2.4-dev",
        "doctrine/doctrine-bundle": "1.0.*",
        "twig/twig": "1.*",
        "symfony/assetic-bundle": "2.1.*",
        "symfony/swiftmailer-bundle": "2.1.*",
        "symfony/monolog-bundle": "2.1.*",
        "sensio/distribution-bundle": "2.1.*",
        "sensio/framework-extra-bundle": "2.1.*",
        "sensio/generator-bundle": "2.1.*",
        "jms/security-extra-bundle": "1.2.*",
        "jms/di-extra-bundle": "1.1.*",
        "doctrine/data-fixtures": "dev-master",
        "doctrine/doctrine-fixtures-bundle": "dev-master",
        "doctrine/doctrine-migrations-bundle": "dev-master"
    },
    "scripts": {
        "post-install-cmd": [
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ],
        "post-update-cmd": [
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ]
    },
    "minimum-stability": "dev",
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "symfony-assets-install": "symlink"
    } }

2 个答案:

答案 0 :(得分:1)

如果您在symfony中创建Twig Extension,则将其标记为服务

例如在tester / TestBundle / Resources / config / services.yml

services:
  test_bundle.twig.tester_test_extension:
    class: tester\TestBundle\Twig\Extensions\testerTestExtension
    tags:
      - { name: twig.extension }

有关详细信息,请参阅文档http://symfony.com/doc/current/cookbook/templating/twig_extension.html

答案 1 :(得分:1)

您的composer.json似乎不正确。而不是

"twig/twig": "1.*",

该行应该是:

"twig/extensions": "1.0.*",