Symfony FOSRestBundle 异常错误 404

Symfony FOSRestBundle exception error 404

我尝试在找不到页面时添加异常,404。

所以我添加这是配置 fos_rest :

#    exception:
#        codes:
#            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
#            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
#        messages:
#            'Acme\HelloBundle\Exception\MyExceptionWithASafeMessage': true

但是我有这个错误:

FOSRestBundle exception mapper: Could not load class 'Acme\HelloBundle\Exception\MyExceptionWithASafeMessage' or the class does not extend from '\Exception'. Most probably this is a configuration problem.

由于命名空间是 acme,您似乎复制了一些演示教程代码,但捆绑包不存在。

您需要创建自己的异常 class 扩展 \Exception 并在 yml 中传递其命名空间。