开发者

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

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

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 w开发者_如何学JAVAith 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜