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?
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 (soShanty_Mongo
will beShanty\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):The library is a submodule to the Shanty-Mongo git repository. The file
autoload_classmap.php
is a classmap created by the php scriptclassmap_generator.php
inside thebin
directory of the ZF2 repository. Then the autoload_function.php can be something simple as this:And autoload_register.php something like this:
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: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:Because ShantyMongo doesn't use namespaces, you don't have that use statement anymore.