Getting started with Zend Framework 2

After a little Google search for a good intro into the Zend Framework 2 I came up with Rob Allen aka Akrabat’s tutorial. I’d had a little play with ZF2 before when it was in beta 3 or beta 4 but after opening up beta 5 I was quite surprised to see how much things had changed.

Rob has not yet updated his tutorial but I understand an update is coming very very soon, while meaning that some things in the tutorial didn’t work it gave me the opportunity to do some digging into the Zend Framework code & documentation to get it up and running.

Rob has now updated his tutorial which you can download from here.

Here I’m going to list the changes I made to get it up and running. This is really for reference purposes and if anyone else is doing this I recommend you head over to Rob’s site and get an update tutorial when he’s done it.

First up the controller wasn’t being recognised but thanks to a post on StackOverflow I found a solution, it appears the Autoload config has changed slightly so had to make the following changes:


I replaced with this:

Next up is appears Zend\Mvc\Controller\ActionController no longer exists so I had to change my AlbumController to extend Zend\Mvc\Controller\AbstractActionController instead:


At this point my controller was recognised and was being called. The next problem however was the Zend\Db\ResultSet\Row has been removed and ResultSet now only returns either a TYPE_ARRAY or TYPE_ARRAYOBJECT.

This caused some head scratching but I came up with the following solution:
(I Iook forward to seeing what Rob comes up with in his tutorial though.)

First of all changed Album to extend from \ArrayObject instead of Zend\Db\ResultSet\Row, this is because the Zend Form object needs some method provided by ArrayObject in order to bind to the the object. The definition of Album now looked like this:

Next I needed to get AlbumTable to return our Album objects, previously this was done through setting the prototype for the type of object RecordSet should return but this no longer works. The first thing I did was told RecordSet to return ArrayObjects by changing:


Next I had to modify the 2 fetch methods to return the Album object:

Next up I found out the escape() view helper has been replaces with escapeHtml(), escapeJs() & escapeCss() so in this case we needed to change all the escape() calls in the views to escapeHtml() calls instead. The following files were affected:


I also discovered a Form method called getRow() which outputs the <input> & the <label> so I opted to use this so in the module/Album/view/album/album/add.phtmlmodule/Album/view/album/album/edit.phtml files I change blocks that looked like this:


as well as removed the html <dl> tags.

Finally Request::post() is now Request::getPost() so I had to change all references to


in the AlbumController.

That got it all working nicely. I do look forward to seeing Rob’s solutions and also I have discovered the Form Annotation system which looks fantastic but I’ll look at that another time. More info on this can be found here.

Next up I’m going to see if I can get PHPUnit tests to cover all this code, I currently have no idea how to unit test Controller classes so this could be “fun”.

4 thoughts on “Getting started with Zend Framework 2

  1. Nice to see all these changes bundled in one post! Great initiative.

    Went through it as well and was also puzzled when Zend\Db\ResultSet\Row got removed. I decided to go with Zend\Db\RowGateway\AbstractRowGateway, which caused some other issues down the line… Will be curious to see how Rob deals with it.

  2. Pingback: Unit testing a Zend Framework 2 Controller | Dev Blog

Leave a Reply

Your email address will not be published. Required fields are marked *