Autoloading non-PSR0 libraries in Symfony 2.0.x

跟風遠走 提交于 2019-12-06 05:27:29

问题


The Symfony 2.0 Autoloader expects that the libraries it can handle follow the PSR0 or PEAR standard when auto-loading files. If you have an old library which does not follow any of these two standards (in my case, class files are named like name.class.php), how would you handle auto-loading of these libraries?

In Symfony 2.1 this is easy as composer supports classmaps and can load this type of libraries, but how would you do it in Symfony 2.0.x?


回答1:


Inside app/autoload.php, create an instance of MapClassLoader:

use Symfony\Component\ClassLoader\MapClassLoader;
use Symfony\Component\ClassLoader\UniversalClassLoader;

// Create default PSR-0 autoloader
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
    'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
    // ...
));

// Create map autoloader
$mapLoader = new MapClassLoader(array(
    'MyComponent' => __DIR__.'/../library/mycomponent.class.php',
    // ...
));

// Other configurations
// ...

// Register autoloaders
$loader->register();
$mapLoader->register();


来源:https://stackoverflow.com/questions/11903432/autoloading-non-psr0-libraries-in-symfony-2-0-x

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