Archive for the 'ZFBlog tutorial' Category

Zend Framework Blog Application Tutorial - Part 9: Exploring Zend_View and Displaying Blog Entries


With the release of Zend Framework 1.5, the Zend_View component received a long overdue boost in its functionality. It is now no longer a simple template engine, but a powerhouse of features intent on making the dynamic generation of a View as simple and as flexible as possible. This update introduced a few new concepts along with existing ones which form the backbone of the changes. The main ones are as follows:

1. View Helpers
2. Layouts
3. Partials
4. Placeholders

We’ve already met Layouts as represented independently from Zend_View by Zend_Layout. The purpose of Layouts is to setup the HTML which is common to all Views of the current Module, i.e. headers, footers, sidebars, etc. Specific templates can then be rendered, have their output strategically inserted in the Layout body, and the final result send to the client browser. I haven’t gone into huge detail on its use other than to setup global Layouts which is its simplest and most common use. But some digging will reveal more of its functionality, for example, chaining action calls to aggregate output.

View Helpers we’ve also touched on briefly. They are classes which attach new callable methods to Zend_View and were introduced to allow presentation logic to be segregated for reuse in a separate class. For example, Zend_Form uses View Helpers to generate a form by calling on individual helpers such as Zend_View_Helper_FormTextarea, etc. There are also View Helpers for tasks as specific as URL generation, form elements and HTML lists. You can even write your own, and indeed it’s encouraged to keep your templates clear of duplicated code. The next two items on our list are introduced separately, but they are themselves View Helpers with more far reaching uses.

Partials were designed to offer the same benefit of reusable presentation logic that View Helpers offer, only this time for template code itself. For example, our index page involves displaying a list of blog entries. Each entry’s HTML is essentially identical except for the content injected into it. So it would be a good idea for us to create a template for one single entry, add this template as a reusable Partial, and simply render one in sequence for each entry we want to display. The same idea is adaptable for lists, menus, and more.

Then, instead of lots of templates with Entry markup, there is only one! Other templates can simply include it, render it from iterated data, etc. But the biggest selling point is variable scope - a Partial is completely blind to any data other than the data you deliberately give it. This trumps using includes and render() since it significantly reduces surrounding code and removes any chance of variable name clashing in a large application. In fact there’s a subclass called the PartialLoop helper which makes looped rendering of Partials incredibly easy.

Placeholders were conceived for a slightly different reason. The problem with having Layouts, templates, Partials, and View Helpers is that one View can be built up using a lot of different parts, which are unable to communicate with each other without introducing some custom wiring. Placeholders ensure that such wiring is standardised - if you have an entry template which outputs content and needs to change the title of the page, it can use a Placeholder to set the title value, and then you can add a sprinkling of code in your Layout to read that value and inject it into the Layout’s

We’ll see these facets of Zend_View in action as we continue.

Continue reading ‘Zend Framework Blog Application Tutorial - Part 9: Exploring Zend_View and Displaying Blog Entries’

Zend Framework Blog Application Tutorial: Part 8: Creating and Editing Blog Entries with a dash of HTMLPurifier


There’s nothing quite like having a functioning application emerge out of the controlled chaos we know as The Development Process. In Part 8 of the ongoing saga describing how to build a real world blog application using the Zend Framework we finally reach the point at which we concentrate on blog entries. At the end of this Part, we will be able to create and edit entries in preparation for Part 9 when we will explore displaying them to the world!

The reason displaying entries is not addressed here is simple. Display requires a lot of Zend_View work which is deserving of an article to itself before we go too far. A few of you have already noted in comments about our suspicious lack of View Helper usage ;-). By now a few uncomfortable flaws should be becoming apparent in how some template content is becoming duplicated.

So, on with the show already!

Continue reading ‘Zend Framework Blog Application Tutorial: Part 8: Creating and Editing Blog Entries with a dash of HTMLPurifier’

Zend Framework Blog Application Tutorial - Part 7: Authorization with Zend_Acl and Revised Styling


This entry concerns authorization. We previously covered how to authenticate an author to the blog, but we still have nothing ensuring only authenticated authors can access the new Administration Module. This is the domain of Zend_Acl, an implementation of an Access Control List system which limits access to resources by the roles assigned to a user.

In the final section of this entry, we take a small detour into the world of CSS (which rarely works out for me ;-)) where I’ll apply some small changes to our Layouts and add two new stylesheets. Once these are added, our infant blog application will look slightly more presentable than it’s current nakedness.

Step 1: Understanding Access Control Lists (ACL)

It can be a bit confusing to face off against ACL if you’re new to the subject. In essence all ACL does is keep track of resources and roles.

As to what a resource is, it is anything to which access can be allowed or denied. For our blog application, I could decide that the Administration Module is itself one resource. From there I can restrict all access to that entire Module, including all it’s Controller classes and Action methods (which are part of that single Resource). Or perhaps I could determine that only one Action method in the whole Module is a specific Resource, bearing in mind that Resources are nestable (i.e. a basket is a Resource, and each egg it holds are also discrete Resources). Since each Resource can be given differing access rules, you can globally prevent non-author users from accessing the Administration Module, but maybe allow some registered users access to specific Actions in that Module as an exception to the global rule.

Banner

Continue reading ‘Zend Framework Blog Application Tutorial - Part 7: Authorization with Zend_Acl and Revised Styling’

Zend Framework Blog Application Tutorial - Part 6: Introduction to Zend_Form and Authentication with Zend_Auth


In the previous entry, we created a new Administration Module to hold blog management functionality, added a Module specific layout for it, and discussed the upcoming need to ensure this is only accessible by authorised Authors. In this entry I’ll unravel some of Zend_Form’s mysteries in adding a login form, before using Zend_Auth to implement authentication for authors.

Previously: Part 5: Creating Models with Zend_Db and adding an Administration Module

Authentication in the Zend Framework is the domain of the Zend_Auth component, and it is really easy to use. Zend_Auth is really an abstract API to a number of components working in concert, and without the usual micromanagement of database interaction, sessions, cookies and user data persistence, it makes my life a lot simpler. Of course authentication demands a login form, and so I’ll first visit using Zend_Form. Zend_Form is an interesting component because it’s one of the worst to get started with. The manual, as it does for all components, does not impose a best practice to setting up forms. Mix that with the number of form organisations possible (class based, config based, view template based) and it can be very confusing.

Continue reading ‘Zend Framework Blog Application Tutorial - Part 6: Introduction to Zend_Form and Authentication with Zend_Auth’