Reputation: 1665
Hey all I am trying to render a twig template in my controller but instead i get this error:
Error: Call to a member function has() on a non-object
I followed few exampled on line and according to these I should be fine, unfortunately I guess I am doing something wrong.
My controller code:
<?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'
]);
}
}
So my twig file src is app/Resources/view/home.html.twig
in this file I have:
<h1>Hey {{ name }} </h1>
Why do i get this error message?
Update 1 OK so my controller was declared as a service, this did not allowed me to use twig container i belief....
I then went back and configured my controller to be just a Controller like so:
<?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'));
}
}
I also had to of course change my routing so it does not expect a service but just a controller.
After these changes I was able render my home.html.twig
, But this not satisfied me as I really wanted my Controller to be a service because I wanted to inject my Factories into it that will prepare all the data for the Controller to Respond and I like THIN controller approach...
To actually achieve my goal I had to add a service_container argument to my Controller service like so:
<!-- 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>
Now my final version of a controller looks like this:
<?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'));
}
}
Not to sure if this is the best approach but it does the trick for me.
Upvotes: 2
Views: 1293
Reputation: 1665
OMG the reason why It was not working because my Controller was a service..... :@, I changed it so that my Controller is standard bang it works..... But I want my Controller to be a Service..... so how can I find a way around it...? is is possible to create Twig container as a service and inject it into my Controller Service?
Upvotes: 1
Reputation: 51
Could you try this code ?
public function indexPageAction()
{
return $this->render('pages/home.html.twig', array(
'name' => 'Tomazi'
));
}
Upvotes: 0