Symfony2 - How to render a view from another controller

10,204

Solution 1

I have found the solution, simply use the forward function by specifying the controller and the action nanme:

return $this->forward('MerrinMainBundle:Homepage:Index', array('flash_message'=>$flash_message));

Solution 2

I believe the checking should not be done in the Security controller. Right place in my opinion is a separate validator service or right in the entity which uses the email address.

But to your question, you can call another controller's action with $this->forward() method:

public function indexAction($name)
{
    $response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
        'name'  => $name,
        'color' => 'green',
    ));

    return $response;
}

The sample comes from symfony2 documentation on: http://symfony.com/doc/2.0/book/controller.html#forwarding

Share:
10,204
Milos Cuculovic
Author by

Milos Cuculovic

Masters degree in Embedded and comunicant systems.

Updated on June 04, 2022

Comments

  • Milos Cuculovic
    Milos Cuculovic almost 2 years

    I have two controllers, homepage and Security.

    In the homepage, I am displaying one view and in the security, I am doing some things, and one of them is the email address validation.

    What I would like is that when the email validation code is not valid, display the homepage with a flash message. For that, I will have to render the indexAction of the HomepageController, from the Security controller, by giving him as parameter the flash message.

    How can this be done? Can I render a route or an action from another controleller?

    Thank you in advance.