api-platform.com的事件不会附加到我的侦听器。我尝试了他们的事件矩阵中的几个组合,但它仍然不能触发。
# services.yml
user_access_listener:
class: AppBundle\Event\Listener\UserAccessListener
arguments: [ "@security.authorization_checker" ]
tags:
- { name: kernel.event_listener, event: kernel.view, method: onKernelView }下面是我的监听器类
namespace AppBundle\Event\Listener;use UserBundle\Entity\User;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class UserAccessListener
{
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* @param AuthorizationCheckerInterface $authorizationChecker
*/
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
$this->authorizationChecker = $authorizationChecker;
}
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
echo "This should trigger";
exit;
$user = $event->getControllerResult();
if (!$user instanceof User) {
return;
}
if (!$this->authorizationChecker->isGranted(null, $user)) {
throw new AccessDeniedException();
}
}
}我预计当我点击GET /projects/1和GET /projects时会出现"This should“,但它没有触发。有什么想法?
发布于 2017-05-15 19:51:50
您应该使用更高的优先级(例如70),以确保您的侦听器在内置视图侦听器之前执行。
示例:
user_access_listener:
class: AppBundle\Event\Listener\UserAccessListener
arguments: [ "@security.authorization_checker" ]
tags:
- { name: kernel.event_listener, event: kernel.view, method: onKernelView, priority: 70 }https://stackoverflow.com/questions/43648968
复制相似问题