0

I have a question: how can 1 view/page use 2 controllers to display different data?

For example:

1) Page displays DVD's information using DvdController.

2) On that same page I want to use a Template/Partial view, that displays list of actors, and that list should come from the second ActorsController.

I can add a template view inside another page but the second controller doesn't work:

MainPage on URL: website/dvd

{% block body %}
    <div>
        <h1>{{ dvd.title }} </h1>
    </div>

    <div>
            {{ include('DVDBundle:Actors:actors.html.twig') }}
    </div>

{% endblock %}

Above I use the partial view actors.html.twig to show the actors, strangely the html code/div's and so on are actually displayed and working on the page, however the controllers(ActorsController) method that meant to return this partial view is not executed for some reason.?

But if I go to the view directly via link: website/actors then its working.

1 Answer 1

2

The Symfony documentation on embedded controllers explains this nicely.

In short, you will need to do something like this in your template file:

<div>
    {{ render(controller(
        'DVDBundle:Actors:actors',
        { ... parameters to the controller action here ... }
    )) }}
</div>

The important thing is calling the render function, which allows a controller to be rendered, as opposed to simply includeing a template. (This assumes that your have an ActorsController with an actorsAction method; change the names as appropriate to your controller.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You - Another thing to clarify: if the partial view contains a form, the Request returns an error, something related to fragment and for bitten permissions. going directly to the form url works but via partial view it doesnt. I also found this comment: but its not clear what to do?: "When using a controller instead of a URL, you must enable the Symfony fragments configuration:"
Maybe use this link now symfony.com/doc/3.4/templating/embedding_controllers.html (Symfony 3.4)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.