我在尝试实施MaintenanceListener服务时遇到了一些错误,该服务将显示维护页面 这是我的服务。yml
# https://symfony.com/doc/current/service_container.html
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in Cocorico\CoreBundle\DataFixtures\ORM\ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
# Cocorico\CoreBundle\DataFixtures\ORM\:
# resource: '../../src/Cocorico/CoreBundle/DataFixtures/ORM/*'
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests,Event}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
# AppBundle\Controller\:
# resource: '../../src/AppBundle/Controller'
# public: true
# tags: ['controller.service_arguments']
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
maintenance_listener:
class: AppBundle\Event\MaintenanceListener
arguments:
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }```
这是我的课程:
<?php
namespace MListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Response;
class MListener
{
public function onKernelRequest(GetResponseEvent $event)
{
$event->setResponse(new Response('Iziparty is in maintenance mode', Response::HTTP_SERVICE_UNAVAILABLE));
$event->stopPropagation();
}
}
这是我得到的错误: FastCGI在stderr中发送:“ PHP消息:PHP致命错误:无法声明类MListener \ MListener,因为该名称已在第9行的/var/www/Symfony/src/AppBundle/Event/MaintenanceListener.php中使用”来自上游的响应头 感谢您的帮助。
答案 0 :(得分:0)
全部归结为自动加载魔术。自动加载魔术假定具有某种目录结构,即(在其他方面)在composer.json中定义的目录结构。它基本上说:
命名空间AppBundle\...
位于目录src/AppBundle/...
因此每个类AppBundle\Something\Else
都位于src/AppBundle/Something/Else.php
现在,symfony开始加载应该处理事件的服务(由于您的配置)AppBundle\Event\MaintenanceListener
,它将尝试实例化该事件,这会导致自动加载程序加载文件src/AppBundle/Event/MaintenanceListener.php
其中仅包含类MListener/MListener
。
由于自动加载有点麻烦,通常,它将尝试其他方法/定义,并可能尝试再次读取该文件,然后它将无法重新声明MListener/MListener
类,因为它已经存在。
对此进行明确说明:如果遵循将目录结构绑定到名称空间结构的标准(在本例中为PSR-4),则这些方法将非常有效。如果您在文件中放入某些内容,根据PSR-4的说法,该内容不属于该文件,那么您将遇到麻烦,就像您遇到的那样。
修复很容易而且很明显:名称空间是目录(使用反斜杠而不是操作系统具有的任何目录分隔符),文件名是类名(显然没有.php)。因此,要么将文件重命名为src/MListener/MListener.php
并相应地修改services.yaml:MListener\Mlistener: ...
,要么将文件中的名称空间和类分别重命名为AppBundle\Event
和MaintenanceListener
。 / p>
答案 1 :(得分:0)
请确保创建文件src / Event / MaintenanceListener.php
<?php
namespace AppBundle\Event;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Response;
class MaintenanceListener
{
public function onKernelRequest(GetResponseEvent $event)
{
$event->setResponse(new Response('Iziparty is in maintenance mode', Response::HTTP_SERVICE_UNAVAILABLE));
$event->stopPropagation();
}
}
在文件service.yaml中声明了相同的名称