嘿所有我想要呈現一根樹枝模板在我的控制器,而是我得到這個錯誤:Symfony2的枝杈渲染錯誤
Error: Call to a member function has() on a non-object
我跟着幾所示例上線,並根據這些我要細,很遺憾我想我做錯了什麼。
我的控制器代碼:
<?php
namespace Tomazi\SiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Tomazi\SiteBundle\Factories\FactoryHomePage;
class IndexController extends Controller
{
public $home;
public function __construct(
FactoryHomePage $home
){
$this->home = $home;
}
public function indexPageAction()
{
return $this->render('pages/home.html.twig', [
'name' => 'Tomazi'
]);
}
}
所以我的樹枝文件src的應用程序/資源/在這個文件視圖/ home.html.twig
我:
<h1>Hey {{ name }} </h1>
爲什麼我得到這個錯誤信息?
更新1 OK,所以我的控制器被宣佈爲一個服務,這並沒有讓我用樹枝容器我相信....
然後我回去,並配置了我的控制器只是一個像這樣的控制器:
<?php
namespace Tomazi\SiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class IndexController extends Controller
{
public function indexPageAction()
{
return $this->render('pages/home.html.twig', array('name' => 'Tomazi'));
}
}
我當然也必須改變我的路由,所以它不期望一個服務,但只是一個控制器。
經過這些更改,我能夠呈現我的home.html.twig
,但是這並不滿足我,因爲我真的希望我的控制器是一項服務,因爲我想將我的工廠注入到它中,以準備Controller to Respond的所有數據,我喜歡薄控制器的方法...
要真正實現我的目標,我不得不一個service_container參數添加到我的控制器服務,像這樣:
<!-- Controller -->
<service id="tomazi.sitebundle.controller.index"
class="Tomazi\SiteBundle\Controller\IndexController">
<argument type="service" id="tomazi.sitebundle.factory.index" />
**<argument type="service" id="service_container" />**
</service>
現在我的控制器的最終版本是這樣的:
<?php
namespace Tomazi\SiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Tomazi\SiteBundle\Factories\FactoryHomePage;
class IndexController extends Controller
{
public $home;
public $container;
public function __construct(
FactoryHomePage $factoryHomePage,
ContainerInterface $containerInterface
){
$this->home = $factoryHomePage;
$this->container = $containerInterface;
}
public function indexPageAction()
{
return $this->render('pages/home.html.twig', array('name' => 'Tomazi'));
}
}
不確定這是否是最好的方法,但它對我有用。
也許你可以用基本佈局顯示完整的模板代碼?它看起來像是這樣的地方:app.request.has('any_var')。但我們需要更多的細節來定義問題(可能是來自錯誤跟蹤的更多細節) – Dmitry
那麼我甚至不擴展我的基本模板,而我的基本模板是Symfony生成的 – John