2

In my project I use the framework as Symfony2. I wanted to write a php function that redirect to a default page if a condition fails. so my function was like this.

public function validate($value){
    if($value==null){
        return  $this->redirect(
             $this->generateUrl('home_page', array("message"=>""))
        ); 
    }   
}

and I call the function like this.

$this->validate($value);

problem is redirect does not happens.
please guys, help me. I don't know why it is not redirecting. Thanks

1
  • Does this function is in your controller? Commented Jul 24, 2012 at 9:08

2 Answers 2

8

You'd have to call it like this:

$response = $this->validate($value);

if ($response instanceof \Symfony\Component\HttpFoundation\Response) {
    return $response;
}

Instead of returning a response you could throw a custom redirect exception and write a listener which would create a redirect response.

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

Comments

1

In order to redirect, the calling action should return a redirect response. Meaning, you should return validate()'s return.

return $this->validate($value);

2 Comments

You have to check what's returned. If validate() returns nothing you don't want to interrupt but continue executing your controller's code.
@Jakub Zalas ah yes, youre right. my bad. :$ I was too focused on why the code didn't redirect. +1 to you

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.