Php Symfony未加载运行时细枝扩展
我按照上的示例在运行时加载一个细枝扩展,但它没有加载:我做错了什么 IN:src/PlotlyBundle/Twig/AppRuntime.phpPhp Symfony未加载运行时细枝扩展,php,symfony,twig,Php,Symfony,Twig,我按照上的示例在运行时加载一个细枝扩展,但它没有加载:我做错了什么 IN:src/PlotlyBundle/Twig/AppRuntime.php <?php namespace PlotlyBundle\Twig; class AppRuntime { public function __construct() { } public function biDraw() { return 'awesome text here';
<?php
namespace PlotlyBundle\Twig;
class AppRuntime
{
public function __construct()
{
}
public function biDraw()
{
return 'awesome text here';
}
}
<?php
namespace PlotlyBundle\Twig;
use PlotlyBundle\Twig\AppRuntime;
class AppExtension extends \Twig_Extension
{
public function getFunctions()
{
return [
new \Twig_SimpleFunction(
'bi_draw',
array(AppRuntime::class, 'biDraw')
),
];
}
}
$twig = $this->get('plotly.twig_runtime');
return $this->render(
'dashboard/index.html.twig'
);
IN:src/PlotlyBundle/Twig/AppExtension.php
<?php
namespace PlotlyBundle\Twig;
class AppRuntime
{
public function __construct()
{
}
public function biDraw()
{
return 'awesome text here';
}
}
<?php
namespace PlotlyBundle\Twig;
use PlotlyBundle\Twig\AppRuntime;
class AppExtension extends \Twig_Extension
{
public function getFunctions()
{
return [
new \Twig_SimpleFunction(
'bi_draw',
array(AppRuntime::class, 'biDraw')
),
];
}
}
$twig = $this->get('plotly.twig_runtime');
return $this->render(
'dashboard/index.html.twig'
);
IN:app/Resources/views/dashboard/index.html.twig
{{ bi_draw() }}
感谢@Federkun的评论,我通过自动连接细枝扩展来修复它: IN:src/PlotlyBundle/Resources/config/services.yml
services:
plotly.twig_runtime:
class: PlotlyBundle\Twig\AppRuntime
public: true
tags:
- { name: twig.runtime }
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# this creates a service per class whose id is the fully-qualified class name
PlotlyBundle\Twig\:
resource: '../../../../src/PlotlyBundle/Twig/*'
tags:
- { name: twig.runtime }
Symfony docs()上的示例需要更新,以说明必须启用自动布线(如中所述)才能使示例工作
我向Symfony docs提交了一份报告。这感觉不对。。文档不是错了吗?您可以尝试使用
类:PlotlyBundle\Twig\AppExtension
吗?^不,没有错--您是自动接线的吗?否则,您需要用twig.extension
标记它,我已经尝试过了:当使用xdebug时,我看到构造函数被调用了,但是getFunctions没有被调用。。我应该向我的bundle(PlotlyBundle)添加依赖项注入吗?我在服务中添加了标记。ymlc你能用它更新你的帖子吗?是否已启用服务容器的自动配置选项?()将Twig
子名称空间的所有服务标记为Twig.runtime
不是正确的解决方案。这些服务中的大多数不是运行时,而是扩展。它解决您的问题的原因是,您现在正在将扩展注册为服务(并由于自动配置而标记为扩展),这不是在原始配置中完成的。