Zend Framework 2 modules with subnamespaces

Zend framework 2 has a new module approach that lets you set things up almost any way you would like. When I create code for myself, I always use the same root namespace ‘Ctrl’. and I want to keep it that way.
For a small testproject I currently have 3 modules of my own:

  • The first one is the application module which will house the MVC application and its classes, its root namespace is ‘AppName’ and it resides in the ./module/AppName directory
  • The second one is a class library module, its root namespace is ‘Ctrl’ and it resides in the ./vendor/ctrl-f5/ctrllib directory
  • The third one is an Auth module, which also contains its own mvc application and classes, the root namespace is ‘Ctrl\Modules\Auth’ and it resides in the ./vendor/ctrl-f5/auth directory

ZF2 allows modules to have any namespace you want, as long as you configure the autoloader correctly, when using subnamespaces you should specify the correct path in the module listener config. This means it should go look for classes in the ‘Ctrl\Modules\Auth’ namespace in the Auth module first, before falling back to the ‘Ctrl’ module.

Continue reading »