4

KO 3.3 中的新功能是 HTTP::redirect 方法,它通过抛出 HTTP_Exception_302 来工作,它会冒泡并由系统处理以进行实际的重定向。

try...catch我的问题是:如果我在一个块中调用重定向,我怎么能在不捕获异常的情况下进行重定向?

例如:

try {
    if($var === TRUE){
        HTTP::redirect(URL::site($_REQUEST['redirect_uri']));
    }else{
        throw new Exception('Error');
    }
} catch(Exception $e) {
    $this->template->errors[] = $e->getMessage();
}

这不会导致重定向,因为通用异常处理程序会捕获它。我该如何避免这种情况?

4

2 回答 2

3
try {
    if($var === TRUE){
        HTTP::redirect(URL::site($_REQUEST['redirect_uri']));
    }else{
        throw new Exception('Error');
    }
} 
catch(HTTP_Exception_Redirect $e) {
    // just rethrow it
    throw $e;
}
catch(Exception $e) {
    $this->template->errors[] = $e->getMessage();
}
于 2013-04-26T19:13:20.133 回答
1

在捕捉异常时不要那么随意。抓住你所期望的,没有别的。这个问题不应该存在。

于 2013-04-26T19:18:53.070 回答