每次需要重定向到另一个URL时,调用太多重复工作header()然后[?].这就是为什么你可能有一个如下所示的函数/方法:die()
function redirect($url, $http_response_code = 302)
{
header("Location: ".$url, true, $http_response_code);
die;
}
Run Code Online (Sandbox Code Playgroud)
这个方法在你的项目/框架中存在于哪里?
它不适合任何类别.无论我把它放在哪里,都感觉不对.笨(Kohana的和)把内部url辅助类,但再次,它感觉不对(用类似的方法一起site_url()和base_url()).
我个人把它保存在一个Response类中(我有静态类,包含像这样的辅助函数:redirect(),sendFile(),sendContent()等).
如果你没有 - 那么你可能有Request类(处理请求的所有方面,例如isAjax(),isCLI(),isSecure(),getServerSoftware(),getClientIP()等).它不适合这里100%,但接近一些.