dongzhou4727 2011-11-06 10:13
浏览 55
已采纳

Zend Framework 2库路径

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)

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);
  }
}

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

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

  • 写回答

1条回答 默认 最新

  • duanjianqu3685 2011-12-13 09:23
    关注

    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.

    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
    

    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];
    };
    

    And autoload_register.php something like this:

    <?php
    spl_autoload_register(include __DIR__ . '/autoload_function.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',
                )
            );
        }
    }
    

    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);
      }
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应