嘿所有我想要呈现一根树枝模板在我的控制器,而是我得到这个错误: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