Symfony2 - Composer class loader instance in controller

Deadly 提交于 2019-12-11 01:05:56

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/20338505/symfony2-composer-class-loader-instance-in-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!