1

Due to some set up restraints I would like to autoload bundles that are in a specific folder without adding new bundles in the AppKernel.php.

For example:

I have a folder called autoload in /src/.

I would like any bundle I create in there to automatically load without needing to register them in the AppKernel.php.

My idea is to write a function to loop through all the folders within /src/autoload and build the string for the bundle and append it to $bundles in registerBundles().

//loop through src/autoload
//Create the string ... new Namespace/Autoload/BundleName()
//$bundles[] = $listOfBundlesFromAutoload 

I know this is not conventional and if there is no other way then I will just add the new bundles manually but there are some constraints that will make it difficult to keep on registering new bundles every time a new bundle is added.

Any ideas?

user742736
  • 2,629
  • 5
  • 30
  • 40

1 Answers1

2

Sure you can, as long as you know it's not conventional and wash your hands well afterwards :-)

Inside your AppKernel.php, just after the default bundles are loaded you can put:

    $bundleFiles = glob(__DIR__ . '/../src/*Bundle/*Bundle.php');

    foreach ($bundleFiles as $bundleFile) {
        $className = $this->getClassFullNameFromFile($bundleFile);
        $bundles[] = new $className();
    }

Where the getClassFullNameFromFile was copied as it is from this answer.

Community
  • 1
  • 1
mickadoo
  • 3,337
  • 1
  • 25
  • 38