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

时间:2019-04-02 20:11:16

标签: symfony class controller symfony4 autoload

我从另一个开发人员那里接管了一个项目,所以我想将他的很多代码移到temp文件夹中以清除问题,如果需要任何示例,我将回到他的工作上。

因此,将他的一个控制器类移动到一个临时文件夹“ src / Controller / Temp”时,我得到以下消息:

  

自动加载器的预期类“ App \ Controller \ Temp \ AccountSetupController”将在文件“ /var/www/vhosts/mywebsite.com/vendor/composer/../../src/Controller/Temp/AccountSetupController”中定义.php”。找到了文件,但没有类,文件名或名称空间可能在/var/www/vhosts/mywebsite.com/config/services.yaml(输入资源“ / var / www / vhosts / mywebsite.com / config / services.yaml”)。

这是控制器类(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

在之前,它做得很好,我所做的就是将控制器类移动到其他位置。

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

1 个答案:

答案 0 :(得分:2)

Symfony文件和名称空间正在使用PSR4 autoloader standard中的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的示例表对此进行了解释

+------------------------------+------------------+------------------------+-------------------------------------------+
| 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                |
+------------------------------+------------------+------------------------+-------------------------------------------+