We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

redirect problems

Hi, I'm studying the response->redirect; and I encounter 2 strange results:

  1. validation - after redirection, seem like it's going to validate one more time. For example I insert a row that contains unique value, after the redirection it throws me an error. This problem will not occur for forward.
  2. flash session message - similar to validation, a flash message will output twice, and doesn't happen when using forward.

Have any of you encounter this problem?

Use

return $this->response->redirect();

Instead of just

$this->response->redirect();


25.7k

Thanks for the reply Pavel, however it doesn't work. The strange thing is the same code (haven't change anything, no return) magically produce different result:

  1. validation didn't work yesterday works today.
  2. flash session message still output twice even with return.

I think it need more complex code review to get a problem



25.7k

they're the simplest code.

    public function testAction(){
        $systemUser           = new SystemUsers();
        $systemUser->username = 'un1'; // this column is unique
        if(!$systemUser->save()){
            foreach($systemUser->getMessages() as $message){
                $this->flash->error((string) $message);
            }
            $this->view->disable();
            $this->response->redirect('test/fail');
            return;
        }else{
            return $this->forward('test/success');
        }
    }

note: apparently the same action (testAction) is executed twice (it happens that it executed four times).