【发布时间】:2017-11-20 14:38:57
【问题描述】:
我想知道用 Laravel 处理异常最专业的方法是什么。
假设我有一张有帖子的桌子。让我们假设一位用户删除了该帖子,而另一位用户在 2 秒后也删除了该帖子。然后我们抛出一个异常——例如 PostNotFoundException。然后我想重定向用户以查看带有自定义错误的帖子。这是一个好方法吗?
异常捕获:
try {
\App\Post::destroy(1);
} catch (Exception $e) {
throw new UserNotFoundException($e->getMessage());
}
PostNotFoundException:
class PostNotFoundException extends \Exception {
public function render () {
return redirect()->route('posts.index')->with([
'error' => 'User not found.'
]);
}
}
最好的方法是什么?可以举个例子吗?
【问题讨论】:
标签: php exception-handling laravel-5.5