0
votes

I'm trying to render an action which is in a sub-namespaced controller from within a Twig view in Symfony 2.

The problem is: The render helper cannot find the controller/action because it's in a namespace below Controller.

This is my controller: src/Acme/Bundle/TestBundle/Controller/FirstModule/ExampleController.php

namespace Acme\Bundle\TestBundle\Controller\FirstModule;

class ExampleController extends Controller
{
    public function exampleAction(Request $request)
    {
        return $this->render('AcmeTestBundle:FirstModuleExample:example.html.twig');
    }

    public function embedAction(Request $request)
    {
        return $this->render('AcmeTestBundle:FirstModuleExample:embed.html.twig');
    }
}

This is my view: src/Acme/Bundle/TestBundle/Resources/views/FirstModuleExample/example.html.twig

{% render "AcmeTestBundle:Example:embed" %}
// or
{% render "Acme\Bundle\TestBundle\Controller\FirstModule\Example:example" %}
// or
{% render "Acme\Bundle\TestBundle\Controller\FirstModule\ExampleController:exampleAction" %}

I have read the Embedding Controllers documentation but no clue there how to handle Controllers that are in a sub-namespace.

Thanks.

3

3 Answers

4
votes

Either of these should work. Remember, backslashes in strings need to be escaped (i.e., doubled)

{% render "AcmeTestBundle:FirstModule\\Example:embed" %}

or

{% render "Acme\\Bundle\\TestBundle\\Controller\\FirstModule\\ExampleController::embedAction" %}
1
votes

Have you tried this?

{% render "AcmeTestBundle:FirstModule/Example:embed" %}

or this?

{% render "AcmeTestBundle:FirstModule\\Example:embed" %}
0
votes

I think you should be able to use backslash-notation but I haven't tried it since I practice to put all controllers into single namespace (which is bad if you have a lot of them).

Something like this:

{% render "Acme\Bundle\TestBundle\Controller\FirstModule\Example:example" %}