我試圖設置一個樹枝過濾器這樣工作:{{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'; } }
如果你想使這個工作,你應該把「命名空間」放在「使用」之前,刪除「定義」行,並最終更新getFieldnames公共函數getFieldnames($記錄){反射=新\ ReflectionClass $記錄); $ props = $ reflect-> getProperties(); $ fieldnames = Array(); foreach($ props as $ prop){ $ fieldnames [] = $ prop-> getName(); } return $ fieldnames; } –