使用Laravel 5.1授权方法时如何返回自定义403异常

使用Laravel 5.1授权方法时如何返回自定义403异常,laravel,laravel-5,laravel-5.1,acl,laravel-authorization,Laravel,Laravel 5,Laravel 5.1,Acl,Laravel Authorization,在laravel 5.1中,如果使用以下方法,则在检查能力时可以返回自定义响应: if (Gate::denies('update', $post)) { return response()->view('errors.403'); } protected function createGateUnauthorizedException( $ability, $arguments, $message = 'This action is unauth

在laravel 5.1中,如果使用以下方法,则在检查能力时可以返回自定义响应:

if (Gate::denies('update', $post)) {
        return response()->view('errors.403');
}
protected function createGateUnauthorizedException(
    $ability,
    $arguments,
    $message = 'This action is unauthorized.',
    $previousException = null
) {
    throw $previousException;
}
使用authorize方法时,是否有任何方法返回类似的自定义错误:

$this->authorize('update', $post);

上面只是抛出一个状态代码为403的http异常。

我可以按以下方式执行:

App\Http\Controllers\Controller
中添加以下方法:

if (Gate::denies('update', $post)) {
        return response()->view('errors.403');
}
protected function createGateUnauthorizedException(
    $ability,
    $arguments,
    $message = 'This action is unauthorized.',
    $previousException = null
) {
    throw $previousException;
}
它将重新显示未经授权的异常

现在在
App\Exceptions\Handler.php
中,您可以在
render
方法的开头添加:

if ($e instanceof \Illuminate\Auth\Access\UnauthorizedException)  {
    return response()->view('errors.403');
}

在授权方法中使用
abort('403')
怎么样?