Getting error to create bundle in symofny 6.0

83 views Asked by At

I am trying to create a bundle in Symfony 6.0.20. When adding that bundle in www\Acme\config\bundle.php it's showing the following error.

#message: """ Attempted to load class "AcmeTestBundle" from namespace "Acme\TestBundle".\n Did you forget a "use" statement for another namespace? """

Bundle file www\Acme\src\TestBundle\AcmeTestBundle.php

<?php
//symfony composer dump-autoload

namespace Acme\TestBundle;

use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class AcmeTestBundle extends AbstractBundle
{
    
}

www\Acme\config\bundle.php

<?php

 return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Acme\TestBundle\AcmeTestBundle::class => ['all' => true],
];

coposer.js

"autoload": {
        "psr-4": {
            "App\\": "src/",
            "Acme\\TestBundle\\": "src/TestBundle/"
        }
    },
"autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/",
            "Acme\\TestBundle\\Tests\\": "tests/"
        }
    },
1

There are 1 answers

4
Skuti On

I think you could try to remove App namespace from both side as you try to register two different namespace where first could handle other the one.

{
    ...
    "autoload": {
        "psr-4": {
            "Acme\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Acme\\Tests\\": "tests/"
        }
    },
    ...
}

Your class namespace still be:

<?php

namespace Acme\TestBundle;

use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class AcmeTestBundle extends AbstractBundle
{  
}

If you need two different namespace in src folder, you should create two folders in your src/ folder (ex: src/App and src/Acme):

{
    ...
    "autoload": {
        "psr-4": {
            "App\\": "src/App/",
            "Acme\\": "src/Acme/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/App/",
            "Acme\\Tests\\": "tests/Acme/",
        }
    },
    ...
}

Your class namespace still be:

<?php

namespace Acme\TestBundle;

use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class AcmeTestBundle extends AbstractBundle
{  
}