Symfony2 - Composer class loader instance in controller

426 views Asked by At

Is there a way to get the composer autoloader instance inside a Symfony2 Controller?

2

There are 2 answers

3
Cerad On BEST ANSWER

Yes - there is a way.

And assuming that you want to know how to actually get the loader then you can do this in your controller:

class MyController
    function myAction()
    {
        die(get_class($GLOBALS['loader'])); // Composer\Autoload\ClassLoader

Should you do this? Probably not. In most cases you can tweak the loader in the app/autoload.php file.

0
polesen On

Using the $GLOBALS did not work for me, but you can also get it like this:

$autoload_functions = spl_autoload_functions();
$loader = $autoload_functions[0][0];

this assumes that "autoload.php" has been required, AND that it is configured to prepend the composer classloader to other classloaders (the default).

NOTE: I am not saying this is good style.