php Zend Framework 2.0 中的自动加载自定义库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8559107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:59:25  来源:igfitidea点击:

Autoload custom library in Zend Framework 2.0

phpzend-frameworkautoloadzend-framework2

提问by Alex Pliutau

I need to use autoloading for my custom classes in Zend Framework 2.0. My custom library located in /vendor/Garvey/library/Garvey. I have a simple extended AbstractTable class in /vendor/Garvey/library/Garvey/Db/Table/AbstractTable.php:

我需要在 Zend Framework 2.0 中为我的自定义类使用自动加载。我的自定义库位于/vendor/Garvey/library/Garvey. 我有一个简单的扩展 AbstractTable 类/vendor/Garvey/library/Garvey/Db/Table/AbstractTable.php

<?php

namespace Garvey\Db\Table;

use Zend\Db\Table\AbstractTable;

abstract class AbstractTable extends AbstractTable
{
    public function getItemById($id)
    {

    }
}

In the index.php I have the following code:

在 index.php 我有以下代码:

require_once 'vendor/ZendFramework/library/Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory(array('Zend\Loader\StandardAutoloader' => array(
    'prefixes' => array(
        'Garvey' => 'vendor/Garvey/library/Garvey',
    )
)));

But I have the following error. What I have missed?

但我有以下错误。我错过了什么?

Fatal error: Class 'Garvey\Db\Table\AbstractTable' not found

Thank you in advance.

先感谢您。

回答by atti

Your original index.php would also worked if you changed the 'prefixes' key to 'namespaces' and specify path like below:

如果您将 'prefixes' 键更改为 'namespaces' 并指定如下路径,您原来的 index.php 也将起作用:

Zend\Loader\AutoloaderFactory::factory(array('Zend\Loader\StandardAutoloader' => array(
    'namespaces' => array(
        'Garvey' => dirname(__DIR__) . '/vendor/Garvey',
    )
)));

回答by Sergey Romanov

Or you can defime method in Module.php

或者你可以在 Module.php 中定义方法

public function getAutoloaderConfig()
{
    $return = array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php'
        ), 
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                'Garvey' => __DIR__ . '/../../vendor/Garvey/library/Garvey',
            )
        )
    );
}

But I would not recommend it. Since ZF2 purpose all centered about speed in autoloading the best way is to use class_map style to load your classes. It will work much quicker at the end but require additional work. You can to register every class in you class_map file.

但我不会推荐它。由于 ZF2 的目的都集中在自动加载的速度上,因此最好的方法是使用 class_map 样式来加载您的类。最后它会工作得更快,但需要额外的工作。您可以在 class_map 文件中注册每个类。

You can create class_map.php in the root of your library and place there

您可以在库的根目录中创建 class_map.php 并将其放在那里

<?php
return array(
    'Garvey\Db\Table\AbstractTable' => __DIR__ . '/Garvey/Db/Table/AbstractTable.php', 
);

And add there as many classes as you use. And in getAutoloaderConfig() you can add you classmap

并添加尽可能多的类。在 getAutoloaderConfig() 你可以添加你的类图

public function getAutoloaderConfig()
{
    $return = array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
            __DIR__ . '/../../vendor/Garvey/library/Garvey/class_map.php',
        ), 
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            )
        )
    );
}

回答by dan_nl

Matthew Weier O'Phinney explains in this videothat there are now 3 methods for autoloading :

Matthew Weier O'Phinney 在此视频中解释说,现在有 3 种自动加载方法:

  • ZF1-style include_path autoloader ( old zf1 method, not recommended)
  • Per-namespace/prefix autoloading ( new zf2 method, better)
  • Class-map autoloading( recommended and the fastest)
  • ZF1 风格的 include_path 自动加载器(旧的 zf1 方法,不推荐
  • 每个命名空间/前缀自动加载(新的 zf2 方法,更好
  • 类映射自动加载推荐和最快

A class-map generatorutility is mentioned in the docs that will take care of writing the /vendor/vendor_name/library/autoload_classmap.phpfor you.

文档中提到了一个类映射生成器实用程序,它将负责/vendor/vendor_name/library/autoload_classmap.php为您编写。

The solution you found is similar to the one Matthew mentions in the video for the Per-namespace/prefix autoloading. Following the code structure in ZendSkeletonApplication, that code would go in the /init_autoloader.phpfile, rather than in the /public/index.phpfile.

您找到的解决方案类似于 Matthew 在视频中提到的 Per-namespace/prefix autoloading。遵循ZendSkeletonApplication 中的代码结构,该代码将放在/init_autoloader.php文件中,而不是/public/index.php文件中。

回答by Kdecom

Have a quick look at this post.

快速浏览一下这篇文章

Now next step is add some code into our custom library.

现在下一步是将一些代码添加到我们的自定义库中。

First of all open a file ./vendor/Garvey/autoload_classmap.php

首先打开一个文件 ./vendor/Garvey/autoload_classmap.php

return array(

    'Garvey\Module' => __DIR__ . '/Module.php',

    'Garvey\Db\Table' => __DIR__ . '/library/Garvey/Db/Table/AbstractTable.php',

)

Next is ./vendor/Garvey/Module.php

接下来是 ./vendor/Garvey/Module.php

namespace Garvey;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),

            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/library/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

Now inside your library create a file inside a folder:

现在在您的库中在文件夹中创建一个文件:

./vendor/Kdecom/library/Kdecom/Db/Table/AbstractTable.php

./vendor/Kdecom/library/Kdecom/Db/Table/AbstractTable.php

One final thing that we need to do which is add this library into your application.config.phpfile.

我们需要做的最后一件事是将此库添加到您的application.config.php文件中。

So your application.config.phpfile will looks something like this way...

所以你的application.config.php文件看起来像这样......

return array(
    'modules' => array(
        'Application',
        'Garvey'
    ),

    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),

        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

回答by Alex Pliutau

I have found the answer. Put this in your index.php:

我找到了答案。把它放在你的 index.php 中:

require_once 'vendor/ZendFramework/library/Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader();
$loader->registerNamespace('Garvey', realpath('vendor/Garvey/library/Garvey'));
$loader->register();