首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能声明类symfony 3

不能声明类symfony 3
EN

Stack Overflow用户
提问于 2019-09-20 14:28:00
回答 2查看 87关注 0票数 1

我在实现MaintenanceListener服务时遇到了一个小错误,该服务将显示一个维护页面--这是我的services.yml

代码语言:javascript
复制
# 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 }```

这是我的课:

代码语言:javascript
复制
<?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();
    }
}

下面是我得到的错误:在stderr中发送的/var/www/Symfony/src/AppBundle/Event/MaintenanceListener.php:"PHP : PHP致命错误:不能声明类MListener\MListener,因为这个名称已经在第9行的/var/www/Symfony/src/AppBundle/Event/MaintenanceListener.php中使用了“,同时从上游读取响应头,谢谢帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-20 14:45:37

这一切都归结为自动魔法。autoload魔术假定了某种目录结构,即(除其他外)在composer.json中定义的目录结构。它实质上说:

命名空间AppBundle\...位于src/AppBundle/...目录中。

因此,每个类AppBundle\Something\Else都位于src/AppBundle/Something/Else.php中。

现在,symfony开始加载应该处理事件的服务(由于您的配置) AppBundle\Event\MaintenanceListener,它尝试实例化该服务,这导致自动加载程序加载仅包含类MListener/MListener的文件src/AppBundle/Event/MaintenanceListener.php

由于自动加载有点麻烦,通常,它会尝试其他方法/定义,并可能尝试再次读取该文件,然后它将无法重新声明MListener/MListener类,因为它已经存在。

为了明确这一点:如果遵循标准(本例中是PSR-4 ),将目录结构绑定到命名空间结构,则这些方法非常有效。如果你把什么东西放进一个文件,根据PSR-4 -不属于那里,你会遇到问题,就像你得到的那个。

修复是简单而明显的:名称空间是目录(用反斜杠代替操作系统的任何目录分隔符),文件名是类名(显然没有.php )。因此,要么将文件重命名为src/MListener/MListener.php,然后相应地调整services.yaml:MListener\Mlistener: ...,要么将文件中的名称空间和类分别重命名为AppBundle\EventMaintenanceListener

票数 0
EN

Stack Overflow用户

发布于 2019-09-20 14:47:51

确保创建文件src/Event/MaintenanceListener.php

代码语言:javascript
复制
<?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中声明的名称相同。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58030307

复制
相关文章

相似问题

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