如何在Symfony2中创建禁止响应?

Jan*_*ota 12 php symfony

我刚刚发现了SensioLabsInsight,并发现了有关如何编写优秀代码的非常有趣的提示.如果对于为什么(或者为什么不)应该使用某些东西有一些解释,那就太好了 - 即使对于像exit和等的基本东西也是如此die.这将有助于我向与我合作的人解释事情.

所以我的问题是针对AccessDeniedHttpException - 它说:

Symfony应用程序不应抛出AccessDeniedHttpException

那么如何从应用程序控制器或EventListener返回403 Forbidden?
什么是最佳做法?

说实话,我以为会是

throw new AccessDeniedHttpException()
Run Code Online (Sandbox Code Playgroud)

因为404你有

throw $this->createNotFoundException()
Run Code Online (Sandbox Code Playgroud)

但看起来我错了.

Ale*_* B. 22

我认为这意味着你必须抛出AccessDeniedException而不是直接抛出AccessDeniedHttpException.

主要原因是AccessDeniedExceptionSymfony\Component\Security\Http\Firewall\ExceptionListener中的事件监听器捕获,然后您可以使用它来制作一些东西.检查onKernelException功能.


Toc*_*car 0

看看这里http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html,似乎你可以抛出一个 AuthenticationException ,它返回一个 403 响应(?)