php - 在 Controller 中进行微小更改后 Symfony Listener 消失了
问题描述
我在 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);
}
}
解决方案
由@Jakumi 回答解决。该错误是由namespace AppBundle\EventListener
. 改成App\EventListener
错误后消失。
推荐阅读
- lua - Lua - 在不使用带有 2 个单独脚本的调试库的情况下获取本地化变量或函数
- rest - 使用 HMAC 时随机数 RESTful 是如何实现的?
- r - R purr:将精度映射到预测对象并返回精度
- php - Xdebug 无法使用 XAMPP 开发服务器连接到 Windows 10 上的 VS Code
- google-apps-marketplace - 管理员中的代码 API 默认签名 gsuite 电子邮件
- python - 使用 FuncAnimation 动态更新 3d 散点图/点图中的 ax.text 位置
- powershell - 如何在Powershell中的文件中的每一行上打印/附加行索引号
- dc.js - Sharepoint 错误上的 aspx 页面:仅在呈现数据表时需要功能
- android - 如何在android studio中动态更改src属性?
- apache - httpd 服务正在启动,尽管它在重新启动时无法启动