Page 1 of 1

My ZF Implementation

Posted: Wed Apr 18, 2007 12:21 am
by bpopp
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Some of you may remember that I was originally a little skeptical of ZF. Well, I've spent a few days working with it and so far I've been very impressed with how easy it is to enhance. This is the sign of a very well-designed system. For the curious (bored), here's a quick breakdown of what I've done w/ my ZF engine:

My view renders using Smarty. I hate embedding PHP in a view. That whole <?php $this->escape ( $this->blah ); ?> crap is really tedious and ugly once you have any significant content. If you're gonna throw all that PHP in the view, you might as well just keep it in the controller as far as I'm concerned. Smarty-style tags, ie. {$blah}, are so much cleaner. Or better yet, {$blah|strip_tags|capitalize|truncate:10}. Very cool and it's a lot friendlier to HTML designer types. 

I also added a new abstract class called MyZend_View_Helper. This overrides my normal view class and can be used to build special helpers. My Helpers are small, little blocks of code that render something for a view/layout. They are put in a special folder at the root of the application called, not surprisingly, "helpers" and can be sucked into any view or layout with a custom Smarty function like so:

[syntax="smarty"]{helper name="LayoutHelper" method="DisplayMenu"}
So here's a typical layout (notice the helpers):

Code: Select all

<body>
<h1>BPopp Sanctuary</h1>
<div id="nav">{helper name="LayoutHelper" method="mainMenu"}</div>

<table width="100%" cellpadding="10">
<tr>
<td width="650" valign="top">
<div id="content">
{$CONTENT}
</div>
</td>
<td valign="top">
<p>{helper name="LayoutHelper" method="identity"}</p>
<p>{helper name="LayoutHelper" method="quoteOfTheDay"}</p>
<p>{helper name="LayoutHelper" method="hallOfFame"}</p>
</td>
</body>
In my controller, I added three notable methods:[/syntax]

Code: Select all

function renderViewToLayout ( $view=null, $layout=null, $area="CONTENT" );
function renderToLayout ( $string, $layout=null, $area="CONTENT" );
function renderLayout ( $layout );
So to render something into this layout (main.tpl) I might do this in my controller's action:

Code: Select all

...
$this->renderToLayout ( '<h1>Hello World!!</h1>', 'main', "CONTENT" );

$this->view->rows =  $articleObject->FetchRows();
$this->renderViewToLayout ( 'articles/index.tpl', 'main', "CONTENT" );

$this->renderLayout ('main');
Finally, I extended the Table class so that models are a little more intelligent (ie., cleaner interface and relationships are fetched). A typical implementation looks like this (from a helper):

Code: Select all

class LayoutHelper extends MyZend_View_Helper
{
 	public function quoteOfTheDay()
 	{
 		Zend_Loader::loadClass('Quote');
		$object = new Quote();
		$object->UseRelationship ( "creator" );
		$object->AddOrderBy ( "RAND()" );
		$object->SetLimit ( 0, 1 );			
		$this->assign ( "rows",  $object->FetchRows() );
		return $this->render ( "quotes/view.tpl" );
 	}
}
I'm a little concerned that a major upgrade is going to smash my little fork all to h*ll, but it's all proof-of-concept stuff anyway and the developer's seem to be pretty respectful of this from what I've seen. Anyway, just thought I'd share what I've done so far. I'd love to hear how others are extending the framework and/or get some ideas for improvement.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Apr 18, 2007 2:57 am
by Maugrim_The_Reaper
Yeah, despite the flaws I'm finding the ZF to be right at the top of my list of framework tools. The ability to extend and personalise is worth the pain of a few missing features at this early stage. The Pet Shop thread is going to push that along nicely and we're already meeting interesting enhancement to be added. The cool one at the moment is adding Composite View support (basically an OO version of Layout includes you have but which will allow specialised Helpers skip the Controllers in accessing our Model).
I'm a little concerned that a major upgrade is going to smash my little fork all to h*ll, but it's all proof-of-concept stuff anyway and the developer's seem to be pretty respectful of this from what I've seen. Anyway, just thought I'd share what I've done so far. I'd love to hear how others are extending the framework and/or get some ideas for improvement.
For better or worse, v1.0 of the framework is being called "1.0" because it represents an undertaking by the Zenders to maintain a stable API (it has little to do with actual completion). So your subclassing should remain perfectly fine for many future versions. Should not be more than a few tweaks now and again to support new features.