Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony redirect in a function

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

like image 243
Gihan De Silva Avatar asked Jul 13 '26 14:07

Gihan De Silva


2 Answers

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.

like image 80
Jakub Zalas Avatar answered Jul 15 '26 04:07

Jakub Zalas


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

return $this->validate($value);
like image 33
Czar Pino Avatar answered Jul 15 '26 05:07

Czar Pino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!