我有一个Symfony2应用程序,有两个捆绑包 - AppBundle和UserBundle。我在UserBundle中创建了kernel.request事件监听器。如何在onKernelRequest方法中使用http状态代码403(Forbidden)抛出AccessDeniedException异常?我将检查用户对控制器/操作的访问权限,如果用户有限制,那么我将抛出此异常。
<?php
namespace Company\AppBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Doctrine\ORM\EntityManager;
class CompanyRequestListener
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function onKernelRequest(GetResponseEvent $event)
{
throw new AccessDeniedException('Access Denied');
}
}
?>