简体   繁体   English

在Symfony2的“组件-> PSR-0类加载器”部分中,正在编辑哪个文件?

[英]In Symfony2's “The Components -> The PSR-0 Class Loader” section which file(s) is being edited?

I'm trying to grasp how to include a PSR-0 enabled external library on my Symfony2 project. 我试图掌握如何在我的Symfony2项目中包括启用PSR-0的外部库。 In "The PSR-0 Class Loader" page (here: http://symfony.com/doc/master/components/class_loader/class_loader.html "). 在“ PSR-0类加载器”页面中(此处: http : //symfony.com/doc/master/components/class_loader/class_loader.html “)。

Which file is being edited? 正在编辑哪个文件? If it's app/autoloader.php, mine looks different than what's being presented: 如果是app / autoloader.php,我的看起来就与所呈现的有所不同:

use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;
/**
 * @var $loader ClassLoader
 */
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;

Do I have to include this code before return? 返回之前是否必须包含此代码? Or it should be in the other files being called? 还是应该在其他文件中被调用? Still if it's app/autoload.php, shouldn't this code: 仍然,如果它是app / autoload.php,则不应该使用此代码:

// register several namespaces at once
$loader->addPrefixes(array(
    'Symfony' => __DIR__.'/../vendor/symfony/symfony/src',
    'Monolog' => __DIR__.'/../vendor/monolog/monolog/src',
));

be addNamespaces instead? 被addNamespaces代替? and once I succeed in declaring this library how do I use it? 一旦我成功声明了这个库,我该如何使用它?

Yes, notice that the app/autoload.php file fetches the $loader instance that is returned from vendor/autoload.php. 是的,请注意app / autoload.php文件将获取从vendor / autoload.php返回的$ loader实例。 So, the documentation you linked to gives an example where you explicitly create a new instance, but actually, using the app/autoload.php, the instance is ready and waiting for you. 因此,您链接到的文档提供了一个示例,您在其中可以显式创建一个新实例,但是实际上,使用app / autoload.php,该实例已准备就绪,正在等待您的光临。

With regards to creating a new library set, let's assume I want to create a new library called 'MyLibrary', and it was going to live in the src directory, after: 关于创建新库集,假设我要创建一个名为“ MyLibrary”的新库,并且该库将在以下位置存在于src目录中:

$loader = require __DIR__.'/../vendor/autoload.php';

I would declare the following: 我要声明以下内容:

$loader->addPrefix('MyLibrary', __DIR__.'/../src');

Now I would: 现在我会:

$ cd src

$ mkdir MyLibrary

$ mkdir MyLibrary/Component

$ mkdir MyLibrary/Component/SomeClasses

$ vim MyLibrary/Component/SomeClasses/MyClass.php

MyClass.php: MyClass.php:

<?php

    namespace MyLibrary\Component\SomeClasses;

    class MyClass
    {
        ...
    }

In another file wanting to use that class, at the top of the file, AFTER the namespace declaration (if there is one): 在另一个想要使用该类的文件中,在文件顶部,名称空间声明之后(如果有的话):

<?php

...

use MyLibrary\Component\SomeClasses\MyClass;

$myClass = new MyClass();

However, I have never found a need to do this so explicitly, as I would create a Symfony Bundle, which in effect is a library performing a specific task, and then put your code in the bundle. 但是,我从来没有发现需要如此明确地执行此操作,因为我将创建一个Symfony捆绑包,实际上是一个执行特定任务的库,然后将您的代码放入捆绑包中。 Creating a bundle is well documented. 创建捆绑文件有据可查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM