1
1
1alexandr2015-05-08 18:37:43
symfony
1alexandr, 2015-05-08 18:37:43

How to get a renderView in a service that doesn't inherit from the Controller class?

I have a service in which I need to use the renderView() of the Controller class. What do I need to inject to use this method in my service? preferably with an example, thanks)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
BoShurik, 2015-05-08
@1alexandr

class SomeService
    /**
     * @var \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface
     */
    private $templating;

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

    public function doSome()
    {
         $template = $this->templating->render('AppBundle::foo.html.twig', array(
             'foo' => 'bar'
         ));
    }

services:
    app.service.foo:
        class: %app.service.foo.class%
        arguments:
            - @templating

A
Alex, 2015-05-08
@shoomyst

If this is necessary, then something is wrong in the application architecture.
If you really need it, the controller becomes a service and is injected through DIC

1
1Michael1, 2015-05-08
@1Michael1

I would stop only on the first suggestion of Alex T.
> If this is necessary, then something is wrong in the application architecture.

V
Vladimir Balin, 2016-09-29
@krocos

$this->container->get('templating')->render('...');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question