无法将控制器 class 移动到其他文件夹

Unable to move a controller class to a different folder

我从另一个开发人员那里接手了一个项目,所以我想将他的很多代码移到一个临时文件夹中以清理这些东西,如果我需要任何示例,我会回到他的东西。

所以在将他的一个控制器 classes 移动到临时文件夹 "src/Controller/Temp" 时,我收到以下消息:

The autoloader expected class "App\Controller\Temp\AccountSetupController" to be defined in file "/var/www/vhosts/mywebsite.com/vendor/composer/../../src/Controller/Temp/AccountSetupController.php". The file was found but the class was not in it, the class name or namespace probably has a typo in /var/www/vhosts/mywebsite.com/config/services.yaml (which is loaded in resource "/var/www/vhosts/mywebsite.com/config/services.yaml").

这是控制器 class (AccountSetupController.php) 的开头部分(我从 /src/Controller/CreatorDashboard 移动到 /src/Controller/Temp):

namespace App\Controller\CreatorDashboard;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
* @Route("/dashboard/setup")
*/
class AccountSetupController extends Controller
{

这是我的 services.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:
    locale: 'en'
    aws.bucket: 'bucket'
    aws.compliance.bucket: 'bucket.compliance'
    num_notification_records: 5
    default_profile_photo_id: 1

services:
    # 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.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    Liip\ImagineBundle\Service\FilterService: '@liip_imagine.service.filter'

    Imagine\Image\ImagineInterface: '@liip_imagine.gd'

    App\Repository\PhotoRepository:
        arguments: ['@liip_imagine.imagine_interface']


    # 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']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

之前它工作得很好,我所做的只是将控制器 class 移到另一个位置。

为什么自动加载器无法加载移动的控制器class?

Symfony 文件和命名空间正在使用 PSR4 autoloader standard from composer.json

"autoload": {
    "psr-4": {
        "App\": "src/"
    }
},

所以文件和命名空间有着密切的关系。

  • 文件 src/Controller/CreatorDashboard/AccountSetupController.php 会有一个命名空间 namespace App\Controller\CreatorDashboard;
  • 当文件 src/Controller/Temp/AccountSetupController.php 应该有一个命名空间 namespace App\Controller\Temp;

这在 PSR-4 PHP-FIG

的示例 table 中进行了解释
+------------------------------+------------------+------------------------+-------------------------------------------+
| FULLY QUALIFIED CLASS NAME   | NAMESPACE PREFIX | BASE DIRECTORY         | RESULTING FILE PATH                       |
+------------------------------+------------------+------------------------+-------------------------------------------+
| \Acme\Log\Writer\File_Writer | Acme\Log\Writer  | ./acme-log-writer/lib/ | ./acme-log-writer/lib/File_Writer.php     |
+------------------------------+------------------+------------------------+-------------------------------------------+
| \Aura\Web\Response\Status    | Aura\Web         | /path/to/aura-web/src/ | /path/to/aura-web/src/Response/Status.php |
+------------------------------+------------------+------------------------+-------------------------------------------+
| \Symfony\Core\Request        | Symfony\Core     | ./vendor/Symfony/Core/ | ./vendor/Symfony/Core/Request.php         |
+------------------------------+------------------+------------------------+-------------------------------------------+
| \Zend\Acl                    | Zend             | /usr/includes/Zend/    | /usr/includes/Zend/Acl.php                |
+------------------------------+------------------+------------------------+-------------------------------------------+