ZF2 ServiceManager Custom ViewHelpers

The previous article explains how the ‘Zend\Mvc\Service\ModuleManagerFactory‘ sets initializers that gather information from certain keys in the configuration. A ‘ViewHelperManager’ is configured to read the ‘view_helpers’ key. Which expects regular ServiceConfiguration config.

We have seen that the Mvc  ’ServiceListenerFactory‘ maps the default ‘ViewHelperManager’ to ‘Zend\Mvc\Service\ViewHelperManagerFactory‘. This class does not only have a default helper map, which includes a HelperConfig that will add a factory under the ‘navigation’ key, but also extends from ‘Zend\View\HelperPluginManager‘, which in turn contains the configuration for more ViewHelpers.

And it doesn’t stop there.

Continue reading »

ZF2 Skeleton Application with Ctrl\DomainService

After creating the domain models and updating the database accordingly, we need to create a ‘Ctrl\DomainModelService‘, for each entity, to manage it. This will allow for the models themselves to be unaware of their persistence.

ctrllib provides a DomainServiceLoader comparable to the ControllerLoader. It is configured through the ServiceManager, and thus gives us many options in return:

  • uses module configuration and can therefore override other module DomainServices
  • it ensures correct initialization of your DomainServices
  • DomainServices can be fetched from wherever you have a ServiceLocator
  • instances can be aliased or marked as shared
  • possibility to add custom initializers or even to override the whole factory

Going further with the Car and Company models created in the previous post, we are going to create a ‘DomainModelService’ for both of them.
To create a DomainService we need 2 things: a class, and a configuration key pointing to the class.

Continue reading »

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

ctrlSkeleton

Continue reading »

ZF2 ServiceManager configuration walkthrough

ZF2 is all about extensibility, and the new ServiceManager and Config make it very easy, if you know how. Extensibility comes with some complexity, and it may be a bit harder to try and trace where everything is going, especially when using events.

With the ZF2 Configuration manager and Service Locator configurations, it’s not always as simple to know where to find the defaults, and how to best override them. This post might remind you of the factoryfactoryfactoryfactory post from joelonsoftware

I wanted to implement some custom navigation stuff and soon began to wonder how the config works, and how I can manipulate it to inject my custom view helpers or navigation pages. This post will cover the buildup of the service manager, and I plan to do some follow up posts to go into some of the main service factories.

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 »

Domain layer and ZendFramework2 using Zend\ServiceManager

UPDATE: ctrllib has been implemented in CtrlSkeleton, providing easy database management for Doctrine entities and implemented ViewHelpers for Twitter Bootstrap.

In the previous article I gave a brief overview of how the current implementation of my Domain Layer looks. To be able to use these classes inside ZF2 controllers I used a custom ServiceManager Factory. This custom ServiceManager will take care of Dependency Injection and register configured DomainServices

Continue reading »

Domain Layer with Doctrine2

UPDATE: ctrllib has been implemented in CtrlSkeleton, providing easy database management for Doctrine entities and implemented ViewHelpers for Twitter Bootstrap.

In a previous post I explained briefly why you need a Service Layer, since then a lot of things have changed, so here’s an update of how I handle things at the moment.

For starters I’m going to stop calling it a service layer and use the term Domain Layer, which is more adequate:

  • my implementation provides a base DomainModel and provides additional domain model related classes (e.g.: Form)
  • concerning service classes are related to the domain model
  • ZF2 adds its own ServiceManager so in the ZF2 world, a Service is something much broader

I’ve been working on a new implementation which can be found on my github: ctrl-f5/ctrllib. please check the github repo for a complete and up-to-date implementation.

This article gives a brief overview of how the base Domain related classes look, integration in ZF2 will follow.

Continue reading »

GoDaddy and SOPA: I’m moving [UPDATE: Done!]

There has been a lot of fuzz the last weeks about GoDaddy supporting SOPA.

Not only are they supporting the bill, resitricting a free and open internet, they are also releasing false statements to try and calm down customers by saying they dropped support. On the day of this writing however they have not dropped their support officially.

No matter wether they will drop their support, i’m already looking too move my 5 domains plus hosting away from the company. If an internet company does not get how bad such a bill can, and will, be from the second they lay their eyes on it, they do not deserve any of my money.

I must say i have never had problems with their services or anything, but supporting SOPA is really a statement of complete idiocy, in which i will not partake. This is a completely selfish act on the behalf of GoDaddy imo, so they will loose me, among many others as a customer in the weeks to come…

If you too are a customer, please inform yourself about SOPA.

Note to Self

ubuntu php dev prep commands:
sudo apt-add-repository ppa:dominik-stadler/subversion-1.7
sudo apt-get update
sudo apt-get install vim php5 apache2 libapache2-mod-php5 mysql-server phpmyadmin imagemagick subversion git
sudo apt-get install php-pear php5-xdebug php5-sqlite php5-curl php5-svn php5-mcrypt php5-ldap php5-imagick phpunit
sudo a2enmod rewrite
echo 'ServerName localhost' | sudo tee /etc/apache2/httpd.conf
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf
sudo service apache2 restart

pear prep commands:
sudo pear upgrade
sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover pear.symfony-project.com
sudo pear channel-discover components.ez.no
sudo pear install phpunit/PHPUnit --alldeps

mounting shared folder in virtualbox:
sudo mkdir /mnt/workspace
sudo chmod 777 -R /mnt/workspace
echo '[FOLDERNAME]  /mnt/workspace  vboxsf  defaults,rw 0   0' | sudo tee /etc/fstab
sudo mount -a

install sun java jdk to run PHPStorm
sudo add-apt-repository "deb http://archive.canonical.com/ maverick partner"
sudo apt-get update
sudo apt-get install sun-java6-jdk

(thx to this blog for the tip on the repo)

install openVPN support in gnome
sudo apt-get install network-manager-openvpn-gnome