Adding my “3rd party” module into /vendor in Zend Framework

This wasn’t initially obvious to me, but when creating a generic module to use across multiple Zend Framework 2 projects (thus putting it in /vendor) you can include a Module.php file in the root of the module’s directory to make registering the namespace with Zend’s StandardAutoloader simple. (I had assumed this was just how it worked under the /module directory).

So for example – I have a module with the namespace ‘Nas’, so I store it under:
/zf2/vendor/Nas

And then I have Module.php here:
/zf2/vendor/Nas/Module.php
containing…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
namespace Nas;
class Module
{
public function getAutoloaderConfig()
{
return array(
'ZendLoaderStandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__,
),
),
);
}
}

Finally I needed to add the module into /zf2/config/application.config.php:

1
2
3
4
5
6
'modules' => array(
'Application',
'Album',
'Nas',
),

And that’s it. After that I can start accessing classes under my ‘Nas’ namespace from anywhere within my ZF2 app.

One thought on Adding my “3rd party” module into /vendor in Zend Framework

Leave a comment