Pragmatism in the real world

Injecting configuration into a ZF2 controller

One thing you may find yourself needing to do is access configuration information in a controller or service class. The easiest way to do this is to use the ServiceManger's initialiser feature. This allows you to write one piece of injection code that can be applied to multiple objects. It's easier to show this in action! Let's assume that we have this configuration file: config/autoload/global.php: return array( 'application' => array( 'setting_1' => 234, ) );… continue reading.

Simple logging of ZF2 exceptions

I recently had a problem with a ZF2 based website where users were reporting seeing the error page displayed, but I couldn't reproduce in testing. To find this problem I decided to log every exception to a file so I could then go back and work out what was happening. In a standard ZF2 application, the easiest way to do this is to add a listener to the 'dispatch.error' event and log using ZendLog. To… continue reading.

Changing the format of a ZendForm DateTime element

If you want to change the format of the value of a DateTime element, the easiest way to do this in your Form class is to do this: $this->add(array( 'name' => 'next_appointment', 'type' => 'ZendFormElementDateTime', 'options' => array( 'label' => 'Next callback time', ), 'attributes' => array( 'min' => '1 Jan 2013, 00:00', ), )); $this->get('next_appointment')->setFormat('j M Y, H:i'); The two things to note: You can't set the format within the array – it has… continue reading.

Thoughts on module directory structure

I've been working on a Zend Framework 2 module within a larger project that doesn't have that many PHP class files. Specifically, it has a controller, a mapper, an entity, a service and a form. As a result, the traditional Zend Framework 2 directory structure for the Account module looks like this (with class names in brackets): module/ Account/ config/ src/ Account/ Controller/ CaseController.php (Account\Controller\CaseController) Entity/ CaseEntity.php (Account\Entity\CaseEntity) Form/ CaseForm.php (Account\Form\CaseForm) Mapper/ CaseMapper.php (Account\Mapper\CaseMapper) Service/… continue reading.

Using ZendSession

This is a quick note on how to use ZendSession. Although the component name is ZendSession, you actually interact with ZendSessionContainer to store and retrieve session data: use ZendSessionContainer; $session = new Container('SomeKeyName'); ZendSessionContainer's constructor takes a string argument which is the name for this container ('SomeKeyName' in this case). It's optional and if you don't set it, then it is set to 'Default'. The name allows you to use the same session keys in… continue reading.

Integrating BjyAuthorize with ZendNavigation

If you are using BjyAuthorize for ACL configuration and want to use ZendNavigation's ZendAcl integration features, then you need to set the Acl and Role information into ZendNavigation. The easiest way to do this is to add the following to ApplicationModule::onBoostrap(): $sm = $e->getApplication()->getServiceManager(); // Add ACL information to the Navigation view helper $authorize = $sm->get('BjyAuthorizeServiceAuthorize'); $acl = $authorize->getAcl(); $role = $authorize->getIdentity(); ZendViewHelperNavigation::setDefaultAcl($acl); ZendViewHelperNavigation::setDefaultRole($role); This assumes that you've set up BjyAuthorize with some resources and… continue reading.

Introducing AkrabatSession

One of the requirements for a new app that I'm writing is that it has a specific session name. In Zend Framework 2, this is done by creating a SessionManager with the correct configuration and then setting the default manager on the Session Container: use ZendSessionConfigSessionConfig; use ZendSessionSessionManager; use ZendSessionContainer; $sessionConfig = new SessionConfig(); $sessionConfig->setOptions(array('name'=>'MY_SESSION_NAME'); $sessionManager = new SessionManager($config); Container::setDefaultManager($sessionManager); Obviously, I need to be able to configure the name (and potentially other session configuration… continue reading.

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.