Zend Framework — 10 Must Dos

The Zend Framework, whilst a great tool, is often slighted for being big and bloated.  Now I'm not contesting that per se but it does have a heck of a lot going for it. So if you're going get the most out of it, then you need these 10 tips.

zf(.sh|bat) is essential

zf is the scaffolding tool available with Zend Framework. Whilst it's not as mature as Gii in the Yii framework, or the scaffolding support in Ruby on Rails, it's better than doing everything manually. With a few commands, that can be automated in a shell script or batch file, you don't need to, manually, create the directory structure for modules, forms et al, you don't need to remember to create all the controllers and their accompanying actions and view files. Logically, why spend the time when you don't need to?

Get to know realpath_cache and realpath_cache_ttl

Let's be honest, the Zend Framework's a bit of a behemoth when it comes to the PHP frameworks available. It's hardly on the lighter end (insert link) of the scale unlike Kohana. The complete library weighs in at around 23Mb, at the time of writing.

No, it doesn't load all of this in to memory at one time, but it does load a lot and go through a lot of file checking, and inclusion, along with the accompanying file stat calls. So do yourself a favour and measure how many files your project's calling and adjust the settings accordingly.

Use modules properly

There are two primary ways of using Zend Framework in an MVC context; Controller-based and Module-based. Whilst controller-based is fine, imho, Module-based gives you a lot more flexibility and leads to a more well organised project structure.

You separate out the content of your project, based on a more logical, efficient structure; such as having all user-related content in a user module, administrative content in an admin module and so on. You can also create plugins relevant only to your module and control fairly acutely when they're run.

Enable layouts

Layouts are a great way of simplifying re-using your work. Through layouts, common view logic, such as headers, footers, copyright and meta tag information, does not have to be repeated unnecessarily, throughout your application This type of content can be put in to, or called from, the layout wrapper, leaving your module and controller views to focus on only the information they need to output. In your project, be sure to run: zf enable layout.

Use FrontController plugins

As I hinted at previously, FrontController Plugins are a great way of encapsulating logic required in your application and keeping it organised in an efficient manner. I have a standard User module that links in to the request predispatch.

It interrogates the user state and the application state and automatically presents either a login or access denied page as the privileges require. The logic is not cluttered through controllers, I don't have to remember to extend classes. It's there whenever it's needed.

Use zfdebug

Zfdebug is a great addition when developing ZF applications. It comes with a range of modules that help you know all about the current state of your application. From database, caching, registry, exceptions to file usage, it shows you in a simple toolbar what's going on in your application. What's more, the way in which it presents the information is far easier to read than a normal stack trace. Check it out – it will really help you.

Enable an opcode cache

This one should be a no-brainer. Whether it's APC, eAccelerator or XCache, without changing a line of code, these will give your code a performance boost of from 50 - 190% (by some reports) just by enabling them. When you start to tweak the configuration settings, you can see even more. I'll skip over how they work, but you can find links in the further reading section at the end of the post.

Integrate security early

This isn't so much a Zend Framework specific issue, so much as a general development consideration. But the thing with this one is that integrating it into Zend Framework properly is fairly straight-forward if you take it as a priority from the start. Whether that's having a good user module, an acl/auth module, it's essential to do.

Integrate caching early

This is one area that I find overlooked so often. I hear a lot of rhetoric around it and hyperbole about it, but in reality, it doesn't always get taken in to consideration seriously. It's so simple to integrate through the framework components, from Zend_Db, Zend_Navigation, Zend_Locale along with the load that Zend Framework can impose, it's important to get this going early and to benchmark it to know that it's working properly.

Know the dispatch process

Arguably, this one should be higher up this list, but to use the MVC components correctly, or as Brandon Savage says, to not fight against the framework and use it properly, you need to know this. You can download a PDF of the process and I strongly encourage you to do so.

Further Reading



About Matthew

Matthew Setter Matthew Setter is a PHP & Zend Framework specialist. If you're in need of a custom software application, need to migrate an existing legacy application, or want to know your current application's GPA - get in touch.

Want To Be A Zend Framework Guru?

Drop your email in the box below, and get awesome tutorialsjust like this one — straight to your inbox, PLUS exclusive content only available by email.