Pragmatism in the real world

Setting up required fields that can be empty with Zend\InputFilter

When you create an input filter entry that has the required element set to true, then by default, the field is set up so that it must have a value in it. If you want the field to be required, but also an empty value is okay, then add the allow_empty element to your definition: $inputFilter->add(array( 'name' => 'notes', 'required' => true, 'allow_empty' => true, )); In this definition, we have a 'notes' element that… continue reading.

Updated ZF2 tutorial for Beta 5

Zend Framework 2, Beta 5 has been released! This is an important release as we think we're at the point where the API has stabilised and expect only small BC breaks between Beta5 and the stable release. We also have two new components: ZendI18n for localisation and translation ZendEscaper for context-specific escaping that targets HTML, HTML attributes, URLs, CSS, and JavaScript. Lots of other changes happened too, so I recommend reading the announcement for all… continue reading.

Zend Framework 2 beta 4 released

Earlier this week, we got beta 4 of Zend Framework 2 out of the door. This version has some very significant improvements in it which mean that if you're following along at home with the betas, then you're going to be doing a bit of updating! Most of the B/C breaks are noted in this thread. I have updated my Zend Framework 2 tutorial to match this release too. The key new features for me… continue reading.

An introduction to ZendEventManager

Zend Framework 2's EventManager is a key component of the framework which is used for the core MVC system. The EventManager allows a class to publish events that other objects can listen for and then act when the event occurs. The convention within Zend Framework 2 is that any class that triggers events composing its own EventManager. Terminology For the purposes of this article, we will use these definitions: An EventManager is an object that… continue reading.

Access view variables in another view model

Unlike Zend Framework 1, the view layer in Zend Framework 2 separates the variables assigned to each view model. This means that when you are in the layout view script, you don't automatically have access to variables that were assigned the the action's view model and vice versa. Accessing action variables in the layout Consider this controller code: class IndexController extends ActionController { public function indexAction() { return array('myvar' => 'test'); } } If you… continue reading.

Returning JSON using the Accept header in ZF2

2 December 2012: Note that as of ZF2.0.4, this no longer works due to changes to fix a security issue. See the release notes for further information. Following yesterday's article on returning JSON from a ZF2 controller action, Lukas suggested that I should also demonstrate how to use the Accept header to get JSON. So this is how you do it! Set up the JsonStrategy We set up the JsonStrategy as we did in returning… continue reading.

Returning JSON from a ZF2 controller action

The new view layer in Zend Framework 2 can be set up to return JSON rather than rendered HTML relatively easily. There are two steps to this: Set up the JsonStrategy Firstly we need to set up the view's JsonStrategy to check to a situation when returning JSON is required and then to render out JSON for us. The JsonStrategy will cause the JsonRenderer to be run in two situations: The view model returned by… continue reading.

A list of ZF2 events

Both the Module Manager and the MVC system use the Event Manger extensively in order to provide "hook points" for you to add your own code into the application flow. This is a list of the events triggered by each class during a standard request with the Skeleton Application: Module Manager Zend\Module\Manager: loadModules.pre For every module: Zend\Module\Manager: loadModule.resolve Zend\Module\Manager: loadModule Zend\Module\Manager: loadModules.post Application Successful: Zend\Mvc\Application: bootstrap Zend\Mvc\Application: route Zend\Mvc\Application: dispatch Zend\Mvc\Controller\ActionController: dispatch (if controller extends… continue reading.

Some Zend\View examples

With the release of Beta 3 of Zend Framework, we now have a significantly refactored the ZendView component. One of the changes made is that there is a ViewModel object that is returned from a controller which contains the variables to be used within the view script along with meta information such as the view script to render. The really nice thing about ViewModels is that they can be nested and this is how the… continue reading.