在Controller中做了一些小改动后,Symfony Listener不见了。

问题描述 投票:0回答:1

我在使用Symfony监听器时遇到了一些奇怪的行为。我有一个有效的配置,但是当我改变或添加一些东西到控制器上时,我发现,我的配置是不正常的。Expected to find class "App\EventListener\AuthenticationSuccessListener" 显示出来。变化可能只是路由路径字符串内的东西。所有的代码都是 此处.删除缓存和服务器重启都没有用。

监听者

namespace AppBundle\EventListener;

use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
use Symfony\Component\Serializer\SerializerInterface;

class AuthenticationListener
{
    /**
     * @var SerializerInterface
     */
    private $serializer;

    public function __construct(SerializerInterface $serializer)
    {
        $this->serializer = $serializer;
    }

    /**
     * @param AuthenticationSuccessEvent $event
     */
    public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
    {

        $event->setData([
            'user' => $this->serializer->normalize($event->getUser(), null, ['groups' => ['basic']]),
            'token' => $event->getData()['token'],
        ]);

    }

}

服务.yaml

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:

services:
    app.event.authentication_success_response:
        class: AppBundle\EventListener\AuthenticationListener
        tags:
            - { name: kernel.event_listener, event: lexik_jwt_authentication.on_authentication_success, method: onAuthenticationSuccessResponse }
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

控制器

<?php

namespace App\Controller;

use App\Entity\User;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;

class ApiUserController extends AbstractController
{
    /**
     * @Route("/api/verify_token", methods="POST", name="api_verify_token")
     * @IsGranted("ROLE_USER")
     */
    public function verifyToken(User $user = null){
        return new JsonResponse([], 200);
    }

}

php symfony listener lexikjwtauthbundle
1个回答
0
投票

由 @Jakumi 回答解决。这个错误是由以下原因引起的 namespace AppBundle\EventListener. 在改为 App\EventListener 错误消失。

© www.soinside.com 2019 - 2024. All rights reserved.