Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you keep your "redirect" method? [closed]

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

like image 624
Emanuil Rusev Avatar asked Dec 12 '22 12:12

Emanuil Rusev


2 Answers

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.

like image 79
LazyOne Avatar answered Dec 15 '22 01:12

LazyOne


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.

like image 42
Michas Avatar answered Dec 15 '22 02:12

Michas