dongshu4755 2016-06-24 23:05
浏览 29
已采纳

Zend Framework v1 - 当我在控制器中使用header()时 - 重定向不起作用

when I use function header() in controller - redirect not work, is it normal in Zend?

Ps... But if I add exit() after header() - this work. And If I use $this->_redirect - is work.

  • 写回答

1条回答 默认 最新

  • dphs48626 2016-06-25 10:50
    关注

    Yeah, this is pretty normal.

    PHP frameworks like ZF1 typically create/expose a Response object that represents the response that your server-side application wants to send to the waiting (typically browser) client. It typically encapsulates the HTTP headers and body that you wish to send. ZF1 is probably overriding your manual header() call and replacing it with headers from the Response object. That's why your header() call appears effective when followed immediately by an exit(); you're not giving the framework a chance to use its own headers from the Response object.

    As you appear to be aware, ZF1 controllers expose a Redirector action helper that can set the desired headers on this Response object and immediately send the response. For example:

    // Get a reference to the redirector
    $redirector = $this->_helper->getHelper('Redirector');
    
    // To redirect to a given url
    $redirector->gotoUrl($url);
    
    // To redirect to a given module, controller, and action
    // $controller is optional, defaults to current controller
    // $module is optional, defaults to current module
    $redirector->gotoSimple($action, $controller, $module);
    
    // To redirect to a named route with specified parameters
    $redirector->gotoRoute($route, $params);
    

    To set HTTP response codes, as well, you can do something like:

    $redirector->setCode(301)->setGoToUrl('/some/url')->redirectAndExit();
    

    See docs for the Redirector action helper.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题