2

I'm trying to use symfony flex with a bundle.

I have this directory structure

/
  src/
    AppBundle/
       AppBundle.php
       # Many classes
    Kernel.php

I want to load Kernel.php class with this namespace App and classes inside AppBundle with the namespace AppBundle.

I've tried many composer configurations but I couldn't load them.

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

But I got errors like this:

Expected to find class "App\AppBundle\AppBundle" in file "/var/www/vhosts/flex/src/AppBundle/AppBundle.php"

UPDATE

src/Kernel.php class have a different namespace and I couldn't change it because other classes used it, the namespace is App. Some scripts call that class using use App\Kernel

src/AppBundle/AppBundle.php class has this namespace AppBundle

There is way to do this?

UPDATE 2

I sorted it out:

"autoload": {
    "psr-4": {
        "AppBundle\\": "src/AppBundle/"
    },
    "classmap": [
        { "App\\Kernel": "src/Kernel.php" }
    ]
},
anacona16
  • 41
  • 1
  • 7

2 Answers2

1

I would suggest using a classmap for Kernel.php instead:

"autoload": {
    "psr-4": { "AppBundle\\": "src/AppBundle" },
    "classmap": [ "src/Kernel.php" ]
}

See https://getcomposer.org/doc/04-schema.md#classmap

dbrumann
  • 16,803
  • 2
  • 42
  • 58
  • From the documentation it looked like it would resolve the class name by searching for classes in all .php files. In that case you might want to create a src/App/Kernel.php and then modify your psr-4 autoload accordingly. – dbrumann Aug 03 '17 at 16:05
0

You only need

"psr-4": { "App\\": "src/" }
Vasil Kulakov
  • 190
  • 2
  • 4