Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - How to render a view from another controller

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.

like image 910
Milos Cuculovic Avatar asked Dec 26 '22 02:12

Milos Cuculovic


2 Answers

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

like image 95
Adam Chysky Avatar answered Jan 05 '23 11:01

Adam Chysky


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));
like image 24
Milos Cuculovic Avatar answered Jan 05 '23 09:01

Milos Cuculovic