首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony api-平台不会附加到事件侦听器

Symfony api-平台不会附加到事件侦听器
EN

Stack Overflow用户
提问于 2017-04-27 12:58:36
回答 1查看 1.3K关注 0票数 0

api-platform.com的事件不会附加到我的侦听器。我尝试了他们的事件矩阵中的几个组合,但它仍然不能触发。

代码语言:javascript
复制
# services.yml
user_access_listener:
    class:      AppBundle\Event\Listener\UserAccessListener
    arguments: [ "@security.authorization_checker" ]
    tags:
        - { name: kernel.event_listener, event: kernel.view, method: onKernelView }

下面是我的监听器类

代码语言:javascript
复制
 namespace AppBundle\Event\Listener;
代码语言:javascript
复制
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();
        }
    }
}

api-platform event reference

我预计当我点击GET /projects/1GET /projects时会出现"This should“,但它没有触发。有什么想法?

EN

回答 1

Stack Overflow用户

发布于 2017-05-15 19:51:50

您应该使用更高的优先级(例如70),以确保您的侦听器在内置视图侦听器之前执行。

示例:

代码语言:javascript
复制
user_access_listener:
    class:      AppBundle\Event\Listener\UserAccessListener
    arguments: [ "@security.authorization_checker" ]
    tags:
        - { name: kernel.event_listener, event: kernel.view, method: onKernelView, priority: 70 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43648968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档