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, 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.

The Zend Framework, whilst a great tool, is often slighted for being big and bloated.  Now I’m not contesting that, 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.

You might also be interested in these tutorials too...

Tue, Nov 15, 2011

Rename uploaded files with Zend Framework

Recently I was asked how to rename a file with the Zend Framework that used a Zend Form and Zend File element. They key requirement was that it should not be a hack or a kludged solution. So I thought I’d write a quick post to provide a simple example on how it was achieved.

Wed, Nov 9, 2011

The Zend Framework Bootstrap made simple (Part 3)

Ok, we’ve established that with the Zend Framework, we need to do a bit more work than some of the other frameworks to get up to speed - but that’s not necessarily a bad thing - right?! But it can be a bit tedious and it’s something as professional developers, we want to automate away. So we’ve been addressing in this series how to do just that with a custom, extendable bootstrap class.

In the first part of the series we laid the foundation of our custom bootstrap class by creating a custom class directory structure, adding its namespace to the application ini and modifying the default application bootstrap so that it extends from it and had a look at the first component - caching.

Then, in the second part of the series, we built on the foundation laid in part one by creating plugin resources for the routing table, application navigation and the database connections - these being some of the most common tasks, usually, associated with a web-based application.

In this, the third and final part of the series, I’m going to finish up by looking at application placeholders and surprise, no not logging as originally promised, but pagination. As an added extra, we’re going to be using a key feature of Zend Application to make it a breeze.

Wed, Nov 2, 2011

The Zend Framework Bootstrap made simple (Part 2)

In the first part of the series, you’ll remember that we laid the foundation of our custom bootstrap class by creating a custom class directory structure, adding its namespace to the application ini and creating our custom bootstrap file that our application bootstrap will extend from.

After we did that, we put in the first but arguably the most important plugin resource – caching and stored it in the application registry. In this post we’re going to be building on that work and adding in three new plugin resources: routing, navigation and databases.

Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus