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.
UPDATE!
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:
module/Album/config/module.config.php
|
1 2 3 4 5 |
'controller' => array(
'classes' => array(
'album/album' => 'Album\Controller\<wbr>AlbumController',
),
), |
I replaced with this:
|
1 2 3 4 5 |
'controllers' => array(
'invokables' => array(
'album/album' => 'Album\Controller\<wbr>AlbumController',
),
), |
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:
module/Album/src/Album/
|
1 |
class AlbumController extends AbstractActionController |
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:
|
1 |
class Album extends \ArrayObject implements InputFilterAwareInterface |
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:
|
1 |
$this->resultSetPrototype = new ResultSet(new Album); |
to:
|
1 |
$this->resultSetPrototype = new ResultSet(ResultSet::TYPE_ARRAYOBJECT); |
Next I had to modify the 2 fetch methods to return the Album object:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public function fetchAll()
{
$resultSet = $this->select();
$results = array();
foreach ($resultSet as $r)
{
$results[] = new Album($r);
}
return $results;
} |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
public function getAlbum($id)
{
$id = (int)$id;
$rowset = $this->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return new Album($row);
} |
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:
module/Album/view/album/album/
module/Album/view/album/album/
module/Album/view/album/album/
module/Album/view/album/album/delete.phtml
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/
|
1 2 3 4 5 |
<dt><?php echo $this->formLabel($form->get('title')); ?></dt>
<dd><?php
echo $this->formInput($form->get('title'));
echo $this->formElementErrors($form->get('title'));
?></dd> |
to
|
1 |
<?php echo $this->formRow($form->get('title')); ?> |
as well as removed the html <dl> tags.
Finally Request::post() is now Request::getPost() so I had to change all references to
|
1 |
$request->post() |
to
|
1 |
$request->getPost() |
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”.
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.
I did consider using the AbstractRowGateway but thought it would change the code too much from the tutorial. I am looking forward to seeing how Rob does this and whether he make use of the other Zend\Db classes.
I’ve just read this post http://www.michaelgallego.fr/blog/?p=190 and now I’m thinking ClassMethodsHydrator() might be a better way of dealing with this
Pingback: Unit testing a Zend Framework 2 Controller | Dev Blog