0
votes

I took over a project from another developer, so I want to move a lot of his code to a temp folder to clear things up and I'll go back to his stuff if I need any examples.

So in moving one of his controller classes to a temp folder "src/Controller/Temp", I get the following message:

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").

Here is what the beginning of the controller class (AccountSetupController.php) looks like (that I moved from /src/Controller/CreatorDashboard to /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
{

Here is what my services.yaml file looks like:

# 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

It worked perfectly fine before, all I did was move the controller class to a different location.

Why can't the autoloader load a moved controller class?

1
Try updating the namespace to namespace App\Controller\Temp;jfadich

1 Answers

2
votes

Symfony files and namespaces are using the PSR4 autoloader standard from composer.json

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

So the files and the namespaces have an intimate relation.

  • the file src/Controller/CreatorDashboard/AccountSetupController.php would have a namespace namespace App\Controller\CreatorDashboard;
  • when the file src/Controller/Temp/AccountSetupController.php should have a namespace namespace App\Controller\Temp;

This is explained in the example table of the 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                |
+------------------------------+------------------+------------------------+-------------------------------------------+