我在那裏我試圖注入路由器服務一根樹枝延伸,所以...... services.yml不可能路由器服務傳遞給樹枝延伸symfony的2.6
app.twig_extension:
class: SeoReportBundle\Twig\SeoReportExtension
arguments: [@router]
tags:
- { name: twig.extension }
擴展:
使用Symfony \ Bundle \ FrameworkBundle \ Routing \ Router;
class SeoReportExtension extends \Twig_Extension
{
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
當我執行的代碼,我得到這個錯誤:
Catchable Fatal Error: Argument 1 passed to SeoReportBundle\Twig\SeoReportExtension::__construct() must be an instance of Symfony\Bundle\FrameworkBundle\Routing\Router, none given
嘗試調試這一點,我沒有這種變化中的代碼,試圖瞭解發生了什麼事情:
public function __construct($router) {
var_dump($router);die();
$this->router = $router; }
所以我刪除了類型提示,然後我做了一個對象轉儲,以查看構造方法中獲得的內容。令我驚訝的是,這是輸出:
Object(Symfony\Bundle\FrameworkBundle\Routing\Router)[198]
private 'container' =>
object(appDevDebugProjectContainer)[219]
private 'parameters' => ...
所以......它實際上傳遞了一個路由器對象。那麼...爲什麼我會得到這個錯誤?我在這一點上完全喪失
--- UPDATE --- 這是我迄今發現: - 在/app/cache/dev/appDevDebugProjectContainer.php有在其中創建擴展兩個地方:
protected function getApp_TwigExtensionService()
{
return $this->services['app.twig_extension'] = new \SeoReportBundle\Twig\SeoReportExtension($this->get('router'));
}
哪個是正確的,它通過路由器構建 而且......
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());
現在,這一個不通過路由器。但它確實將某些依賴關係傳遞給其他擴展。因此,必須有可供\Twig_SimpleFunction
實例,以及以同樣的方式\Twig_SimpleFilter
一個原因,礦井延伸工作不
只是咧嘴笑,拿出大衆:虛假的屬性。您可以使用public:false來忽略此文檔。 – Cerad 2015-02-23 15:10:17
完成!問題依然存在:) – petekaner 2015-02-23 15:17:30
刪除die();也許構建被稱爲第二次沒有路由器。 – gries 2015-02-23 15:27:10