开发者

How to stop execution of a Request in Kohana?

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!


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


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜