Redirecting in Zend Framework

While working with Zend Framework, you must have asked yourself “Is there an easy way to redirect to another controller/action?”

Well, there is. When redirecting inside a controller, the first method we think about will probably be $this->_redirect(). This is quick, but it only accepts a URL. This means you have to create a URL to a specific route yourself. Either with the $this->view->url() URL view helper (which is not a pretty sight) or by assembling a route.

Yes, there is also $this->_forward() which sends your request to another controller/action, but it also goes through all init() logic again and forwarding isn’t suited for every situation (e.g. after a form POST).

But there is a far more flexible way: the less known Redirector action helper. Actually, $this->_redirect() uses it, but it provides easier ways to redirect to a specific route:

Its usage will be self explanatory (actually the parameters are the same as the URL view helper), but I want to add a little note about the last parameter (which the URL view helper also accepts): if true, it “resets” your route, meaning all other parameters currently in your Request object are removed. This implies that when setting it to false (which is the default) parameters you have in your “current URL” will be reused (unless overwritten).
This can be particularly useful when passing search query, paging,… parameters from one page to another.

So, next time you need to redirect: pull out the Redirector action helper.

Stay informed on Zend Framework

When developing with Zend Framework, it is difficult to keep track of new versions, new components, changes to components, roadmaps,…

After “Beginning Zend Framework“, you’ll need to stay current too. You can of course read everything on the wiki, but if you use a RSS reader (if not, you really need to) add this feed.

It provides a great overview of all proposals and discussions concerning components. Even if you don’t have time to follow up on this each day (there can be a lot of comments), by looking at the titles of the posts you can have an idea of what is going on.

Beginning Zend Framework

One of the consequences of the huge popularity of Zend Framework is that everyone who is using PHP in one way or another has at least thought about using it (because it all sounds so good and, possibly, easy).

But different people need different approaches to start developing with Zend Framework. And the publishing companies that published the first books about Zend Framework (and apparently also the second wave of books) tend to forget about some groups of users. A little summary (in no way meant to offend):

Developers with a different background (another programming language)

Depending on the background, migration will not be that hard. I can imagine a .NET or Java developer easily gets the principles of a framework (and hopefully also understands OO programming). Ruby or Python will be even easier (but why on earth would they migrate?).

The average PHP developer (not already using Zend Framework of course)

If the Zend Framework manual is not sufficient for this group, there certainly are a lot of books available that eliminate all barriers. Of course look for the most recent books, since a lot of versions have been announced the last 2 years and, to be honest, some of the early books were published in a hurry to benefit from being among the first.

Dreamweaver users (often called “webdesigners“)

Yes, this group is also (willingly or unwillingly) pushed to, at least, have a look at Zend Framework. And to be honest, at the moment, I wouldn’t know how they can prevent themselves from creating something that isn’t maintainable in the years to come.
Zend Framework (or any other framework in this case) promises a lot of good things (which may lack in the current PHP snippets, Smarty templates, open source CMS,… now being used). Starting with the manual certainly is tricky. Getting a thorough grip of PHP/OO concepts may sound boring, but you need it.
As a suggestion: don’t set too high expectations and take your time. It may be useful to force yourself to get a PHP certification. Not because you need it, but because it forces you to learn/refresh the PHP basics.

Also: be honest to yourself. If you notice OO concepts don’t interest you, think about it and maybe try something else. Another lightweight framework like CodeIgniter (there is no such thing as “the best framework”) or no PHP development at all may better suit you. No one has ever been happy forcing themselves in a position doing something they don’t like to do!

Dummies

Belonging to this group is no disadvantage. Ok, you don’t have the experience of someone who has been using PHP since before the dot-com bubble burst, but at least you acknowledge this. Starting from scratch (and knowing it) can eliminate a lot of barriers.
Start by learning PHP and fairly early start with a decent PHP OO book and, again, take your time. In my opinion you don’t have to finish 5 courses and 3 books to start with Zend Framework. When learning Zend Framework (e.g. from the manual) you’ll also learn OO principles and best practices, but you need a basic knowledge first.
Also don’t expect that you can add new Zend Framework components on day 2, it will take time.

Did I forget about someone? I certainly hope this post can help as a starting point for new Zend Framework developers (or people thinking about becoming one).

Practical use of QR codes

Popular and widespread in Japan, QR codes (a variant of the well-known bar-codes) certainly provide many opportunities.

strictlyphp-qr-code

The QR code was invented by Denso Wave, Inc of Japan in 1994. They appear on many product packaging in addition to “normal” bar-codes for shipment tracking and other purposes. When scanned, these codes can return numbers and text (e.g. the URL to this blogpost in the example image). The camera on your mobile phone and dedicated scanning software do the rest of the magic.

While their ability to be a link between the “online” and “offline” world can provide many opportunities, it hasn’t really become the hype it could be outside of Japan. Practical implementations are rare. Most likely because of the lack of knowledge by the broad public. Although QR reader software is freely available and a July ’09 research by the University of Essex stated that 68% of UK phone owners can install such an application, most have not.

The biggest difference between the numerous free QR readers (for about any phone brand/model) may be the image quality needed to recognize a code. I’m happy with the  speed of NeoReader but there are also Kaywa Reader and QuickMark barcode reader (among others).

Besides WordPress plug-ins and support in the Google Charts API, there is a PHP library but support in the Zend Framework could stimulate its use even more (Zend_Gdata_Chart anyone)?

Some more implementations:

One useful tip is to shorten URLs through one of the many URL shortening services there are. This generates a smaller, less prominent QR code.

View specific JavaScript with Zend Framework

Because Zend Framework (and in this case also the MVC part) lets you choose how you structure your project, you may have doubted about where to place client-side JavaScript scripts.

icon_javascript

Personally, I place application-wide scripts in something like /media/js/application.js, but in many cases you have scripts that are only applicable to a certain view. Of course we want to avoid having a JavaScript file for each view somewhere in a general JS folder like /media/js.

Putting the JavaScript straight into the view also isn’t optimal since Zend_Layout can for instance put more HTML below it and client-side scripts should be right before the </body> tag.

I thought it would be nice to be able to have [viewname].js files inside the views/[controller] directories. E.g.:

That makes development easier since your view-specific scripts are still as close to your view as possible, but separated in a .js file so your IDE/editor can parse them as JavaScript. In my case I thought it would also be nice if the .js file would be parsed like the view itself meaning that any variables assigned to the view can also be used in the JavaScript file.

The Zend_Controller_Action postDispatch() method provides a possibility to parse and include the client-side script into the inlineScript view helper where it belongs:

As you can see, the regular view script is cloned so that the same information is passed to the JavaScript file.

The only thing you now need to do is subclass Zend_Controller_Action, paste in the code above and use that new class in your controllers:

Happy JavaScripting!

Project: get a quote for USB sticks

usbkey

For Cited (Dutch) I created a dynamic quote request form (Dutch) which allows potential customers to specify exactly what they are looking for (of course limited to the options that are available).

Through a secured back-end, the site-owner can specify which configuration options are available in which order for which products and categories. You may notice a lot of different configurations are possible!

Within the existing site, the MVC of Zend Framework was used to build only the form and back-end. It’s not ideal, but yet again proves the advantage of the ZF use-at-will structure: almost anything is possible.

So you now know where to get your next shipment of USB sticks/braces/cards!

More bikinis

In an attempt to write more (short) posts about completed projects: a first case.

clickini

Last year I built this Bikini store for Bubble (warning: nasty browser resize) and this week a lot of changes were launched. Among them: a few fancy JavaScript effects, a lot of usability improvements and an altered delivery costs calculation.

Zend Framework got upgraded from version 1.5.3 to 1.7.6. Only major consequence was removal of parent directory traversal when including view scripts because of this fix.

PHP versions on shared hosting in Belgium

Since the PHP build version became so important with Zend Framework, I made a list of some (on the Belgian market) prominent hosting companies and the PHP version they offer on their shared hosting.

It does not pretend to be a complete list, but rather an alphabetical list of hosting providers I bump or bumped in to once in a while. The PHP version listed is mentioned on their site or, if not on their site, retrieved through their support.

PHP version Mentioned on site
www.alfahosting.be 5.2.8 (*) No
www.combell.be 5.1.2 Yes
www.eops.be 5.2.6 Yes
www.hostbasket.be 5.2.3 No
www.level27.be 5.2.4 (*)
No
www.nucleus.be 5.2.4 No
www.one.com 5.2.8 No
www.openminds.be 5.2.8 (*) No
www.priorweb.be 5.2.8 No
www.uniweb.be 5.2.4 No
www.weble.be 5.2.6 (*) Yes

(*) they offered to install any version required.

As you can see, Combell is worst for PHP developers. There currently is also no way to get any higher version with them. It is a sad thing.

Besides them, only Weble mentioned (luckily a proper) full version number on their site. Although customers that tend to find the complete PHP version number important are probably a (growing) minority, it can make a sale.

Also, I’m surprised by the cheapest one, One. It’s the only “big” one that offers a recent version.

Please, don’t hesitate to add any others to the list.

Zend Framework PHP requirements

I try to enforce Zend Framework for every new project started. If not in the requirements section of my quote, it is not that hard to convince a client of its advantages.

Tell a client how Zend Framework

  • improves stability,
  • lowers development time

and in particular

  • makes them less dependent on you as other ZF developers can pick up projects you started a lot easier.

Now, please don’t start to think about job security. Just don’t.

But, Zend Framework has a fairly high PHP version requirement. Since version 1.7 Zend has quietly changed the requirements to PHP 5.2.4. In respect of a client’s (shared) hosting, this can be the most difficult part.
This was probably a logical thing to do since the requirements mentioned PHP 5.1.4 until ZF 1.6.2 and pre-5.2.4 versions miss some great features and contain nasty bugs (in reality some ZF components didn’t even run on PHP 5.1.4).

In Belgium however, it is difficult to find a hosting company that supports PHP 5.2.4.

Speed up Zend Framework

The performance of frameworks has always been subject to discussion and Zend Framework is no exception.

But with the release of version 1.7 an appendix has been added to the manual with guidelines to improve performance.

Most notably:

  • How to optimize your include path (even if it is in your vhost conf).
  • Do not use XML translation formats if possible and certainly use caching for these framework parts.
  • Different techniques to improve view rendering.

Read the complete appendix for a detailed explanation.