Search code examples
phpsymfonysymfony-3.3

ClassNotFoundException when loading local Symfony bundle


I try to install a custom Symfony bundle in my application. I created the bundle with the following folder structure:

.
├── composer.json
├── composer.lock
└── src
    └── Namespace
        └── CoreBundle
            ├── Controller ...
            ├── CoreBundle.php
            ├── Entity ...
            ├── Helper ...
            ├── Repository ...
            ├── Resources ...
            ├── Services ...
            └── Tests ...

I successfully installed the bundle by composer install in the vendor folder. There it is under vendor/namespace/core-bundle as a sym-link (I loaded it locally).

After that I added new Namespace\CoreBundle\CoreBundle(), in the AppKernel.php and this produces the following error:

(1/1) ClassNotFoundException

Attempted to load class "CoreBundle" from namespace "Namespace\CoreBundle".
Did you forget a "use" statement for another namespace?
in AppKernel.php (line 25)

I tried to copy the folder manually in vendor folder, if there is any problem with the sym-link, but it produces the same error. I checked out the autoloaded classes by dump(ClassMapGenerator::createMap('/path/to/symfony')); and I don't found my namespace there. Is my configuration wrong or my folder structure? If you need more code, I'll update my question as fast as possible.

Update: My bundles composer.json:

{
  "name": "namespace/core-bundle",
  "license": "proprietary",
  "type": "symfony-bundle",
  "autoload": {
    "psr-4": {
      "Namespace\\CoreBundle\\": "src/Namespace/CoreBundle"
    }
  },
  "require": {
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/orm": "^2.5",
    "liip/imagine-bundle": "1.9.*",
    "php": ">=5.5.9",
    "symfony/symfony": "3.3.*"
  },
  "require-dev": {
  },
  "extra": {
    "branch-alias": {
      "dev-master": "development"
    }
  }
}

Solution

  • After every manual update of autoload section in your composer.json:

    {
        "autoload": {
            "psr-4": {
                "Namespace\\CoreBundle\\": "src/Namespace/CoreBundle"
            }
        }
    }
    

    don't forget to refresh your generated /vendor/autoload.php via CLI:

    composer dump 
    # shortcut for `composer dump-autoload`