doujiu7704
2014-04-30 13:53
浏览 44
已采纳

如何停止在PHP中运行的脚本

i am running a script in Zend Framework 2, the issue is how to stop the script from proceeding if certain conditions are not met

below is my function, you will note that i attempted to redirect the script if the condition of willMemberAcceptMessage() was not met

if(!$this->willMemberAcceptMessage())
        {  
            $this->flashMessenger()->addMessage(static::ERROR_UNABLE_TO_PROCEED);
            $this->redirect()->toUrl($url);
        } 

the problem is that the script does not redirect straight away, it still keeps running.

what should i do?

图片转代码服务由CSDN问答提供 功能建议

我在 Zend Framework 2 中运行脚本,问题是如何停止 如果不符合某些条件,则继续执行脚本

下面是我的函数,您将注意到,如果 willMemberAcceptMessage()的条件是,我试图重定向脚本 未满足

  if(!$ this-> willMemberAcceptMessage())
 {
 $ this-> flashMessenger() - > addMessage(static :: ERROR_UNABLE_TO_PROCEED  ); 
 $ this-> redirect() - > toUrl($ url); 
} 
   
 
 

问题是脚本没有重定向 它会立即继续运行。

我该怎么办?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douji8033 2014-04-30 14:01
    已采纳

    Actually the correct approach is to return the result of the redirect helper call, as each of its methods return the response object:

    if (!$this->willMemberAcceptMessage()) {  
        $this->flashMessenger()->addMessage(static::ERROR_UNABLE_TO_PROCEED);
        return $this->redirect()->toUrl($url);
    } 
    

    You should avoid exit; calls if possible, and in this case, if you exit no redirect will happen.

    已采纳该答案
    打赏 评论
  • doumangzhen7204 2014-04-30 13:56

    add exit after redirect line

    if(!$this->willMemberAcceptMessage())
            {  
                $this->flashMessenger()->addMessage(static::ERROR_UNABLE_TO_PROCEED);
                $this->redirect()->toUrl($url);
                exit;
    
            } 
    
    打赏 评论

相关推荐 更多相似问题