ZF2 Skeleton Application with Doctrine and ctrllib

Building further upon the previous post, I’m gonna follow up with some more integration into ZF2. I’ve been building this class library as a ZF2 module. since it is only a class library, it does not contain any MVC elements. So to be able to work with it I’ve also copied the ZendSkeletonApplication, and added a couple of tools.

  • Composer : dependency management
  • Doctrine 2: ORM Layer (and database managerment via cli)
  • Phing: build scripting and database management
  • DbDeploy: database migration
  • ctrllib: my extensions library for ZF2 and Doctrine 2
  • ctrlAuth: a module based on ctrllib provinding MVC for authentication and authorization
  • Twitter Bootstrap: view helpers and layout

To install the SkeletonApplication just clone the github repo, and fire the ‘composer.phar install’ command.

for more setup options, follow the installation guide in the README file in the repo.
The application provides a default database setup you can manage with phing.

After installation, your application should look like this.

app landing page


Continue reading »

Custom default templates in ZF2

My previous post explains how I’ve set up my module structure, for the modules I create under my ‘Ctrl\Module\’ namespace. That post shows you how to configure the autoloader correctly, so the module classes can be accessed. This post will explain how I configured the view manager to avoid template name collisions that this system could cause.

The problems start when a controller in the Auth module does not return a ViewModel with a template specified. the ZF2 MVC registeres a couple of eventListeners to inject a ViewModel if one was not returned, or inject a template based on the matched module/controller/action pair if a ViewModel was returned, but does not have a template set.

For example the action ‘Ctrl\Module\Auth\Controller\IndexController::indexAction()’ that resides in the Auth module will be injected with the template ‘ctrl/index/index’

As you can see it only uses the root namespace of the module to generate a template name. This may seem fine at first, but what if I add a second module that uses MVC? lets say, a blog module:

The action ‘Ctrl\Module\Blog\Controller\IndexController::indexAction()’ that resides in the Blog module would also be injected with the template ‘ctrl/index/index’

Continue reading »

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 »