It is too much repetitive work to call header()
and then die()
[?] every time a you need to redirect to another URL. That's why you probably have a function / method that looks like the following:
function redirect($url, $http_response_code = 302)
{
header("Location: ".$url, true, $http_response_code);
die;
}
Where does this method live in your projects / frameworks?
It doesn't fit in any category. Wherever I put it, it doesn't feel right. CodeIgniter (and Kohana) put inside of the url
helper class but again, it doesn't feel right (along with methods like site_url()
and base_url()
).
I personally keep it in a Response class (I have static class, contains helper functions like this one: redirect(), sendFile(), sendContent() etc).
If you do not have one -- then you may have Request class (dealing with all aspects of the request, e.g. isAjax(), isCLI(), isSecure(), getServerSoftware(), getClientIP() etc). It does not fit here 100% but something close.
In my humble opinion this code is too simple to write a function for it.
P.S. I think the default should be 303 or 307.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With