4

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.

3 Answers 3

8

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

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

1 Comment

Thank you, that's exactly the answer I found. And it works perfectly.
2

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));

Comments

0

redirectToRoute : Just a recap with current symfony versions (as of 2016/11/25 with v2.3+)

public function genericAction(Request $request)
{
    if ($this->evalSomething())
    {
        $request->getSession()->getFlashBag()
            ->add('warning', 'some.flash.message');
        $response = $this->redirectToRoute('app_index', [
            'flash_message' => $request->getSession()->getFlashBag(),
        ]);
    } else {
        //... other logic
    }
    return $response;
}

Comments

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.