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

发布于 2024-09-25 00:31:07 字数 519 浏览 0 评论 0原文

假设我有一个带有像这样的 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();

编辑:: 我实际上不想停止请求流,只是阻止该控制器执行任何操作。该控制器很可能是从另一个控制器调用的,我想将控制权传递回调用控制器。

谢谢!

Let's say I have a controller template with a before function like so...

public function before()
  {
     parent::before();
     if ($this->request === Request::instance()) 
     {
         // its a main request, throw an exception or redirect
         Request::instance()->redirect('/');
     }
     else
     {
        // ok
     }
  }

But let's say I don't want to redirect, I want to stop the Request flow, and do nothing.

Does an exception do this? Is there a simple way, like Request::die();?

EDIT:: I actually don't want to halt the Request flow, just prevent this controller from doing anything. It's likely that this controller was called from another controller, and I want to pass the control back to the calling controller.'

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦境 2024-10-02 00:31:07

1.使用异常(尚未测试):

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);
}
  1. 更改操作名称:

    $this->请求->action('遗忘'); // 重定向到不执行任何操作的“oblivion”操作

1.Use exceptions (not tested yet):

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);
}
  1. Change action name:

    $this->request->action('oblivion'); // redirects to an "oblivion" action that does nothing

絕版丫頭 2024-10-02 00:31:07

您可以在 before() 中设置一个类变量:

$this->execute = false;

然后在您的操作中:

public function action_example()
{
    if (!$this->execute) return;
    // etc
}

You can set a class variable in before() say:

$this->execute = false;

Then in your action:

public function action_example()
{
    if (!$this->execute) return;
    // etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文