Kohana 3.x - Return 如果遇到错误,请尽快采取行动

Kohana 3.x - Return from action ASAP if error encountered

是否有等同于以下内容的 kohana(源自 Symfony 1.4):

public function ajax_win() {

    try {
        ...
    } catch Exception($e) {

        $response['errors'] = array($e->getMessage());

        $this->template->content = json_encode($response);

        // Id like to return here, early return if error encountered
        // Symfony example
        // Is there a Kohana counterpart, or just do empty return?
        return sfView::NONE;
    }

    // More code here, which is why I want early return, so I don't have to nest conditionals
    ....
}

所以您处于 Controller 操作内部的情况并且不希望显示任何其他内容,但您的 JSON 编码变量。

您不需要 return 任何东西,因为这不是控制器的工作方式。不过需要修改$this->response.

如果你在Controller_Template的环境中,设置$this->auto_renderFALSE(或者不是TRUE的东西)也很重要,所以after() 方法不会覆盖您尝试的所有内容。

此代码未经测试,但应该可以解决问题。

public function ajax_win() {

    try {
        ...
    } catch Exception($e) {
        $data['errors'] = array($e->getMessage());
        $response = new Response();
        $response->body(json_encode($data));
        $this->response = $response;
        return;
    }

    // More code here, which is why I want early return, so I don't have to nest conditionals
    ....
}

有关详细信息,您可以查看指南,其中详细描述了应用程序的Request Flow