LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Sunday, March 16. 2008

Zend Framework 1.5 is on its way!

As many know, Zend Framework 1.5.0 is almost ready for release... heck, it might even be released by the time you read this. There are a ton of new features worth looking into, but I'll list some of my own favorites here - the ones I've been either working on or using.

Zend_Layout and Zend_View enhancements

I've been using the Two Step View pattern for years now, and its practically second nature with each project to set it up. With Zend_Layout now released, I no longer have to setup a plugin of my own and think about how it all works -- I can just create my site layout and start working.

But Zend_Layout by itself isn't that revolutionary. What is revolutionary is the combination of Zend_Layout with a variety of new view helpers: partials, placeholders, and actions. In particular, placeholders are increasingly finding a place in my toolbox.

As an example, I use the new headScript() and headLink() view helpers in my applications a ton. They allow me to specify javascript and CSS includes for my HTML head section... and also prevent me from accidently including the same file multiple times. Add to this the fact that, as placeholders, they can also capture arbitrary content for later inclusion, I can now create javascript that I need for a particular view in that view and include it in the document head easily. This helps me keep my UI logic close to the application, while still keeping it located in the appropriate place in the document.

Zend_Form

To be honest, about half-way through writing Zend_Form, I was seriously wondering if a form component was necessary; having Zend_Filter_Input directly in your model, and simply writing your forms using Zend_View helpers seemed perfectly straightforward and painless.

However, I've had a chance to play with Zend_Form a fair bit since then, and I'm very much appreciating the component. With it, I can put all the information about an element -- how it should validate, any pre-filtering I want, any metadata I want to associate with it, and also hints as to how I want it rendered -- all in one place. While it seems like a dangerous mix of business logic and presentation logic, it's not. The display logic is self-contained in the decorators used to render the element -- and are not even necessary. But having them present also means that my views can be much simpler than they were in the past, as can my controller and/or model logic -- I can pull all the information in from the form, and simply use it.

As an example, consider this controller action:


<?php
class FooController extends Zend_Controller_Action
{
    public function formAction()
    {
        $request = $this->getRequest();
        $form    = $this->getForm(); // helper method to pull in form

        if (!$request->isPost()) {
            // display form
            $this->view->form = $form;
            return;
        }

        if (!$form->isValid($request->getPost())) {
            // form not valid; re-display
            $this->view->form = $form;
            return;
        }

        // Valid form; save data
        $this->getModel()->save($form->getValues());
        $this->_helper->redirector('success');
    }
}
?>
 

And the related view script for displaying the form:


<h2>Please fill out the form</h2>
<?= $this->form ?>
 

Seriously -- while there may be a fair amount of code to setup the form, it's self-contained, and leads to some seriously shorter methods and views -- making the rest of the application logic much cleaner and easier to read. I like clean and easily read code.

Zend_Search_Lucene

Sure, this component has been around for a long time, but Alex has done some increcible stuff with it. You can now build queries programmatically, and have support for wildcard searches. Additionally, you now have the ability to modify the index in place, without re-building -- which means that as your site grows, you don't have to worry that indexing the site is going to consume more and more resources.

Zend_Db_Table Improvements

One thing that has always been a thorn in my side is that I will often want to do a query that includes a JOIN from within a Zend_Db_Table... but it hasn't allowed this. This has led to having custom methods that return arrays, or adding logic that returns custom ResultSets... all of which is tedious. Simon has put in a ton of time in the past few months to make these headaches go away. You can now return read-only resultsets that contain joins, letting you access your JOIN'ed rowsets just as you would one containing only the current table's data.

Additionally, instead of needing to build your SQL by hand, or build it with Zend_Db_Select and then cast it to a string, Simon has also added the ability to create Zend_Db_Select's directly in your table classes, and pass them to any method that performs a query. This improvement will also save me time -- particularly when coupled with the ability to return JOINed ResultSets.

Context Switching and REST

Something I've been playing with for some time now is the ability to change view context by simply changing a parameter in the request. For example, you may want to return XML or JSON in addition to HTML from a given action. One thing I've added for 1.5.0 is a ContextSwitch action helper that helps automate exactly this.

With your actions ContextSwitch enabled, you can then request an action and add a 'format' parameter with the desired context: /foo/bar/format/xml. The parameter is detected, checked against a list of allowed contexts, and then, if available, a separate view is rendered and the response headers changed to match the format.

In the above example, /foo/bar/format/xml, this would mean that a response Content-Type header of 'text/xml' would be returned, and instead of rendering the foo/bar.phtml view script, the foo/bar.xml.phtml script would be rendered. This allows you to have separate display logic for different contexts.

In addition to context switching, at the request of some vocal contributors, I've added a variety of methods to Zend_Controller_Request to allow detecting the various HTTP request types (HEAD, GET, POST, PUT, DELETE, etc.), as well as retrieving raw post data. These, combined with context switching, will allow you to build REST services into your existing MVC apps trivially.

Conclusion

There are a ton more features coming out with 1.5.0. Visit the framework site in the coming days to see what all is new, and download the new release to give it a try!

Posted by Matthew Weier O'Phinney in PHP at 21:26 | Comments (8) | Trackback (1)
Defined tags for this entry: mvc, php, zend framework
Related entries by tags:
Autoloading Benchmarks
Applying FilterIterator to Directory Iteration
Running mod_php and FastCGI side-by-side
Creating Zend_Tool Providers
State of Zend Framework 2.0

Trackbacks
Trackback specific URI for this entry

Using Zend_View Placeholders to Your Advantage
Somebody asked for some examples of how I use the headLink(), headScript(), and other placeholder helpers, so I thought I'd take a crack at that today. First off, let's look at what these helpers do. Each are concrete instances
Weblog: phly, boy, phly
Tracked: Mar 18, 21:21

Comments
Display comments as (Linear | Threaded)

Sweet - been waiting for 1.5. Thanks for the work.
#1 Ultra on 2008-03-16 23:45 (Reply)
Congrats on the upcoming release and thank you for all your hard work on making it happen! It really is awesome how much ZF has matured in such a short period of time.
#2 Bradley Holt (Link) on 2008-03-17 09:47 (Reply)
Excellent. Thanks for all your hard work on Zend_Layout and Zend_Form recently, Matthew!

Regards,

Rob...
#3 Rob... (Link) on 2008-03-17 10:28 (Reply)
Awesome!
If I saw you guys in real life, I would literally kiss you (Matthew, Rob, and others) for doing this! :-) jkjkjkjk

@Matthew, I would definitely want to look into your "toolbox" :-P Do you have it online somehwere? Can you show us and example of headScript() and headLink()?
#4 wenbert (Link) on 2008-03-17 23:06 (Reply)
Keep your kisses to yourself... but I'll accept a beer if I see you ;-).

Regarding the Zend_View helpers, I've made another post: http://weierophinney.net/matthew/archives/163-Using-Zend_View-Placeholders-to-Your-Advantage.html
#4.1 Matthew Weier O'Phinney (Link) on 2008-03-18 17:22 (Reply)
;) thanks Matthew!
#4.1.1 wenbert (Link) on 2008-03-18 21:31 (Reply)
For switching representations, why require the resource URI to be changed? I'd rather the HTTP 'Accept' header have been used, as many representations for a given resource (identified by a single URI) is more aligned with HTTP.
#5 tobyjoe (Link) on 2008-03-28 11:32 (Reply)
Dudes -- Love 1.5 ... really appreciate it ...
#6 Matthew Gravatt on 2008-04-03 09:47 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Twitter
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back September '10
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Planet PHP
  • Zend Framework, where I'm project lead
  • Sebastian Bergmann
  • Cal Evans
  • Shahar Evron
  • Paul M. Jones
  • Bill Karwin
  • Mike Naberezny
  • Fabien Potencier
  • Ben Ramsey
  • Derick Rethans
  • Ralph Schindler
  • Marco Tabini

Archives

September 2010
August 2010
July 2010
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml apache
xml best practices
xml books
xml conferences
xml cw09
xml decorators
xml dojo
xml dpc08
xml file_fortune
xml git
xml linux
xml mvc
xml oop
xml pear
xml perl
xml personal
xml php
xml phpworks08
xml programming
xml rest
xml ubuntu
xml vim
xml webinar
xml zendcon
xml zendcon08
xml zendcon09
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net