-1

For the update to Symfony 4.0, I added "_default" and "AppBundle" to services.yml and verified the operation, but the following error occurred.
I have two bundles and want to achieve automatic wiring for each.
Is there anything wrong with it?
https://symfony.com/doc/4.0/service_container/3.3-di-changes.html

ErrorCode

The autoloader expected class "App\Sp\AppBundle\AppBundle" to   
  be defined in file "/home/vagrant/Symfony2/vendor/composer/../../src/App/Sp/AppBundle/AppBundle.php". The file was found but the class was not in it, the class name or namespace probably has a typo.  

services.yml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    AppBundle\:
        resource: '../../src/App/Sp/AppBundle/*'
        exclude: '../../src/App/Sp/AppBundle/{Entity,Repository,AppBundle.php}'

    CommonBundle\:
        resource: '../../src/App/Sp/CommonBundle/*'
        exclude: '../../src/App/Sp/CommonBundle/{Entity,Repository}'

AppBundle.php

<?php
namespace App\Sp\AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AhiSpAdminBundle extends Bundle
{
}
Will B.
  • 17,883
  • 4
  • 67
  • 69
go_live
  • 29
  • 7
  • Why is the file named `AppBundle.php`, but the class is named `AhiSpAdminBundle`? Doesn't the error message tell you that this is wrong? – Nico Haase Feb 01 '21 at 06:39

2 Answers2

2

Composer

The composer.json PSR-4 autoloader in Symfony Flex by default applies a namespace for App from the /src path. The App namespace will apply to all of the subdirectories and files within the src/ directory.

composer.json

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

Symfony Naming Conventions

Symfony naming conventions also require the *Bundle.php file to reside in a namespace of the same name [sic] and [sic]. This is comprised of the vendor or category name (Sp) and the namespace (AhiSpAdminBundle) to make SpAhiSpAdminBundle.

| Namespace Bundle       | Class Name     |
|------------------------|----------------|
| Acme\Bundle\BlogBundle | AcmeBlogBundle |
| Acme\BlogBundle        | AcmeBlogBundle |
// src/Acme/BlogBundle/AcmeBlogBundle.php
namespace App\Acme\BlogBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeBlogBundle extends Bundle
{
}

PSR-4 Autoloading

Since the namespace is App\Sp\AhiSpAdminBundle, the filename and directory needs to be changed to match the namespace and Symfony bundle class name conventions respectively to src/Sp/AhiSpAdminBundle/SpAhiSpAdminBundle.php.

Rename the file to match the class name for the PSR-4 autoloader.

mv src/Sp/AppBundle/AppBundle.php src/Sp/AppBundle/SpAhiSpAdminBundle.php

Rename directory to match the namespace for the PSR-4 autoloader.

mv src/Sp/AppBundle src/Sp/AhiSpAdminBundle

Symfony Services

Fix the namespace and class name of your bundle to adhere to the Symfony naming conventions.

// src/Sp/AhiSpAdminBundle/SpAhiSpAdminBundle.php
namespace App\Sp\AhiSpAdminBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class SpAhiSpAdminBundle extends Bundle
{
  //...
}

Then update your services definitions to match the PSR-4 pathing that is compatible with Symfony.

# config/services.yaml
services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    App\Sp\AhiSpAdminBundle\:
        resource: '../src/Sp/AhiSpAdminBundle/*'
        exclude: '../src/Sp/AhiSpAdminBundle/{Entity,Repository,SpAhiSpAdminBundle.php}'

    App\Sp\CommonBundle\:
        resource: '../src/Sp/CommonBundle/*'
        exclude: '../src/Sp/CommonBundle/{Entity,Repository}'

Cleaning Up

Generate the autoloader files.

composer dump-autoload

Clear and Warmup the Syfmony Cache.

rm -rf var/cache/dev
bin/console --env=dev cache:warmup

If needed repeat the process for the CommonBundle namespace, class name, and filename of src/Sp/CommonBundle/SpCommonBundle.php or restructure following the naming conventions to achieve the desired end-results.

// src/Sp/CommonBundle/SpCommonBundle.php
namespace App\Sp\CommonBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class SpCommonBundle extends Bundle
{
  //...
}

While the vendor/category name (Sp) is optional in the bundle class name, with non-descriptive names like AdminBundle and CommonBundle, it is best-practice to include the category in the bundle name, as to ensure there are no naming conflicts and to better differentiate between them when debugging and unit-testing.

Will B.
  • 17,883
  • 4
  • 67
  • 69
0

I'm a bit familiar with Symfony, but my experience was developing an existing project not creating a new one, but looking at the error: ... The file was found but the class was not in it, the class name or namespace probably has a typo.

I think that explains it then? The file name is AppBundle.php but the class name is AhiSpAdminBundle. PHP usually use the same name for both file and class name so I think you should rename the class name to AppBundle or just rename the file name, basically make sure they're using the same name. (I assume Symfony is looking a class named AppBundle in a AppBundle.php file according to this "convention")

RonaldoC
  • 114
  • 5