在Symfony 2.0.x中自动加载非PSR0库 [英] Autoloading non-PSR0 libraries in Symfony 2.0.x
问题描述
Symfony 2.0 Autoloader
期望它在自动加载文件时可以处理的库遵循PSR0
或PEAR
标准.如果您有一个不符合这两个标准中任何一个的旧库(在我的情况下,类文件的名称类似name.class.php),您将如何处理这些库的自动加载?
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?
在Symfony 2.1
中,这很容易,因为composer支持classmaps
并且可以加载这种类型的库,但是您将如何在Symfony 2.0.x
中实现呢?
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
?
推荐答案
在app/autoload.php
内,创建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();
这篇关于在Symfony 2.0.x中自动加载非PSR0库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!