4

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

Community
  • 1
  • 1
Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201

4 Answers4

5

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.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • Did you develop these classes or are they a part of a publicly available framework / library? – Emanuil Rusev Jun 19 '11 at 15:39
  • In my case these are my own classes (I looked though quite a few frameworks, investigating how they work etc and these are compilation of useful functions that I actually use (written myself or adopted to my needs, not just copy-paste)). But I think Kohana (don't remember which version though) and/or Yii have such separate classes. – LazyOne Jun 19 '11 at 15:44
  • What does `sendContent()` do? – Emanuil Rusev Jun 19 '11 at 16:52
  • sendContent() and sendFile() are similar; they both made to send downloadable content (like, PDF, XLS or CSV file) so that on user end browser will ask/save the file on a disk instead of opening directly in browser. Useful when generating PDF Invoices, History of orders in Excel format etc. 2 methods: one sends data you generated in a script (you provide appropriate mime type, file name etc) while second sends already existing file (may detect/guess mime type based on file extension etc). They are very simple in my implementation -- I have seen much more advanced implementations elsewhere. – LazyOne Jun 19 '11 at 17:08
4

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.

Michas
  • 8,534
  • 6
  • 38
  • 62
  • The code is simple but what it does is important and allow a mistake in it can be a significant problem (i.e. omitting the die() after the header()). – Emanuil Rusev Jun 19 '11 at 16:18
  • On the other hand. This code is so important, so extraordinary, it shouldn't be hidden in any layer of abstraction. Well, it should work without `die()` or `exit()` too. – Michas Jun 19 '11 at 18:08
3

Certainly it should be in your Response class of your framework. If you use a (single) front controller -my case- you have such method in the front controller and so it can be called from anywhere.

Update: Your front controller script handles all requests (or most of them). The basic structure is:

<?
// include you libraries
// few common functions
// get the request parameters
// do some common work
// include specific scripts to perform job depending on Url
?>

For example, common work has to do with the ability to control security permissions for all URLs in the application in one single spot, logging, database connectivity, etc. Then you delegate the detailed work to specific scripts depending on the URL. Ok, where to put the redirect method? If you put it in any of the common libraries included in the first part of the script or in common functions it should be available to any method being called afterwards, particularly in the scripts that handle specific URLs. Hope this clarifies.

I haven't read all the details, but this link may help.

carlosayam
  • 1,396
  • 1
  • 10
  • 15
1

I usually put it and some other miscellaneous functions in a file called lib.php or misc.php in the root of my include directories. It may not be the most explicit location but I always include the file in my Controller with a comment explaining what it is.

EDIT:

A few other methods that end up in that file for me are a few helper functions that I find myself using frequently such as:

function def_value($arr, $k, $d = false){
    return array_key_exists($k,$arr) ? $arr[$k] : $d;
}

They are usually very generic methods that I use in my framework but I don't want to require them in every file. Occasionally I will include the redirect method as a static method in my Controller class as well.

GWW
  • 43,129
  • 11
  • 115
  • 108