2014-04-24 90 views
0

我試圖設置一個樹枝過濾器這樣工作:{{entities | fieldnames}}將返回一個包含實體對象的屬性名稱的數組。 我的問題,閱讀並嘗試幾個小時後,我不能執行Twig Extension php中的$ this-> container-> get(「helpers」)。看起來我沒有正確地鏈接服務容器......請幫忙;)從Symfony2的樹枝擴展中使用幫助器服務

錯誤:調用成員函數get()在/ Users/a77/Dropbox/06中的非對象.Proyectos/2011的U Vox的/ DEV U形Vox的/ UVOX網絡/ SRC/Acme公司/ DemoBundle /擴展/ FieldnamesTwigExtension.php線38

或者,如果構建體而不= NULL 錯誤 ContextErrorException:捕致命錯誤:傳遞給Acme \ DemoBundle \ Extension \ FieldnamesTwigExtension :: __ construct()的參數1必須是Acme \ DemoBundle \ Extension \ Container的一個實例,沒有給出,在

services.yml

services:

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

namespace Acme\DemoBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Definition; // Added

class AcmeDemoExtension extends 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'; 
} 

} 
+0

如果你想使這個工作,你應該把「命名空間」放在「使用」之前,刪除「定義」行,並最終更新getFieldnames公共函數getFieldnames($記錄){反射=新\ ReflectionClass $記錄); $ props = $ reflect-> getProperties(); $ fieldnames = Array(); foreach($ props as $ prop){ $ fieldnames [] = $ prop-> getName(); } return $ fieldnames; } –

回答

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 \資源\ CONFIG \ services.yml

更換此行:

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

通過:

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

你打敗了我,雖然我認爲只是注入幫手而不是整個容器是一個稍微好一點的方法。 – Cerad

+0

雖然這兩個編輯,但仍然沒有運氣,但我有一個新的錯誤: ContextErrorException:可捕獲的致命錯誤:參數1傳遞給Acme \ DemoBundle \ Extension \ FieldnamesTwigExtension :: __ construct()必須是Acme \ DemoBundle \ Extension \ Container,給出的appDevDebugProjectContainer實例,在第718行的/Users/a77/Dropbox/06.Proyectos/2011 U-Vox/DEV U-Vox/Uvox Web/app/cache/dev/appDevDebugProjectContainer.php中調用, /Users/a77/Dropbox/06.Proyectos/2011 U-Vox/DEV U-Vox/Uvox Web/src/Acme/DemoBundle/Extension/FieldnamesTwigExtension.php line 23 –

+0

剛剛改變了命名空間和使用的順序,看到對象返回。我幾分鐘後回來......感謝兩位! –

1

在你擴展\ FieldnamesTwigExtension.php,這是正常的,你的namespaceuse指令後,叫什麼名字?

Symfony返回哪個錯誤?

+0

現在在原始帖子上添加了錯誤。謝謝。不幸的是,改變命名空間和使用的順序並沒有解決問題... –

+0

嗯,它是解決方案的一大部分 –

2

你正在這樣太難。你應該直接將助手注射到你的樹枝裏。你也應該在services.yml中做你的標記。不在依賴注入擴展中。

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

服務。yml

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

並相應地調整其餘代碼。

+0

我打算增強我的代碼使用是隻有所需的服務,而不是完整的容器......但沒有任何方法工作 –