Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

symfony - Injecting Twig as a service in Symfony2

Instead of extending the standard controller, I'd like to inject Twig into one of my classes.

Controller:

namespace ProjectSomeBundleController;

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: ProjectSomeBundleControllerSomeController
    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 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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                                                           SymfonyBundleTwigBundleTwigEngine
  ...

Now we would probably go for TwigEngine class (templating service) instead of Twig_Enviroment (twig service). You can find templating service under vendorsymfonysymfonysrcSymfonyBundleTwigBundleTwigEngine.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: ProjectSomeBundleControllerSomeController
        arguments: ['@templating']

Your class

namespace ProjectSomeBundleController;

use SymfonyBundleFrameworkBundleTemplatingEngineInterface;
use SymfonyComponentHttpFoundationResponse;

class SomeController
{
    private $templating;

    public function __construct(EngineInterface $templating)
    {
        $this->templating = $templating;
    }

    public function indexAction()
    {
        return $this->templating->render(
            'SomeBundle::template.html.twig',
            array(

            )
        );
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...