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

beforeNotFoundAction redirecting

Can anyone tell me how can you do a redirect from this event? I cannot do that.



98.9k

This way:

public function beforeNotFoundAction()
{
    $this->response->redirect("location");
    return false;
}


33.8k

Ah, my bad, I haven't realized that I did the wrong question. What I want is to redirect, but before that, use setStatusCode(). The problem is that, even with setNotModified(), the statusCode is deleted.



98.9k
Accepted
answer

Phalcon\Http\Response::redirect automatically sets up a 301/302 status code which is the right way to perform a redirection: https://en.wikipedia.org/wiki/HTTP_301

You can pass the status code in the third parameter:

$response->redirect("https://www.example.com/new-location", true, 304);

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/http/response.zep#L333



33.8k

Ah well, nevermind. I forgotted that I was redirecting, so setting a 404 doesn't makes sense. Thanks.