Pragmatism in the real world

Initial notes on Zend_Application

Zend_Application is now in the incubator and being actively developed for version 1.8 of Zend Framework. I've had a little play using it with a standard ZF application with no modules and this is what I've worked out so far. As a result this post rambles a bit; sorry about that. Zend_Application is intended to make bootstrapping your application easier, presumably with less code. It also has a new autoloader, Zend_Loader_Autoloader, but I haven't worked… continue reading.

On models in a Zend Framework application

Let's talk about writing models that communicate with databases within a Zend Framework application. It's a popular topic at the moment as there's been a few threads recently on the ZF mailing lists about creating models. The "is a" relationship When working with models within Zend Framework, the simplest solution is to extend Zend_Db_Table_Abstract and Zend_Db_Table_Row_Abstract along these lines: class Users extends Zend_Db_Table_Abstract { protected $_name = 'users'; protected $_rowClass = 'User'; public function fetchAllInLastNameOrder()… continue reading.

Zend Framework in Action Errata

Note that ZFiA was written for version 1.5 of Zend Framework. If you are using version 1.8 or later, then please read this article on Zend Loader. This is a list of issues that we are aware of: Chapter 2 On page 18, the right hand bracket in the first paragraph is not needed. On page 38, in the last code block, the first line of the method should be: $cutOff = date('Y-m-d', strtotime('-3 months'));… continue reading.

File uploads with Zend_Form_Element_File

Now that Zend Framework 1.7 has been released, I thought I'd take a look at the built in file upload element, Zend_Form_Element_File, and see how it can be used. This is how to use it in its most basic form. I decided to use the same set of form elements as before in order to make things easy. Let's start with the form: The form We extend Zend_Form and store it in the application/forms folder… continue reading.

Hooks in Action Helpers

Following on from the discussion on Zend Framework Action Helpers, let's talk about hooks within them. Hooks are a feature of action helpers that allow you to automatically run code at certain points in the dispatch cycle. Specially, there are two hook functions available for action helpers: preDispatch(): runs before the action function is called postDispatch(): runs after the action function has completed These allow you to ensure that some functionality is always run for… continue reading.

Using Action Helpers in Zend Framework

When you have some functionality that needs to be shared across multiple controllers, one method is to use action helpers. Action helpers are very powerful and contain hooks to automatically run when you need them too, but you can ignore all that if you don't need it. The first thing you need to do is decide where to put them. The latest default project structure document recommends using a sub folder from your controllers directory.… continue reading.

Notes on Zend_Cache

Recently I needed to speed up a legacy project that makes a lot of database calls to generate each page. After profiling, I discovered that 90% of the database calls returned data that rarely changed, so decided to cache these calls. One of the nice things about Zend_Framework is that its use-at-will philosophy means that you can use any given component with minimal dependencies on the rest of the framework code. In my case, I… continue reading.

Zend Framework URLs without mod_rewrite

Some of our Zend Framework applications have to run on IIS without ISAPI_Rewrite installed. In these cases we need urls of the form http://www.example.com/index.php?module=mod&controller=con&action=act. I couldn't get this to work out of the box with Zend Framework 1.5, so wrote my own router called App_Controller_Router_Route_RequestVars. This code obviously only supports what I needed and I've only tested it on IIS for Windows 2003 Server, so you may need to tweak to make it do what… continue reading.

Top Tip: XHTML with Zend Form Elements

When you render a Zend_Form, the elements will render to HTML compliance rather than XHTML compliance, even if you have < ?php echo $this->doctype('XHTML1_STRICT');?> at the top of your layout script. Practically, this means that all the input elements do not end in "/>". To resolve this, you need to call the doctype() view helper prior to rendering your form. Within my projects, I do this within a front controller plug-in called ViewSetup that looks… continue reading.

Simple Zend_Form File Upload Example Revisited

I've been thinking about the Simple Zend_Form File Upload Example that I discussed last month. To recap, if you haven't read the comments, if the form fails to validate for some reason then you get a nasty error: Warning: htmlspecialchars() expects parameter 1 to be string, object given in /Users/rob/Sites/akrabat/Zend_Form_FileUpload_Example/lib/Zend/View/Abstract.php on line 786 Essentially, what is happening is that the App_Form_Element_File class that we wrote assigns the $_FILES array to the $value parameter for the… continue reading.