无法将路由器服务传递到symfony 2.6中的树枝扩展 [英] Impossible to pass the router service to a twig extension in symfony 2.6
问题描述
我有一个小树枝扩展名,试图插入路由器服务,所以...
services.yml
app.twig_extension:
类:SeoReportBundle\Twig\SeoReportExtension
参数:[@router]
标签:
-{名称:twig.extension}
扩展名:
使用Symfony\捆绑包\FrameworkBundle\Routing\Router;
class SeoReportExtension扩展\Twig_Extension
{
private $ router;
公共功能__construct(Router $ router)
{
$ this-> router = $ router;
}
执行代码时出现此错误:
可捕获的致命错误:传递给SeoReportBundle\Twig\SeoReportExtension :: __ construct()的参数1必须是Symfony\Bundle\FrameworkBundle\的实例路由\路由器,没有给出
尝试调试此代码,我在代码中进行了更改,以了解发生了什么事情:
公共函数__construct($ router){
var_dump($ router); die();
$ this-> router = $ router; }
所以我删除了类型提示,并转储了该对象以查看得到的内容构造方法。令我惊讶的是,这是输出:
Object(Symfony\Bundle\FrameworkBundle\Routing\Router)[ 198]
私人容器 =>
object(appDevDebugProjectContainer)[219]
私人参数 => ...
所以...它实际上传递了一个路由器对象。所以...为什么我得到错误?此时我完全迷失了
---更新---
到目前为止,我发现的是:
-在/ app / cache / dev / appDevDebugProjectContainer.php在两个地方创建了扩展:
受保护的函数getApp_TwigExtensionService()
{
return $ this-> services ['app.twig_extension'] = new \SeoReportBundle\Twig\SeoReportExtension($ this-> get('router'));
}
这是正确的,它将路由器传递给构造
并且...
受保护的函数getTwigService()
{
$ this-> services ['twig '] = $ instance = new wigTwig_Environment($ this-> get('twig.loader'),array('debug'=> true,'strict_variables'=> true,'exception_controller'=>' twig.controller.exception:showAction','form_themes'=> array(0 =>'form_div_layout.html.twig'),'autoescape'=> array(0 =>'Symfony\\Bundle\ wigTwigBundle\\TwigDefaultEscapingStrategy',1 =>'guess'),'cache'=>(__DIR __。'/ twig'),'charset'=>'UTF-8','paths' => array()));
$ instance-> addExtension($ this-> get(’app.twig_extension'));
...
$ instance-> addExtension(new \Symfony\Bundle\AsseticBundle\Twig\AsseticExtension($ this-> get('assetic.asset_factory'),$ this-> get('templating.name_parser'),true,array(),array(0 =>'SeoReportBundle'),new \Symfony\Bundle\AsseticBundle\DefaultValueSupplier($ this)));
$ instance-> addExtension(new \Doctrine\Bundle\DoctrineBundle\Twig\DoctrineExtension());
$ instance-> addExtension(new \SeoReportBundle\Twig\SeoReportExtension());
现在,这个没有通过路由器。但是它确实将某些依赖项传递给其他扩展。因此,必须有一个原因,导致我的扩展无法正常工作
Twig环境可用于 wigTwig_SimpleFunction
实例以及 \Twig_SimpleFilter
实例,尽管Twig文档仅针对过滤器显示。在环境中,您可以像这样获得路由扩展:
/ **
* {@inheritdoc}
* /
公共函数getFunctions():array
{
return array(
new \Twig_SimpleFunction('get_my_link_thing',array($ this,'getMyLinkThing') ,array('needs_environment'=> true)),
);
}
/ **
*使用生成的链接获取内容。
*
* @返回字符串
* /
公共函数getMyLinkThing(\Twig_Environment $ env):字符串
{
$ targetUrl = $ env- > getExtension('routing')-> getPath('my_route_name');
return sprintf(链接为< a href =%s>此处< / a>!,$ targetUrl);
}
$还有很多其他漂亮的东西env
-值得尝试 dump(get_class_methods($ env));
,然后转储在那里调用任何东西的结果,这会引起您的幻想。 / p>
I have a twig extension where I'm trying to inject the router service, so... services.yml
app.twig_extension:
class: SeoReportBundle\Twig\SeoReportExtension
arguments: [@router]
tags:
- { name: twig.extension }
Extension:
use Symfony\Bundle\FrameworkBundle\Routing\Router;
class SeoReportExtension extends \Twig_Extension
{
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
When I execute the code I get this error:
Catchable Fatal Error: Argument 1 passed to SeoReportBundle\Twig\SeoReportExtension::__construct() must be an instance of Symfony\Bundle\FrameworkBundle\Routing\Router, none given
Trying to debug this, I did this change in the code to try to understand what's going on:
public function __construct($router) {
var_dump($router);die();
$this->router = $router; }
So I remove the type hint and I do a dump of the object to see what's getting in the construct method. To my surprise, this is the output:
Object(Symfony\Bundle\FrameworkBundle\Routing\Router)[198]
private 'container' =>
object(appDevDebugProjectContainer)[219]
private 'parameters' => ...
So... it actually passes a router object. So... why am I getting the error? I completely lost at this point
--- UPDATE --- Here's what I found so far: - In /app/cache/dev/appDevDebugProjectContainer.php there are two places where the extension is created:
protected function getApp_TwigExtensionService()
{
return $this->services['app.twig_extension'] = new \SeoReportBundle\Twig\SeoReportExtension($this->get('router'));
}
Which is correct, it passes the router to the construct And...
protected function getTwigService()
{
$this->services['twig'] = $instance = new \Twig_Environment($this->get('twig.loader'), array('debug' => true, 'strict_variables' => true, 'exception_controller' => 'twig.controller.exception:showAction', 'form_themes' => array(0 => 'form_div_layout.html.twig'), 'autoescape' => array(0 => 'Symfony\\Bundle\\TwigBundle\\TwigDefaultEscapingStrategy', 1 => 'guess'), 'cache' => (__DIR__.'/twig'), 'charset' => 'UTF-8', 'paths' => array()));
$instance->addExtension($this->get('app.twig_extension'));
...
$instance->addExtension(new \Symfony\Bundle\AsseticBundle\Twig\AsseticExtension($this->get('assetic.asset_factory'), $this->get('templating.name_parser'), true, array(), array(0 => 'SeoReportBundle'), new \Symfony\Bundle\AsseticBundle\DefaultValueSupplier($this)));
$instance->addExtension(new \Doctrine\Bundle\DoctrineBundle\Twig\DoctrineExtension());
$instance->addExtension(new \SeoReportBundle\Twig\SeoReportExtension());
Now, this one doesn't pass the router. But it does pass some dependencies to other extensions. So there has to be a reason why mine extension is not working
The Twig environment is available to \Twig_SimpleFunction
instances as well as \Twig_SimpleFilter
in the same way, though the Twig docs only show it for filters. From the environment, you can get the routing extension like this:
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return array(
new \Twig_SimpleFunction('get_my_link_thing', array($this, 'getMyLinkThing'), array('needs_environment' => true)),
);
}
/**
* Get something with a generated link.
*
* @return string
*/
public function getMyLinkThing(\Twig_Environment $env): string
{
$targetUrl = $env->getExtension('routing')->getPath('my_route_name');
return sprintf('Link is <a href="%s">here</a>!', $targetUrl);
}
There are lots of other nifty things in that $env
- it's worth trying dump(get_class_methods($env));
and then dumping the results of calling anything in there that strikes your fancy.
这篇关于无法将路由器服务传递到symfony 2.6中的树枝扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!