Zend Framework 2 库路径 [英] Zend Framework 2 Library Paths

查看:27
本文介绍了Zend Framework 2 库路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在 ZF2 上弄湿我的脚,但我偶然发现了我的第一个问题.在一个模块上说我想使用 Shanty_Mongo(一个连接到 MongoDb 的外部库)

Trying to get my feet wet on ZF2 and I've stumbled on my first problem. Say on a module I want to use Shanty_Mongo (an external library to connect to MongoDb)

所以我复制了库上的整个 Shanty 目录并创建了一个新的 Model 类:

So I've copied the entire Shanty directory on the library and created a new Model class:

namespace Dummy\Model;

use Shanty\Mongo\Document;

class Dummy extends Shanty_Mongo_Document {
  public function setConnections( $connections ) {
    Shanty_Mongo::addConnections($connections);
  }
}

(如果我理解得很好,DI 将使用 setConnections() )

(The setConnections() is to be used by DI, if I've understood it well)

这似乎找不到 Shanty_Mongo_Document.我应该向 application.config.php 添加一些东西来指向额外的库吗?

This seems to fail to find Shanty_Mongo_Document. Should I add something to the application.config.php to point to the extra library?

推荐答案

Shanty_Mongo 是一个不使用命名空间的旧"下划线分隔库.在 ZF2 中,样式与 PSR-0 标准相同,但具有命名空间(因此 Shanty_Mongo 将是 Shanty\Mongo).但是,例如,您可以使用类映射很好地加载这些旧样式.然后您可以在 ZF2 项目中使用下划线分隔的类.

The library Shanty_Mongo is an "old" underscore separated library without using namespaces. In ZF2, the style is the same PSR-0 standard but with namespaces (so Shanty_Mongo will be Shanty\Mongo). However, you are able to load these old style fine with a classmap for example. Then you can use underscore separated classes inside your ZF2 project.

我建议您为此库创建一个模块并将该模块放在 ./vendor 下(用于提供 3rd 方功能的模块").在这个模块中,您可以创建以下目录结构(我假设模块的名称是 ShantyMongo):

I'd suggest you create a module for this library and put that module under ./vendor (for "modules providing 3rd party features"). In this module, you can create the following directory structure (I assume the name of the module is ShantyMongo):

./vendor/ShantyMongo/
    library/
    Module.php
    autoload_classmap.php
    autoload_function.php
    autoload_register.php

该库是 Shanty-Mongo git 存储库的子模块.文件 autoload_classmap.php 是由 ZF2 存储库的 bin 目录中的 php 脚本 classmap_generator.php 创建的类映射.然后 autoload_function.php 可以是这样简单的:

The library is a submodule to the Shanty-Mongo git repository. The file autoload_classmap.php is a classmap created by the php script classmap_generator.php inside the bin directory of the ZF2 repository. Then the autoload_function.php can be something simple as this:

<?php
return function ($class) {
    static $map;
    if (!$map) {
        $map = include __DIR__ . '/autoload_classmap.php';
    }

    if (!isset($map[$class])) {
        return false;
    }
    return include $map[$class];
};

和 autoload_register.php 类似:

And autoload_register.php something like this:

<?php
spl_autoload_register(include __DIR__ . '/autoload_function.php');

为了让 ZF2 应用程序知道您有这个模块,您需要用 ShantyMongo\Module 类填充 module.php.这样的东西应该就足够了:

To let the ZF2 application know you have this module, you need to fill the module.php with a ShantyMongo\Module class. Something like this should be sufficient:

<?php

namespace ShantyMongo;

use Zend\Module\Consumer\AutoloaderProvider;

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

如果您在 application.config.php 的模块数组中添加ShantyMongo",您现在已经为 ZF2 中的这个 3rd 方库设置了自动加载器.然后,您可以按如下方式使用您的模型:

If you add "ShantyMongo" to your modules array in application.config.php you now have set up the autoloader for this 3rd party library inside ZF2. You can then use your model as follows:

<?php

namespace Dummy\Model;

class Dummy extends Shanty_Mongo_Document {
  public function setConnections ($connections) {
    Shanty_Mongo::addConnections($connections);
  }
}

因为 ShantyMongo 不使用命名空间,所以您不再有那个 use 语句.

Because ShantyMongo doesn't use namespaces, you don't have that use statement anymore.

这篇关于Zend Framework 2 库路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆