在Symfony2中使用Twig Extension中的帮助程序服务

时间:2014-04-24 12:21:45

标签: symfony dependency-injection twig

我试图以这种方式设置一个twig过滤器:{{entities | fieldnames}},它将返回一个包含实体对象属性名称的数组。 在阅读并尝试了几个小时后,我的问题是,我无法从Twig Extension php执行$ this-> container-> get(" helpers")。我似乎没有正确链接服务容器......请帮助;)

错误:在/Users/a77/Dropbox/06.Proyectos/2011中的非对象上调用成员函数get()U-Vox / DEV U-Vox / Uvox Web / src / Acme / DemoBundle / Extension / FieldnamesTwigExtension.php第38行

或者如果构造没有= null 的错误 ContextErrorException:Catchable Fatal Error:传递给Acme \ DemoBundle \ Extension \ FieldnamesTwigExtension :: __ construct()的参数1必须是Acme \ DemoBundle \ Extension \ Container的实例,没有给出,在

中调用

services.yml

  

服务:

helpers:
    class:        Acme\DemoBundle\Services\Helpers

twig.extension.acme.demo:
    class:        Acme\DemoBundle\Twig\Extension\DemoExtension
    arguments:    [twig.loader]

acme.demo.listener:
    class:        Acme\DemoBundle\EventListener\ControllerListener
    arguments:    [twig.extension.acme.demo]

fieldnames:
    class: Acme\DemoBundle\Extension\FieldnamesTwigExtension
    arguments:  [@service_container]

扩展\ FieldnamesTwigExtension.php

use Symfony\Component\DependencyInjection\ContainerInterface as Container;
namespace Acme\DemoBundle\Extension;

class FieldnamesTwigExtension extends \Twig_Extension {
private $container;
public function __construct(Container $container=null)
{
    $this->container = $container;
    //var_dump ($container); exit; //  prints null !!!

}
 protected function get($service)
{
    return $this->container->get($service);
}

public function getFilters() {
    return array(
        'fieldnames' => new \Twig_Filter_Method($this, 'fieldnamesFilter'),
    );
}

public function getName() {
    return 'fieldnames_twig_extension';
}

public function fieldnamesFilter($obj) {
    if (is_array($obj)) {
        $first = $obj[0];

// GET (HELPERS) NOT WORKING :

       $fieldnames = $this->container->get("helpers")->getFieldnames($first);
        return $fieldnames;
    }
    return null;
}

public function twig_array_get_function($array, $name) {
    return $array[$name];
}

}

Helpers.php

namespace Acme\DemoBundle\Services;

class Helpers {

public function sum($n1, $n2) {
    return $n1 + $n2;
}

public function getFieldnames($entities) {
    $reflect = new \ReflectionClass($entities[0]);
    $props = $reflect->getProperties();
    $fieldnames = Array();
    foreach ($props as $prop) {
        $fieldnames[] = $prop->getName();
    }
    return $fieldnames;
 }

}

AcmeDemoExtension.php

  

命名空间Acme \ DemoBundle \ DependencyInjection;

     

使用Symfony \ Component \ DependencyInjection \ ContainerBuilder;使用   Symfony的\分量\ DependencyInjection \装载机\ YamlFileLoader;使用   Symfony的\分量\ HttpKernel \ DependencyInjection \推广;使用   Symfony的\分量\ CONFIG \ FileLocator;使用   Symfony的\分量\ DependencyInjection \定义; //添加了

     

类AcmeDemoExtension扩展Extension {

public function load(array $configs, ContainerBuilder $container) {

    $definition = new Definition('Acme\DemoBundle\Extension\AccessTwigExtension');
    $definition->addTag('twig.extension');
    $container->setDefinition('access_twig_extension', $definition);

    $definition2 = new Definition('Acme\DemoBundle\Extension\FieldnamesTwigExtension');
    $definition2->addTag('twig.extension');
    $container->setDefinition('fieldnames_twig_extension', $definition2);


    $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
    $loader->load('services.yml');
}

public function getAlias() {
    return 'acme_demo';
}

 }

3 个答案:

答案 0 :(得分:4)

我在我的应用程序上尝试了您的代码并遇到了同样的错误。解决它:

的Acme \ DemoBundle \ DependencyInjection \ AcmeDemoExtension.php

删除这一行:

$definition2 = new Definition('Acme\DemoBundle\Extension\FieldnamesTwigExtension');
$definition2->addTag('twig.extension');
$container->setDefinition('fieldnames_twig_extension', $definition2);

的Acme \ DemoBundle \资源\配置\ services.yml

替换这一行:

fieldnames:
    class: Acme\DemoBundle\Extension\FieldnamesTwigExtension
    arguments:  [@service_container]

通过:

fieldnames:
    class: Acme\DemoBundle\Extension\FieldnamesTwigExtension
    arguments:  [@service_container]
    tags: [{ name: twig.extension }]

答案 1 :(得分:2)

你这太难了。您应该直接将助手注入您的枝条延伸。您还应该在services.yml中进行标记。不在依赖注入扩展中。

http://symfony.com/doc/current/cookbook/templating/twig_extension.html

services.yml

fieldnames:
    class: Acme\DemoBundle\Extension\FieldnamesTwigExtension
    arguments:  [@helpers]
    tags:
        - { name: twig.extension }

并相应地调整其余代码。

答案 2 :(得分:1)

Extension \ FieldnamesTwigExtension.php 中,namespace指令后调用use是否正常?

Symfony返回哪个错误?