Instead of extending the standard controller, I'd like to inject Twig into one of my classes.
Controller:
namespace Project\SomeBundle\Controller; use Twig_Environment as Environment; class SomeController { private $twig; public function __construct( Environment $twig ) { $this->twig = $twig; } public function indexAction() { return $this->twig->render( 'SomeBundle::template.html.twig', array() ); } }
and then in services.yml
I have the following:
project.controller.some: class: Project\SomeBundle\Controller\SomeController arguments: [ @twig ]
The error I'm getting is:
SomeController::__construct() must be an instance of Twig_Environment, none given
But I'm passing in @twig
via config
. I can't see what I'm doing wrong.
Edit:
Adding in the correct code - this is what fixed the problem:
// in `routing.yml` refer to the service you defined in `services.yml` project.controller.some project_website_home: pattern: / defaults: { _controller: project.controller.some:index }
Other tags are used to integrate your services into other systems. For a list of all the tags available in the core Symfony Framework, check out Built-in Symfony Service Tags. Each of these has a different effect on your service and many tags require additional arguments (beyond the name parameter). For most users, this is all you need to know.
In a Symfony bundle, call this method in the load () method of the bundle extension class: Tags on their own don't actually alter the functionality of your services in any way. But if you choose to, you can ask a container builder for a list of all services that were tagged with some specific tag.
If you enable autoconfigure, then some tags are automatically applied for you. That's true for the twig.extension tag: the container sees that your class extends AbstractExtension (or more accurately, that it implements ExtensionInterface) and adds the tag for you.
Services tagged with the twig.extension tag are collected during the initialization of TwigBundle and added to Twig as extensions. Other tags are used to integrate your services into other systems.
First of all, lets look at what is available in your service container:
λ php bin/console debug:container | grep twig twig Twig_Environment ... λ php bin/console debug:container | grep templa templating Symfony\Bundle\TwigBundle\TwigEngine ...
Now we would probably go for TwigEngine class (templating service) instead of Twig_Enviroment (twig service). You can find templating service under vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\TwigEngine.php
... class TwigEngine extends BaseEngine implements EngineInterface { ...
In this class you will find two methods render(..) and renderResponse(...), which means that the rest of your code should work fine with the below example. You will also see that TwigEngine injects twig service (Twig_Enviroment class) to construct it parent class BaseEngine. There fore there is no need to request twig service and your error requesting Twig_Environment should vanish.
So in your code You would do this like so:
# app/config/services.yml services: project.controller.some: class: Project\SomeBundle\Controller\SomeController arguments: ['@templating']
Your class
namespace Project\SomeBundle\Controller; use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Component\HttpFoundation\Response; class SomeController { private $templating; public function __construct(EngineInterface $templating) { $this->templating = $templating; } public function indexAction() { return $this->templating->render( 'SomeBundle::template.html.twig', array( ) ); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With