It's 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);


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()).

Accepted Answer

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.

Written by LazyOne
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the community.
It is licensed under cc-wiki