我正在学习 Symfony5,我想知道是否可以在路由执行后捕获异常。
主要思想是摆脱我的路线中一些重复的 try-catch 代码块:
public function getStudentMean(int $studentId): Response
{
try {
$mean = $this->gradedStudentService->getMean($studentId);
return new Response($mean, Response::HTTP_OK);
} catch (AbstractApiException $e) {
return $this->returnBadRequestResponse($e->getMessage());
}
}
public function deleteStudent(int $id): Response
{
try {
$this->studentService->deleteStudent($id);
return new Response('', Response::HTTP_NO_CONTENT);
} catch (AbstractApiException $e) {
return $this->returnBadRequestResponse($e->getMessage());
}
}
我是否必须编辑我的public\index.php文件才能捕获此处的异常?还有另一种更清洁的方法吗?
谢谢!
开心每一天1111