Pragmatism in the real world

Zend\ServiceManager configuration keys

Zend\ServiceManager is usually configured in two places: an array in a config file or a method within your Module class. In either case, you provide a nested array of configuration information. For example, in a config file: return array( 'service_manager' => array( 'invokables' => array( 'session' => 'ZendSessionStorageSessionStorage', ), 'factories' => array( 'db' => 'ZendDbAdapterAdapterServiceFactory', ), ) ); Within the service_manager array, there are a set of nested arrays which are generally used to configure… continue reading.

Sending an HTML with text alternative email with Zend\Mail

Sending a multi-part email with Zend\Mail is easy enough, but if you want to send an HTML email with a text alternative, you need to remember to set the content-type in the headers to multipart/alternative. As this is the second time I had to work this out, I'm noting it here for the next time I forget! use Zend\Mail; use Zend\Mime\Message as MimeMessage; use Zend\Mime\Part as MimePart; function sendMail($htmlBody, $textBody, $subject, $from, $to) { $htmlPart… continue reading.

Module specific layouts in ZF2

If you need different layout scripts to be rendered for different modules in Zend Framework 2, then Evan Coury has made this extremely easy. His new module EdpModuleLayouts is just the ticket! Once installed, you simply have to add a new array to a config file in the config/autoload folder with the following in it: array( 'module_layouts' => array( 'Application' => 'layout/application', 'ZfcUser' => 'layout/user', ), ); i.e. you provide a list of the module… continue reading.

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.