Php 如何在Kohana中停止执行请求?

Php 如何在Kohana中停止执行请求?,php,exception-handling,request,kohana-3,Php,Exception Handling,Request,Kohana 3,假设我有一个控制器模板,它有一个before函数,就像这样 public function before() { parent::before(); if ($this->request === Request::instance()) { // its a main request, throw an exception or redirect Request::instance()->redirect('/'

假设我有一个控制器模板,它有一个before函数,就像这样

public function before()
  {
     parent::before();
     if ($this->request === Request::instance()) 
     {
         // its a main request, throw an exception or redirect
         Request::instance()->redirect('/');
     }
     else
     {
        // ok
     }
  }
但是假设我不想重定向,我想停止请求流,什么也不做

例外情况会这样做吗?有没有一种简单的方法,比如
Request::die()

实际上我不想停止请求流,只是阻止这个控制器做任何事情。很可能这个控制器是从另一个控制器调用的,我想将该控件传递回调用的控制器。”


谢谢

您可以在
before()中设置类变量,例如:

$this->execute = false;
然后在你的行动中:

public function action_example()
{
    if (!$this->execute) return;
    // etc
}
1.使用异常(尚未测试):

  • 更改操作名称:

    $this->request->action('oblivion');//重定向到不做任何事情的“遗忘”操作


  • 常规的
    die()
    有什么问题吗?“什么也不做”,如“完全停止,什么都不显示”?就像Tesserex说的,
    die
    一点问题都没有。所以在if语句中,您只需将die()?如果在调用此子控制器之后还有其他子控制器调用,该怎么办?它只会停止此控制器的操作,还是停止整个脚本?我只想停止这个控制器,但如果有控制器,我会将控制权传递回调用控制器。除了其他线程之外,die将完全停止所有线程的执行。我不知道Kohana是否是多线程的,但我有点怀疑。因此,这大致相当于抛出一个致命错误;现在,我只是在调用一个假动作,但我想使用异常,所以我选择了这个。如果我发现它不起作用,我会回来更新这个。
    try
    (
       Request->instance()->execute();
    }
    catch (MyRequest_Exception $e)
    {
       // do what you want
    }
    
    echo Request->instance()->send_headers->response();
    
    // somewhere in before()
    if ($error)
    {
       throw new MyRequest_Exception($errortext);
    }