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

Question with INVO sample application - understanding Redirection

Hi there,

I've posted a few questions about redirection that have occured in small applications I have been creating using Phalcon. I've been looking at the INVO sample application and am confused by the behaviour if I alter some code. In SessionController, startAction, on a successful login, the code forwards to 'invoices/index' but the browser address does not reflect this change. If I replace the forward with a redirect like:

$this->response->redirect('invoices/index', true, 302);

I get a browser address of "https://localhost:8888/invo/session/invoices/index" and a Phalcon error of "Action 'invoices' was not found on handler 'session'"

I'm using latest Phalcon on the latest MAMP. Just to be clear, I understand why Phalcon results in an error. I just don't understand why doing an external redirect results in the funky browser address/url of "https://localhost:8888/invo/session/invoices/index"?

Is this the correct, expected behaviour and if so, why? I've pasted the altered code below. Any comments much appreciate! :)

    /**
     * This actions receive the input from the login form
     *
     */
    public function startAction()
    {
        if ($this->request->isPost()) {
            $email = $this->request->getPost('email', 'email');

            $password = $this->request->getPost('password');
            $password = sha1($password);

            $user = Users::findFirst("email='$email' AND password='$password' AND active='Y'");
            if ($user != false) {
                $this->_registerSession($user);
                $this->flash->success('Welcome ' . $user->name);
                //return $this->forward('invoices/index');          // Works but doesn't change browser address
                //$this->response->redirect('invoices/index');  // Works and does change browser address

                // The following creates browser address: https://localhost:8888/invo/session/invoices/index
                // Renders the error: Action 'invoices' was not found on handler 'session'
                $this->response->redirect('invoices/index', true, 302); 
            }

            $username = $this->request->getPost('email', 'alphanum');
            $user = Users::findFirst("username='$username' AND password='$password' AND active='Y'");
            if ($user != false) {
                $this->_registerSession($user);
                $this->flash->success('Welcome ' . $user->name);
                return $this->forward('invoices/index');
            }

            $this->flash->error('Wrong email/password');
        }

        return $this->forward('session/index');
    }

I think it's undefined behaviour. You're saying it's an external redirect, but you're passing a URL internal to the application. Just call

$this->response->redirect('invoices/index');


38.8k

@quasipickle - thanks very much for your comments. It seemed odd to see the "confused" browser address value and I wondered whether there was in fact an explanation for it. What you are saying sounds reasonable :) Cheers.