<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Robert Basic &#187; framework</title>
	<atom:link href="http://robertbasic.com/blog/tag/framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://robertbasic.com/blog</link>
	<description>the magic of coding...</description>
	<lastBuildDate>Mon, 30 Jan 2012 13:15:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Loading custom module plugins</title>
		<link>http://robertbasic.com/blog/loading-custom-module-plugins/</link>
		<comments>http://robertbasic.com/blog/loading-custom-module-plugins/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 12:24:28 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=915</guid>
		<description><![CDATA[OK, here&#8217;s a quicky one from the office :P I was trying to load a Front Controller plugin which resides in app/modules/my_module/controllers/plugins/ and not in the &#8220;usual&#8221; lib/My_App/Plugin/. I want this plugin to be called in every request and I want the plugin file to be under it&#8217;s &#8220;parent&#8221; module. Here&#8217;s what I did: added [...]]]></description>
			<content:encoded><![CDATA[<p>OK, here&#8217;s a quicky one from the office :P</p>
<p>I was trying to load a Front Controller plugin which resides in <code>app/modules/my_module/controllers/plugins/</code> and not in the &#8220;usual&#8221; <code>lib/My_App/Plugin/</code>. I want this plugin to be called in every request and I want the plugin file to be under it&#8217;s &#8220;parent&#8221; module.</p>
<p>Here&#8217;s what I did: added the path to the plugin and it&#8217;s namespace to the Zend_Application_Module_Autoloader as a new resource type and then just register the plugin in the front controller in an other _init method.</p>
<p>Code is better, here&#8217;s some:</p>
<pre class="php" name="code">class News_Bootstrap extends Zend_Application_Module_Bootstrap
{
    /**
     * Autoloader for the "news" module
     *
     * @return Zend_Application_Module_Autoloader
     */
    public function _initNewsAutoload()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(
                                array(
                                    'namespace' =&gt; 'News',
                                    'basePath' =&gt; APPLICATION_PATH . '/modules/news'
                                )
                            );

        // adding model resources to the autoloader
        $moduleLoader-&gt;addResourceTypes(
                array(
                    'plugins' =&gt; array(
                        'path' =&gt; 'controllers/plugins',
                        'namespace' =&gt; 'Controller_Plugin'
                    )
                )
            );

        return $moduleLoader;
    }

    public function _initPlugins()
    {
        $this-&gt;bootstrap('frontcontroller');
        $fc = $this-&gt;getResource('frontcontroller');

        $fc-&gt;registerPlugin(new News_Controller_Plugin_Scheduler());
    }
}
</pre>
<p>If anyone knows a better way for doing this, please do share it with me.</p>
<p>Now back to work. Cheerio.</p>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/loading-custom-module-plugins/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Honeypot for Zend Framework</title>
		<link>http://robertbasic.com/blog/honeypot-for-zend-framework/</link>
		<comments>http://robertbasic.com/blog/honeypot-for-zend-framework/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 10:43:14 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[honeypot]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[validator]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=891</guid>
		<description><![CDATA[I just hacked up a little code snippet based on Matthew&#8217;s Honeypot WordPress plugin. It&#8217;s basically just a Validator for a Zend Form element which is hidden from the user via CSS. Cause it&#8217;s hidden, users won&#8217;t see it, but spambots will, well, cause they are bots. If the element is left empty, it&#8217;s valid, [...]]]></description>
			<content:encoded><![CDATA[<p>I just hacked up a little code snippet based on <a href="http://twitter.com/elazar">Matthew&#8217;s</a> <a href="http://matthewturland.com/2010/01/01/im-a-honey-pot/">Honeypot WordPress plugin</a>. It&#8217;s basically just a Validator for a Zend Form element which is hidden from the user via CSS. Cause it&#8217;s hidden, users won&#8217;t see it, but spambots will, well, cause they are bots.</p>
<p>If the element is left empty, it&#8217;s valid, otherwise it&#8217;s not.</p>
<p>So, here&#8217;s the code:</p>
<pre class="php" name="code">class App_Validate_Honeypot extends Zend_Validate_Abstract
{
    const SPAM = 'spam';

    protected $_messageTemplates = array(
        self::SPAM =&gt; "I think you're a spambot. Sorry."
    );

    public function isValid($value, $context=null)
    {
        $value = (string)$value;
        $this-&gt;_setValue($value);

        if(is_string($value) and $value == ''){
            return true;
        }

        $this-&gt;_error(self::SPAM);
        return false;
    }
}
</pre>
<p>I add the element to the form like this:</p>
<pre class="php" name="code">$this-&gt;addElement(
            'text',
            'honeypot',
            array(
                'label' =&gt; 'Honeypot',
                'required' =&gt; false,
                'class' =&gt; 'honeypot',
                'decorators' => array('ViewHelper'),
                'validators' => array(
                    array(
                        'validator' =&gt; 'Honeypot'
                    )
                )
            )
        );
</pre>
<p>There. Done.</p>
<p>Happy hackin&#8217;!</p>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/honeypot-for-zend-framework/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Chaining routes in Zend Framework</title>
		<link>http://robertbasic.com/blog/chaining-routes-in-zend-framework/</link>
		<comments>http://robertbasic.com/blog/chaining-routes-in-zend-framework/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 19:23:23 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[route]]></category>
		<category><![CDATA[routing]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=816</guid>
		<description><![CDATA[On a forum, there was a question today, about adding language &#8220;support&#8221; to the routes using Zend Framework. The guy wanted routes like /en/foo/bar or /de/baz. I wrote there an example for that using Zend_Router_Routes_Chain, so just posting that example here, too :) Image by shoothead via Flickr For what chains are for, is described [...]]]></description>
			<content:encoded><![CDATA[<p>On a forum, there was a question today, about adding language &#8220;support&#8221; to the routes using <a class="zem_slink" href="http://framework.zend.com/" title="Zend Framework" rel="homepage">Zend Framework</a>. The guy wanted routes like <code>/en/foo/bar</code> or <code>/de/baz</code>. I wrote there an example for that using <a href="http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.chain">Zend_Router_Routes_Chain</a>, so just posting that example here, too :)</p>
<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl style="width: 250px;" class="wp-caption alignright">
<dt class="wp-caption-dt"><a href="http://www.flickr.com/photos/66621443@N00/141114012"><img src="http://farm1.static.flickr.com/51/141114012_8cfe928eb5_m.jpg" alt="rusty chain" title="rusty chain" height="160" width="240"></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image by <a href="http://www.flickr.com/photos/66621443@N00/141114012">shoothead</a> via Flickr</dd>
</dl>
</div>
</div>
<p>For what chains are for, is described in the manual, so I won&#8217;t be covering that :P</p>
<p>Basically, we&#8217;re prepending the language route to the other routes. This way, we have defined the route for the languages in one place only, plus, the other routes don&#8217;t have to worry about the language, too.</p>
<pre name="code" class="php">// this goes in the bootstrap class
public function _initRoutes()
{
    $this-&gt;bootstrap('FrontController');
    $this-&gt;_frontController = $this-&gt;getResource('FrontController');
    $router = $this-&gt;_frontController-&gt;getRouter();

    $langRoute = new Zend_Controller_Router_Route(
        ':lang/',
        array(
            'lang' =&gt; 'en'
        )
    );
    $contactRoute = new Zend_Controller_Router_Route_Static(
        'contact',
        array('controller'=&gt;'index', 'action'=&gt;'contact')
    );
    $defaultRoute = new Zend_Controller_Router_Route(
        ':controller/:action',
        array(
            'module'=&gt;'default',
            'controller'=&gt;'index',
            'action'=&gt;'index'
        )
    );

    $contactRoute = $langRoute-&gt;chain($contactRoute);
    $defaultRoute = $langRoute-&gt;chain($defaultRoute);

    $router-&gt;addRoute('langRoute', $langRoute);
    $router-&gt;addRoute('defaultRoute', $defaultRoute);
    $router-&gt;addRoute('contactRoute', $contactRoute);
}
</pre>
<p>Assuming that we have an Index controller, with actions index and contact and a Foo controller with actions index and bar, paired with the routes from the above example, we could do requests like:</p>
<pre>/ =&gt; /index/index/lang/en
/de =&gt; /index/index/lang/de
/sr/contact =&gt; /index/contact/lang/sr
/en/foo =&gt; /foo/index/lang/en
/fr/foo/bar =&gt; /foo/bar/lang/fr
</pre>
<p>Requesting a page like, e.g. <code>/de/baz</code>, would give us a 404 page, cause we don&#8217;t have a Baz controller.</p>
<p>HTH :)</p>
<p>Happy hacking!</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/ea22a463-b7d7-42d3-bf7c-0fcb2e89785b/" title="Reblog this post [with Zemanta]"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=ea22a463-b7d7-42d3-bf7c-0fcb2e89785b" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/chaining-routes-in-zend-framework/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Zend Framework bug hunt days</title>
		<link>http://robertbasic.com/blog/zend-framework-bug-hunt-days/</link>
		<comments>http://robertbasic.com/blog/zend-framework-bug-hunt-days/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 21:37:03 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[hunt]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=808</guid>
		<description><![CDATA[On the 19th and 20th of this month, the third Zend Framework Bug Hunt days were held. I joined the party for the first time and I say, it was a jolly good one! It was announced on Zend DevZone after which Pádraic wrote a nice and detailed Guide To Zend Framework Bug Hunt Days [...]]]></description>
			<content:encoded><![CDATA[<p>On the 19th and 20th of this month, the third Zend Framework Bug Hunt days were held. I joined the party for the first time and I say, it was a jolly good one!</p>
<p>It was announced on <a href="http://devzone.zend.com/">Zend DevZone</a> after which <a href="http://twitter.com/padraicb">Pádraic</a> wrote a nice and detailed <a href="http://blog.astrumfutura.com/archives/423-A-Guide-To-Zend-Framework-Bug-Hunt-Days.html">Guide To Zend Framework Bug Hunt Days</a> (I think I read on IRC that there&#8217;ll be a <a href="http://framework.zend.com/wiki/display/ZFDEV/Monthly+Bug+Hunt+Days">Bug Hunt Day FAQ</a>, too). I decided to try and give back to the community as much as I can. OK, it wasn&#8217;t much &#8211; submitted a patch for an issue and closed another which was lacking information &#8211; but hey! I think it was pretty good for a noob bug hunter like me :P The only downside for me is it that it&#8217;s held on Thursdays and Fridays, which means I can join up only after work. Blah.</p>
<p>All in all &#8211; <a href="http://framework.zend.com/issues/secure/IssueNavigator.jspa?mode=hide&#038;requestId=11199">more than 130 closed issues</a> &#8211; w00t!</p>
<p>So yep, see you all in December on the fourth Bug Hunt days ;)</p>
<p>Happy hacking!</p>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/zend-framework-bug-hunt-days/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Framework 1.8 Web Application Development book review</title>
		<link>http://robertbasic.com/blog/zend-framework-18-web-application-development-book-review/</link>
		<comments>http://robertbasic.com/blog/zend-framework-18-web-application-development-book-review/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 21:14:44 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[review]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=798</guid>
		<description><![CDATA[A few days ago I finished reading Keith Pope&#8216;s book titled &#8220;Zend Framework 1.8 Web Application Development&#8220;, so, after letting it &#8220;rest&#8221; in my mind for a while, here are my thoughts on it&#8230; First, I must point out the “language” of the book – I was expecting a text that&#8217;s hard to follow, that&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I finished reading <a href="http://www.thepopeisdead.com/">Keith Pope</a>&#8216;s book titled &#8220;<a href="http://www.packtpub.com/zend-framework-1-8-web-application-development/book">Zend Framework 1.8 Web Application Development</a>&#8220;, so, after letting it &#8220;rest&#8221; in my mind for a while, here are my thoughts on it&#8230; <div id="attachment_804" class="wp-caption alignright" style="width: 310px"><a href="http://robertbasic.com/blog/wp-content/uploads/2009/11/zf_book.jpg"><img src="http://robertbasic.com/blog/wp-content/uploads/2009/11/zf_book-300x225.jpg" alt="ZF Web App Development" title="zf_book" width="300" height="225" class="size-medium wp-image-804" /></a><p class="wp-caption-text">ZF Web App Development</p></div></p>
<p>First, I must point out the “language” of the book – I was expecting a text that&#8217;s hard to follow, that&#8217;s full of words and sentences requiring at least two dictionaries by my side to help me out (hey, English is not my first language!), but, it was quite an easy and, if I may add, an enjoyable read.</p>
<p>If you think, that you&#8217;re just gonna sit down, read the book and know all about <a href="http://framework.zend.com/">Zend Framework</a>, boy you&#8217;re wrong! Yes, the book explains a lot, but you&#8217;ll still need to follow the example codes along the way and play with them to get really familiar with ZF.</p>
<p>The book starts off with a basic application (yep, “Hello world!”), explains the bootstrapping, configuring, working with action controllers, views and handling errors&#8230; The second chapter continues with explaining the MVC architecture, the front controller, router, dispatcher&#8230; It even has a nice flowchart about the whole dispatch process, great stuff.</p>
<p>From chapter 3 to chapter 12, the author is taking you through a process of building a web application – from creating the basic directory structure, over the hardcore programming stuff to the optimizing/testing part. Chapter 4 gives a rather good explanation on the “Fat Model Skinny Controller” concept; chapter 8 deals with authentication and authorization; chapter 11 takes care of the optimization.</p>
<p>At last, my favourite part of the book is when the author has several “ways out of a problem”, he tells the good and the bad sides of each, picks out the best one and explains why did he choose that particular one. I hate it when an author just simply says: “This is the right way, trust me.”, without caring to explain why.</p>
<p>So, would I recommend this book to a friend who wants to start working with ZF? Absolutely.</p>
<p>Also, be sure to check out what <a href="http://codeutopia.net/blog/feed/">Jani</a>, <a href="http://raphaelstolt.blogspot.com/2009/10/zend-framework-18-web-application.html">Raphael</a>, <a href="http://rob.purplerockscissors.com/feed/">Rob</a> and <a href="http://techchorus.net/zend-framework-18-web-application-development-book-review">Sudheer</a> have to say about this book (Jani&#8217;s and Rob&#8217;s reviews are not up yet, so I&#8217;m linking to their feeds!), too.</p>
<p>Happy reading! :)</p>
<p>Edit 2009., November 23rd: Added a link Sudheer&#8217;s post :)</p>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/zend-framework-18-web-application-development-book-review/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>A book review</title>
		<link>http://robertbasic.com/blog/a-book-review/</link>
		<comments>http://robertbasic.com/blog/a-book-review/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 08:46:29 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[review]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=789</guid>
		<description><![CDATA[On Thursday (October 8th) I was contacted by mr. Priyanka Sanghvi from Packt Publishing. He made me an offer I couldn&#8217;t refuse: to write a review on a new Zend Framework book! OMG! How cool is this? Very! :) The book is titled “Zend Framework 1.8 Web Application Development” and is written by Keith Pope [...]]]></description>
			<content:encoded><![CDATA[<p> <a href="http://robertbasic.com/blog/wp-content/uploads/2009/10/zend-book-image.jpg"><img src="http://robertbasic.com/blog/wp-content/uploads/2009/10/zend-book-image.jpg" alt="zend-book-image" title="zend-book-image" width="100" height="123" class="alignright size-full wp-image-795" /></a> On Thursday (October 8th) I was contacted by mr. Priyanka Sanghvi from <a class="zem_slink" href="http://www.packtpub.com" title="Packt Publishing" rel="homepage">Packt Publishing</a>. He made me an offer I couldn&#8217;t refuse: to write a review on a new <a class="zem_slink" href="http://framework.zend.com/" title="Zend Framework" rel="homepage">Zend Framework</a> book! OMG! How cool is this? Very! :)</p>
<p>The book is titled <a href="http://www.packtpub.com/zend-framework-1-8-web-application-development/book">“Zend Framework 1.8 Web Application Development”</a> and is written by <a href="http://www.thepopeisdead.com/">Keith Pope</a> (<a href="http://twitter.com/muteor">@muteor</a> on Twitter):</p>
<blockquote><p>This book takes you through detailed examples as well as covering the foundations you will need to get the most out of the Zend Framework. From humble beginnings you will progress through the book and slowly build upon what you have learned previously. By the end, you should have a good understanding of the Zend Framework, its components, and the issues involved in implementing a Zend Framework based application.</p></blockquote>
<p>I&#8217;ll publish my review here, soon as I get a copy of the book (it should arrive in a week or two), so stay tuned!</p>
<p>Until then, happy hacking!</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/245ef7ba-9214-453d-bbde-1bfbed58e71b/" title="Reblog this post [with Zemanta]"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=245ef7ba-9214-453d-bbde-1bfbed58e71b" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/a-book-review/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Playing with Zend_Navigation and routes</title>
		<link>http://robertbasic.com/blog/playing-with-zend_navigation-and-routes/</link>
		<comments>http://robertbasic.com/blog/playing-with-zend_navigation-and-routes/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 17:05:20 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[routing]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=762</guid>
		<description><![CDATA[Image by Aurelijus Valeiša via Flickr O hai. First things first — someone should slap me for being such a lazy blogger. Somehow I lost all the motivation I had in the beginning, but looks like it&#8217;s back now :) I finally had the time to play around with the latest Zend Framework version (v [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl style="width: 250px;" class="wp-caption alignright">
<dt class="wp-caption-dt"><a href="http://www.flickr.com/photos/95124659@N00/2570224124"><img src="http://farm4.static.flickr.com/3031/2570224124_07e06c809f_m.jpg" alt="&quot;Zend Framework&quot; and &quot;PHP is th..." title="&quot;Zend Framework&quot; and &quot;PHP is th..." height="180" width="240"></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image by <a href="http://www.flickr.com/photos/95124659@N00/2570224124">Aurelijus Valeiša</a> via Flickr</dd>
</dl>
</div>
</div>
<p>O hai. First things first — someone should slap me for being such a lazy blogger. Somehow I lost all the motivation I had in the beginning, but looks like it&#8217;s back now :) I finally had the time to play around with the latest <a class="zem_slink freebase/guid/9202a8c04000641f8000000000b66a0f" href="http://framework.zend.com/" title="Zend Framework" rel="homepage">Zend Framework</a> version (v 1.9 now). I managed to skip the whole 1.8.x version, so this whole Zend_Application stuff is quite new to me. I spent a few days poking around the manual and the code to make it work. And it works! Yey for me! And yey for <a href="http://twitter.com/akrabat">Rob Allen</a> for his post on <a href="http://akrabat.com/2009/07/08/bootstrapping-modules-in-zf-1-8/">Bootstrapping modules in ZF 1.8</a>!</p>
<p>Zend_Tool is an awesome tool. Creating a new project is like “zf create project project_name” :) And the new <a class="zem_slink" href="http://en.wikipedia.org/wiki/Bootstrapping" title="Bootstrapping" rel="wikipedia">bootstrapping</a> process with the Bootstrap class is somehow much clearer to me now&#8230; Anyways, lets skip to the code.</p>
<h2>The goal</h2>
<p>I wanted to set up routes in such way that when a user requests a page, all requests for non-existing controllers/modules are directed to a specific controller (not the error controller). In other words, if we have controllers IndexController, FooController and PageController, anything but http://example.com/index and http://example.com/foo is directed to the PageController. This can be useful for CMSs or blogs to make pretty links. Here&#8217;s where the <a href="http://twitter.com/jaspertandy/status/3205493310">Zend_Controller_Router_Route_Regex</a> stuff comes in:</p>
<pre class="php" name="code">$route = new Zend_Controller_Router_Route_Regex(
    '(?(?=^index$|^foo$)|([a-z0-9-_.]+))',
    array(
        'controller' =&gt; 'page',
        'action' =&gt; 'view',
        'slug' =&gt; null
    ),
    array(
        1 =&gt; 'slug',
    ),
    '%s'
    );

$router-&gt;addRoute('viewPage', $route);
</pre>
<p>Basically the regex does the following: if it&#8217;s index or foo don&#8217;t match anything, thus calling up those controllers, in any other case match what&#8217;s requested and pass it to the PageController&#8217;s viewAction as the slug parameter. The fourth parameter, the &#8216;%s&#8217;, is needed so that ZF can rebuild the route in components like the Zend_Navigation.</p>
<p>Now, when the PageController, viewAction get&#8217;s called up, we can check, for example, if a page with that slug exists (like, in a database). If it exists, show the content, otherwise call up a 404 page with the error controller. In this fancy and sexy way we can call up pages without passing ID&#8217;s or even letting the user know what part of the website is working on his request. He just request&#8217;s http://example.com/some_random_article and kaboom! he get&#8217;s the content :)</p>
<h2>Page navigation</h2>
<p>Oh the joy when I saw Zend_Navigation in the library! And it even includes view helpers to help us render links and menus and breadcrumbs! Yey! There are a <a href="http://blog.ekini.net/2009/05/25/zend-framework-making-the-built-in-breadcrumb-helper-work/">several</a> <a href="http://blog.ekini.net/2009/06/10/zend-framework-navigation-and-breadcrumbs-with-an-xml-file-in-zf-18/">blog posts</a> which go in details <a href="http://www.zendcasts.com/zend_navigation-dynamically-creating-a-menu-a-sitemap-and-breadcrumbs/2009/06/">about Zend_Navigation</a>, so I won&#8217;t be bothering with that. What I wanted to make with Zend_Navigation is to have a menu of all the pages rendered everywhere. Here&#8217;s where action helpers kick in. I made an action helper which makes up the structure of the links/pages. Something like this:</p>
<pre name="code" class="php">&lt;?php
class Zend_Controller_Action_Helper_LinkStructure extends
        Zend_Controller_Action_Helper_Abstract{
function direct(){
$structure = array(
    array(
         'label'=&gt;'Home page',
         'uri'=&gt;'/'
    ),
    array(
         'label'=&gt;'Articles',
         'uri'=&gt;'',
         'pages'=&gt;array(array(
                                  'label'=&gt;'Article 1',
                                  'uri'=&gt;'article_1'),
                              array(
                                  'label'=&gt;'Article 2',
                                  'uri'=&gt;'article_2'),
                         )
    )
);
return new Zend_Navigation($structure);
}
}
</pre>
<p>This is a simple example of the structure; I&#8217;m actually making it out from the database, with all the categories, subcategories and pages.</p>
<h2>Links everywhere</h2>
<p>To have this menu on all pages, we need to render it in the layout.phtml. Rendering is quite simple:</p>
<pre name="code" class="php">// somewhere in layout.phtml
&lt;?php echo $this-&gt;navigation()-&gt;menu(); ?&gt;
</pre>
<p>Of course, we need to pass the menu to the navigation helper somehow. To avoid doing <code>$this-&gt;navigation($this-&gt;_helper-&gt;linkStructure());</code> in all the controllers, we could do that once in the bootstrap (any other ways to make it happen?):</p>
<pre name="code" class="php">// in Bootstrap.php somewhere in the Bootstrap class
function _initView(){

        $view = new Zend_View();
        $view-&gt;doctype('XHTML1_STRICT');
        $view-&gt;headMeta()-&gt;appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8');

        // our helper is in app/controllers/helpers folder, but ZF doesn't know that, so tell him
        Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH.'/controllers/helpers');
        // now get the helper
        $linkStructure = Zend_Controller_Action_HelperBroker::getStaticHelper('LinkStructure');
        // and assign it to the navigation helper
        $view-&gt;navigation($linkStructure-&gt;direct());

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer-&gt;setView($view);

        return $view;
}
</pre>
<p>There. Now we have our menu rendered on all pages. Sexy isn&#8217;t it? :)</p>
<p>That&#8217;s it for now. Hope someone will find this useful :) Now I gotta go, need to get ready for a punk rock concert tonight!</p>
<p>Happy hacking!</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/900cdd04-8f9e-4bc3-9de1-03e0bd23457c/" title="Reblog this post [with Zemanta]"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=900cdd04-8f9e-4bc3-9de1-03e0bd23457c" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related more-info pretty-attribution paragraph-reblog"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/playing-with-zend_navigation-and-routes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Online resources for Zend Framework</title>
		<link>http://robertbasic.com/blog/online-resources-for-zend-framework/</link>
		<comments>http://robertbasic.com/blog/online-resources-for-zend-framework/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 12:21:26 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Places on the web]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[site]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=603</guid>
		<description><![CDATA[Besides the official documentation and the Quickstart, there are many useful resources for Zend Framework, like blogs and Twitter. I did my best to collect them. If you know something that&#8217;s not listed here, but should be, please leave a comment and I&#8217;ll update the post :) Update #1 (seconds after publishing): Gotta love Twitter. [...]]]></description>
			<content:encoded><![CDATA[<p>Besides the <a href="http://framework.zend.com/manual/en/">official documentation</a> and <a href="http://framework.zend.com/docs/quickstart">the Quickstart</a>, there are many useful resources for <a href="http://framework.zend.com/">Zend Framework</a>, like blogs and Twitter. I did my best to collect them. If you know something that&#8217;s not listed here, but should be, please leave a comment and I&#8217;ll update the post :)</p>
<p><strong>Update #1 (seconds after publishing): Gotta love Twitter. Already got a message that I missed a blog. List is updated.</strong></p>
<p><strong>Update #2: Added more blogs to the list, thanks Jani for the recommendations!</strong></p>
<p><strong>Update #3: Thanks to Federico and Pablo, even more stuff to add :)</strong></p>
<p><strong>Update #4: Thank you Jon and Cal :)</strong></p>
<p><strong>Update #5: This is growing up into a pretty big list :) new stuff added!</strong></p>
<p><strong>Update #6: Should I keep adding these Update #x lines? :)</strong></p>
<p><strong>Update #7: A bunch of new stuff!</strong></p>
<p><strong>Update #8: A new ZF application <a href="http://phpimpact.wordpress.com/">via Federico&#8217;s blog!</a></strong></p>
<h2>Blogs</h2>
<p>Blogs are probably the most important resources out there. Besides the posts, comments can add a great value to the topic, so be sure to read them too. Here are the blogs that have posts on ZF and were updated recently (in the past month or two):</p>
<ul>
<li>Matthew Weier O&#8217;Phinney &#8212; <a href="http://weierophinney.net/matthew/">Phly, boy, phly</a></li>
<li>Pádraic Brady &#8212; <a href="http://blog.astrumfutura.com/">Maugrim The Reaper&#8217;s Blog</a></li>
<li>Rob Allen &#8212; <a href="http://akrabat.com/">Akra&#8217;s Dev Notes</a></li>
<li>Jani Hartikainen &#8212; <a href="http://codeutopia.net/blog/">CodeUtopia</a></li>
<li>Michelangelo van Dam &#8212; <a href="http://www.dragonbe.com/">DragonBe&#8217;s PHP blog</a></li>
<li>A.J. Brown &#8212; <a href="http://ajbrown.org/blog/">A.J. Brown&#8217;s Blog</a></li>
<li>Federico Cargnelutti &#8212; <a href="http://phpimpact.wordpress.com/">PHP::Impact ([str Blog])</a></li>
<li>Matthew Turland &#8212; <a href="http://matthewturland.com/">Matthew Turland</a></li>
<li>Juozas Kaziukėnas &#8212; <a href="http://dev.juokaz.com/">Juozas devBlog</a></li>
<li>Bradley Holt &#8212; <a href="http://bradley-holt.blogspot.com/">Bradley Holt&#8217;s Blog</a></li>
<li>Jon Lebensold &#8212; <a href="http://www.zendcasts.com/">ZendCasts</a></li>
<li>Tom Graham &#8212; <a href="http://www.noginn.com/">Tom Graham&#8217;s Blog</a></li>
<li>Benjamin Eberlei &#8212; <a href="http://www.whitewashing.de/">Benjamin Eberlei&#8217;s Blog</a></li>
<li>Thomas Weidner &#8212; <a href="http://www.thomasweidner.com/flatpress/index.php">Blacksheeps paradise</a></li>
<li>Mike Rötgers &#8212; <a href="http://www.roetgers.org/">Mike Rötgers&#8217; Blog</a></li>
<li>Raphael Stolt &#8212; <a href="http://raphaelstolt.blogspot.com/">Raphael on PHP</a></li>
<li>Armando Padilla &#8212; <a href="http://www.armando.ws/">Online Notes</a></li>
<li><a href="http://www.frontrangephp.org/presentations">FrontRangePHP users group</a></li>
<li><a href="http://blog.aditu.de/">Tobis blog</a> &#8212; in German</li>
<li><a href="http://www.ralfeggert.de/">Ralfs Zend Framework und PHP Blog</a> &#8212; in German</li>
<li><a href="http://zfblog.de/">ZFBlog.de</a> &#8212; in German</li>
<li>Alexander Romanenko &#8212; <a href="http://alex-tech-adventures.com/">Alex Tech Adventures</a></li>
</ul>
<p>Also, I recommend subscribing to <a href="http://phpdeveloper.org/">PHPDeveloper&#8217;s</a> and <a href="http://devzone.zend.com/">Zend Developer Zone&#8217;s</a> feeds, just in case I missed some good blogs ;)</p>
<h2>Twitter</h2>
<p>On <a href="http://twitter.com/">Twitter</a> there are many friendly developers willing to help out with any problems related to Zend Framework &#151 just write your question with a ZF hashtag and someone will most likely show up with the answer :)</p>
<ul>
<li><a href="http://twitter.com/wllm">Wil Sinclair</a></li>
<li><a href="http://twitter.com/weierophinney">Matthew Weier O&#8217;Phinney</a></li>
<li><a href="http://twitter.com/akrabat">Rob Allen</a></li>
<li><a href="http://twitter.com/padraicb">Pádraic Brady</a></li>
<li><a href="http://twitter.com/jhartikainen">Jani Hartikainen</a></li>
<li><a href="http://twitter.com/elazar">Matthew Turland</a></li>
<li><a href="http://twitter.com/BradleyHolt">Bradley Holt</a></li>
<li><a href="http://twitter.com/DragonBe">Michelangelo van Dam</a></li>
<li><a href="http://twitter.com/arjo">arjo</a></li>
<li><a href="http://twitter.com/barryroodt">Barry Roodt</a></li>
<li><a href="http://twitter.com/momcilovic">Milan Momčilović</a></li>
<li><a href="http://twitter.com/arkon108">Saša Tomislav Mataić</a></li>
<li><a href="http://twitter.com/mfacenet">Shawn Stratton</a></li>
<li><a href="http://twitter.com/webholics">Mario Volke</a></li>
<li><a href="http://twitter.com/bdeshong">Brian DeShong</a></li>
<li><a href="http://twitter.com/caseyw">Casey Wilson</a></li>
<li><a href="http://twitter.com/ralphschindler">Ralph Schindler</a></li>
<li><a href="http://twitter.com/tholder">Tom Holder</a></li>
<li><a href="http://twitter.com/walberty">Willie Alberty</a></li>
<li><a href="http://twitter.com/dshafik">Davey Shafik</a></li>
<li><a href="http://twitter.com/jacobkiers">Jacob Kiers</a></li>
<li><a href="http://twitter.com/CalEvans">Cal Evans</a></li>
<li><a href="http://twitter.com/fedecarg">Federico Cargnelutti</a></li>
<li><a href="http://twitter.com/zendcasts">ZendCasts</a></li>
<li><a href="http://twitter.com/RobertCastley">Robert Castley</a></li>
<li><a href="http://twitter.com/wjgilmore">Jason Gilmore</a></li>
<li><a href="http://twitter.com/AlexanderRV">Alexander Romanenko</a></li>
</ul>
<h2>Books</h2>
<p>These <del datetime="2009-03-10T01:04:32+00:00">two</del> books are a must read. That is all :)</p>
<p><a href="http://www.survivethedeepend.com/">Surviving The Deep End</a> &#8212; a free online book that is written chapter by chapter. Author is <a href="http://blog.astrumfutura.com/">Pádraic Brady</a>:</p>
<blockquote><p>The book was written to guide readers through the metaphorical &#8220;Deep End&#8221;. It&#8217;s the place you find yourself in when you complete a few tutorials and scan through the Reference Guide, where you are buried in knowledge up to your neck but without a clue about how to bind it all together effectively into an application. This take on the Zend Framework offers a survival guide, boosting your understanding of the framework and how it all fits together by following the development of a single application from start to finish. I&#8217;ll even throw in a few bad jokes for free.</p></blockquote>
<p><a href="http://www.zendframeworkinaction.com/">Zend Framework in Action</a> &#8212; OK, this book is not an online resource, but it is great and surely must be mentioned :) Authors are <a href="http://akrabat.com/">Rob Allen</a>, <a href="http://www.ingredients.com.au/nick/">Nick Lo</a> and Steven Brown:</p>
<blockquote><p>Zend Framework in Action is a book that covers all you need to know to get started with the Zend Framework.<br />
The first part of the book works through the creation of web site using the MVC components (Zend_Controller, Zend_View and Zend_Db). The book then follows on by looking at user authentication and access control, forms, searching and email to round out the application. After considering deployment issues, we then look at other components that add value to a web site; including web services, PDF creation, internationalisation and caching.</p></blockquote>
<p><a href="http://www.phparch.com/c/books/id/9780973862157">Guide to Programming with Zend Framework</a> &#8212; another great book, a must have. Written by <a href="http://blog.calevans.com/">Cal Evans</a>.</p>
<blockquote><p>This book covers much of the primary functionality offered by the Zend Framework, and works well both as a thorough introduction to its use and as a reference for higher-level tasks</p></blockquote>
<p><a href="http://www.amazon.com/Beginning-Zend-Framework-Armando-Padilla/dp/1430218258">Beginning Zend Framework</a> &#8212; written by <a href="http://www.armando.ws/">Armando Padilla</a></p>
<blockquote><p>Beginning Zend Framework is a beginner’s guide to learning and using the Zend Framework. It covers everything from the installation to the various features of the framework to get the reader up and running quickly.</p></blockquote>
<p><a href="http://www.easyphpwebsites.com/">Easy PHP Websites with Zend Framework</a> by Jason Gilmore</p>
<blockquote><p>Easy PHP Websites with the Zend Framework is the ultimate guide to building powerful PHP websites. Combining over 330 pages of instruction with almost 5 hours of online video and all of the example code, you&#8217;ll have everything you need to learn PHP faster and more effectively than you ever imagined.</p></blockquote>
<h2>Applications powered by ZF</h2>
<p>Wanna see what&#8217;s ZF capable of?</p>
<ul>
<li>A web-based Project Management &#038; Issue/Bug Tracking solution &#8212; <a href="http://jotbug.org/">JotBug</a></li>
<li>A content management system &#8212; <a href="http://digitaluscms.com/">Digitalus CMS</a></li>
<li>A project management system &#8212; <a href="http://www.phprojekt.com/index.php?&#038;newlang=eng">PHPProjekt 6</a></li>
<li>eCommerce platform &#8212; <a href="http://www.magentocommerce.com/">Magento</a></li>
<li>PHP Lifestream Aggregator, by <a href="http://markupartist.com/">Johan Nilssons</a> &#8212; <a href="http://johannilsson.me/streams/list">a phplifestream example</a> and it&#8217;s <a href="http://github.com/johannilsson/phplifestream/tree/master">source</a></li>
<li>A free and open source collections based web-based publishing platform &#8212; <a href="http://omeka.org/">Omeka</a></li>
</ul>
<h2>Other resources</h2>
<p>Of course, there&#8217;s the good ol&#8217; IRC, channels are <strong>#zftalk</strong> and <strong>#zftalk.dev</strong>. For more information, visit <a href="http://www.zftalk.com/">ZFTalk</a>.</p>
<p><a href="http://codeutopia.net/blog/">Jani Hartikainen&#8217;s</a> <a href="http://epic.codeutopia.net/pack/">Packageizer</a> is a great tool to get only those ZF components you need.</p>
<p><a href="http://jokke.dk/blog/2009/01/introducing_the_scienta_zf_debug_bar">Scienta ZF Debug Bar</a> an awesome plugin for Zend Framework which &#8220;injects into every request a snippet of HTML with commonly used debug information.&#8221;</p>
<p>There&#8217;s also the <a href="http://www.zfforums.com/">Zend Framework Forum</a>. For those of you who understand it, here&#8217;s a German forum <a href="http://www.zfforum.de/">www.zfforum.de</a>.</p>
<p>The <a href="http://framework.zend.com/wiki/">Zend Framework Wiki</a> and the <a href="http://framework.zend.com/issues/">Zend Framework Issue Tracker</a> are also very helpful, so, be sure to check them out.</p>
<p>The unofficial PEAR channel for the Zend Framework can be found at <a href="http://zend.googlecode.com/">http://zend.googlecode.com/</a>.</p>
<p>That&#8217;s all from me. This are the resources I found useful and hopefully are and will be useful for you too :)</p>
<p>Do you know anything I missed? If so, please, leave a comment and I&#8217;ll update the post :)</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/online-resources-for-zend-framework/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Login example with Zend_Auth</title>
		<link>http://robertbasic.com/blog/login-example-with-zend_auth/</link>
		<comments>http://robertbasic.com/blog/login-example-with-zend_auth/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 20:25:38 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[authenticate]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=505</guid>
		<description><![CDATA[Happy New Year! Hope everyone had a blast for New Year&#8217;s Eve and managed to get some rest :) This is my first working day for this year. I&#8217;m still kinda lazy and sleepy. And I wanna eat something all the time. Damn you candies!!! So, here&#8217;s what I&#8217;m going to do: authenticate an user [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Happy New Year!</strong> Hope everyone had a blast for New Year&#8217;s Eve and managed to get some rest :) This is my first working day for this year. I&#8217;m still kinda lazy and sleepy. And I wanna eat something all the time. Damn you candies!!!</p>
<p>So, here&#8217;s what I&#8217;m going to do: authenticate an user against a database table using Zend Framework&#8217;s Zend_Auth component. It&#8217;s really a piece of cake. You can see a working example here: <a href="http://robertbasic.com/dev/login/">http://robertbasic.com/dev/login/</a>. Feel free to test it and report any misbehavior down in the <a href="http://robertbasic.com/blog/login-example-with-zend_authlogin-example-with-zend_auth/#comments">comments</a>. In the codes below all paths, class names, actions, etc. will be as are in the example, so you probably will need to changed those according to your setup.</p>
<h2>Preparation</h2>
<p>Because I&#8217;m gonna use a database, be sure to have set the default database adapter in the bootstrap file, I have it setup like this:</p>
<pre name="code" class="php">
$config = new Zend_Config_Ini('../application/dev/config/db_config.ini', 'offline');
$registry = Zend_Registry::getInstance();
$registry-&gt;set('db_config',$config);
$db_config = Zend_Registry::get('db_config');
$db = Zend_Db::factory($db_config-&gt;db);
Zend_Db_Table::setDefaultAdapter($db);
</pre>
<p>I&#8217;ll need it later in the code. The table structure is as follows:</p>
<pre name="code" class="sql">
--
-- Table structure for table `zendLogin`
--

CREATE TABLE `zendLogin` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
</pre>
<p><span id="more-505"></span></p>
<h2>The login controller</h2>
<p>The magic happens in the <code>LoginController</code>. It has two actions: <code>indexAction</code> and <code>logoutAction</code>. The <code>indexAction</code> will take care of showing the login form and processing the login process. The <code>logoutAction</code> will just logout the user. You would never figure out that one on your own, right?</p>
<p>Now, let&#8217;s get to the fun part &#8212; the code:</p>
<pre name="code" class="php">
&lt;?php
class Dev_LoginController extends Zend_Controller_Action
{
    public function indexAction()
    {
        // If we're already logged in, just redirect
        if(Zend_Auth::getInstance()-&gt;hasIdentity())
        {
            $this-&gt;_redirect('dev/secured/index');
        }

        $request = $this-&gt;getRequest();
        $loginForm = $this-&gt;getLoginForm();

        $errorMessage = "";
</pre>
<p>Not much happening here: if the user is already logged in, I don&#8217;t want him at the login form, so just redirect him somewhere else; most likely to a home page or a control panel&#8217;s index page.</p>
<p>The <code>Zend_Auth</code> implements the Singleton pattern &#8212; if you&#8217;re not familiar with it read <a href="http://framework.zend.com/manual/en/zend.auth.html#zend.auth.introduction">http://framework.zend.com/manual/en/zend.auth.html#zend.auth.introduction</a> and <a href="http://www.php.net/manual/en/language.oop5.patterns.php">http://www.php.net/manual/en/language.oop5.patterns.php</a> (at php.net scroll down to the example #2).</p>
<p>So, I&#8217;m just asking the <code>Zend_Auth</code> does it have an user identity stored in it; the identity gets stored only upon successful log in. I&#8217;m also getting the request object. The <code>getLoginForm()</code> is a function that I wrote for assembling the login form and is a part of the <code>LoginController</code>, I&#8217;ll show it&#8217;s code later.</p>
<pre name="code" class="php">
if($request-&gt;isPost())
{
    if($loginForm-&gt;isValid($request-&gt;getPost()))
    {
        // get the username and password from the form
        $username = $loginForm-&gt;getValue('username');
        $password = $loginForm-&gt;getValue('password');
</pre>
<p>This doesn&#8217;t needs a lot of explanation: if it&#8217;s a post request, it means the form is submitted. If the submitted data is valid, just get the wanted values from the form.</p>
<pre name="code" class="php">
        $dbAdapter = Zend_Db_Table::getDefaultAdapter();
        $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);

        $authAdapter-&gt;setTableName('zendLogin')
                    -&gt;setIdentityColumn('username')
                    -&gt;setCredentialColumn('password')
                    -&gt;setCredentialTreatment('MD5(?)');
</pre>
<p>Here I&#8217;m getting the default database adapter, so I know whit which database I&#8217;m working with. Then I&#8217;m creating an adapter for Zend_Auth, which is used for authentication; the docs give good explanation on the adapter, read it here: <a href="http://framework.zend.com/manual/en/zend.auth.html#zend.auth.introduction.adapters">http://framework.zend.com/manual/en/zend.auth.html#zend.auth.introduction.adapters</a>.</p>
<p>Next, I&#8217;m telling the authentication adapter which table to use from the database, and which columns from that table. Also, I&#8217;m telling it how to treat the credentials &#8212; the passwords are stored as MD5 hashes, so the submitted passwords will first be MD5ed and then checked.</p>
<pre name="code" class="php">
        // pass to the adapter the submitted username and password
        $authAdapter-&gt;setIdentity($username)
                    -&gt;setCredential($password);

        $auth = Zend_Auth::getInstance();
        $result = $auth-&gt;authenticate($authAdapter);
</pre>
<p>I&#8217;m passing to the adapter the user submitted username and password, and then trying to authenticate with that username and password.</p>
<pre name="code" class="php">
        // is the user a valid one?
        if($result-&gt;isValid())
        {
            // get all info about this user from the login table
            // ommit only the password, we don't need that
            $userInfo = $authAdapter-&gt;getResultRowObject(null, 'password');

            // the default storage is a session with namespace Zend_Auth
            $authStorage = $auth-&gt;getStorage();
            $authStorage-&gt;write($userInfo);

            $this-&gt;_redirect('dev/secured/index');
        }
</pre>
<p>If the user is successfully authenticated, get all information about him from the table (if any), like the real name, E-mail, etc. I&#8217;m leaving out the password, I don&#8217;t need that. Next I&#8217;m getting the <code>Zend_Auth</code>&#8216;s <a href="http://framework.zend.com/manual/en/zend.auth.html#zend.auth.introduction.persistence.default">default storage</a> and storing in it the user information. In the end I&#8217;m redirecting it where I want it.</p>
<pre name="code" class="php">
else
{
    $errorMessage = "Wrong username or password provided. Please try again.";
}
}
}
$this-&gt;view-&gt;errorMessage = $errorMessage;
$this-&gt;view-&gt;loginForm = $loginForm;
}
</pre>
<p>And this is the end of the <code>indexAction</code>. I know I could take the correct message from <code>$result</code> with <code>getMessages()</code>, but I like more this kind of message, where I&#8217;m not telling the user which part did he got wrong.</p>
<pre name="code" class="php">
public function logoutAction()
{
    // clear everything - session is cleared also!
    Zend_Auth::getInstance()-&gt;clearIdentity();
    $this-&gt;_redirect('dev/login/index');
}
</pre>
<p>This is the <code>logoutAction</code>. I&#8217;m clearing the identity from <code>Zend_Auth</code>, which is also clearing all data from the <code>Zend_Auth</code> session namespace. And, of course, redirecting back to the login form.</p>
<pre name="code" class="php">
protected function getLoginForm()
{
    $username = new Zend_Form_Element_Text('username');
    $username-&gt;setLabel('Username:')
            -&gt;setRequired(true);

    $password = new Zend_Form_Element_Password('password');
    $password-&gt;setLabel('Password:')
            -&gt;setRequired(true);

    $submit = new Zend_Form_Element_Submit('login');
    $submit-&gt;setLabel('Login');

    $loginForm = new Zend_Form();
    $loginForm-&gt;setAction('/dev/login/index/')
            -&gt;setMethod('post')
            -&gt;addElement($username)
            -&gt;addElement($password)
            -&gt;addElement($submit);

    return $loginForm;
}
</pre>
<p>As promised, here&#8217;s the code for <code>getLoginForm</code> function. That&#8217;s the whole <code>LoginController</code> code, not really a rocket science :) Sorry if it&#8217;s a bit hard to keep up with the code, I needed it to break it up in smaller pieces&#8230;</p>
<p>And here&#8217;s the view script for the <code>indexAction</code>.</p>
<pre name="code" class="php">
&lt;h2&gt;Zend_Login example&lt;/h2&gt;

&lt;p&gt;
Hello! This is an example of authenticating users with the Zend Framework...
&lt;/p&gt;

&lt;p&gt;Please login to proceed.&lt;/p&gt;

&lt;?php if($this-&gt;errorMessage != ""): ?&gt;
&lt;p class="error"&gt;&lt;?= $this-&gt;errorMessage; ?&gt;&lt;/p&gt;
&lt;?php endif; ?&gt;

&lt;?= $this-&gt;loginForm; ?&gt;
</pre>
<h2>Other controllers</h2>
<p>Couldn&#8217;t come up with a better subtitle :(</p>
<p>Here&#8217;s an example how to require the user to log in to see the page: in the <code>init()</code> method ask <code>Zend_Auth</code> is the user logged in, and if not redirect him to the login form. This way the user will have to log in to the &#8220;whole controller&#8221;. Implement the same only to the <code>indexAction</code>, and the user will have to only log in to see the index page; he&#8217;ll be able to access another page without logging in. </p>
<pre name="code" class="php">
class Dev_SecuredController extends Zend_Controller_Action
{
    function init()
    {
        // if not logged in, redirect to login form
        if(!Zend_Auth::getInstance()-&gt;hasIdentity())
        {
            $this-&gt;_redirect('dev/login/index');
        }
    }

    public function indexAction()
    {
        // get the user info from the storage (session)
        $userInfo = Zend_Auth::getInstance()-&gt;getStorage()-&gt;read();

        $this-&gt;view-&gt;username = $userInfo-&gt;username;
        $this-&gt;view-&gt;name = $userInfo-&gt;name;
        $this-&gt;view-&gt;email = $userInfo-&gt;email;
    }

    public function anotherAction()
    {
    }
}
</pre>
<p>I&#8217;m also reading out the user information from the <code>Zend_Auth</code>&#8216;s storage, that I have stored there during the log in process.</p>
<p>So there. A fully working login system, which can be setup in a really short time.</p>
<p><strong>Update: If you want, you can get an example source code from here: <a href="http://robertbasic.com/downloads/zendLogin.zip">zendLogin.zip</a> ~8kB</strong></p>
<p>Happy hacking!</p>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/login-example-with-zend_auth/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Styling the default Zend_Form layout</title>
		<link>http://robertbasic.com/blog/styling-the-default-zend_form-layout/</link>
		<comments>http://robertbasic.com/blog/styling-the-default-zend_form-layout/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 22:17:21 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[styling]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://robertbasic.com/blog/?p=458</guid>
		<description><![CDATA[Here&#8217;s an example for styling Zend_Form&#8216;s default layout. The default layout is using definition lists. While there&#8217;s an option for changing the default layout, the wrapper tags and stuff, I see no reason for it. Create the form, add some CSS and your good to go :) Note: Be sure to provide a Document Type [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s an example for styling <a href="http://framework.zend.com/manual/en/zend.form.html">Zend_Form</a>&#8216;s default layout. The default layout is using <a href="http://w3schools.com/tags/tag_dl.asp">definition lists</a>. While there&#8217;s an option for changing the default layout, the wrapper tags and stuff, I see no reason for it. Create the form, add some CSS and your good to go :)</p>
<p>Note: Be sure to provide a Document Type in your view scripts like this:</p>
<pre name="code" class="php">
&lt;?= $this-&gt;doctype('XHTML1_STRICT') ?&gt;
</pre>
<p>because when the form is generated, ZF is looking at the doctype to see how to create the form elements. Forgetting the doctype will probably generate invalid markup. I learned the hard way. Don&#8217;t do the same mistake, k? :)</p>
<h2>The generated markup</h2>
<p>So, here&#8217;s what Zend_Form makes for us (this markup is after submitting the form, but whit generated error, to show the error markup, too):</p>
<pre name="code" class="php">
&lt;form enctype="application/x-www-form-urlencoded" method="post" action=""&gt;
&lt;dl class="zend_form"&gt;
    &lt;dt&gt;
        &lt;label for="input1" class="required"&gt;Input field #1:&lt;/label&gt;
    &lt;/dt&gt;
    &lt;dd&gt;
        &lt;input type="text" name="input1" id="input1" value="" /&gt;
        &lt;ul class="errors"&gt;
            &lt;li&gt;Value is empty, but a non-empty value is required&lt;/li&gt;
        &lt;/ul&gt;
        &lt;p class="description"&gt;Description? Yes, please.&lt;/p&gt;
    &lt;/dd&gt;
    &lt;dt&gt;
        &nbsp;
    &lt;/dt&gt;
    &lt;dd&gt;
        &lt;input type="submit" name="submit" id="submit" value="Submit form" /&gt;
    &lt;/dd&gt;
&lt;/dl&gt;
&lt;/form&gt;
</pre>
<p>The PHP code which generates this form (without the error, of course) goes like this:</p>
<pre name="code" class="php">
$input1 = new Zend_Form_Element_Text('input1');
$input1-&gt;setLabel('Input field #1:')
          -&gt;setDescription('Description? Yes, please.')
          -&gt;setRequired(true);

$submit = new Zend_Form_Element_Submit('submit');
$submit-&gt;setLabel('Submit form')

$form = new Zend_Form();
$form-&gt;setMethod('post')
       -&gt;addElement($input1)
       -&gt;addElement($submit);
</pre>
<div id="attachment_466" class="wp-caption alignright" style="width: 160px"><a href="http://robertbasic.com/blog/wp-content/uploads/2008/12/zendformnocss.gif"><img src="http://robertbasic.com/blog/wp-content/uploads/2008/12/zendformnocss-150x150.gif" alt="Default Zend_Form layout with no CSS" title="zendformnocss" width="150" height="150" class="size-thumbnail wp-image-466" /></a><p class="wp-caption-text">Default Zend_Form layout with no CSS</p></div>
<p>Now, the generated form looks kinda good with no styling (which is good, if some maniac comes to visit with CSS support disabled).</p>
<p>OK, I lie: there&#8217;s a minimum of CSS for setting the background to white and the width to 460 pixels.</p>
<p>As you can see I&#8217;ve shortened the HTML and the PHP in the example codes&#8230;</p>
<h2>The styling</h2>
<p>I like my forms a bit different: form elements and their labels side by side with element descriptions and eventual errors showing up under the element. Here&#8217;s the CSS to achieve this:</p>
<pre name="code" class="css">
.zend_form{
background:#fff;
width:460px;
margin:5px auto;
padding:0;
overflow:auto;
}

.zend_form dt{
padding:0;
clear:both;
width:30%;
float:left;
text-align:right;
margin:5px 5px 5px 0;
}

.zend_form dd{
padding:0;
float:left;
width:68%;
margin:5px 2px 5px 0;
}

.zend_form p{
padding:0;
margin:0;
}

.zend_form input, .zend_form textarea{
margin:0 0 2px 0;
padding:0;
}

.submit{
float:right;
}

.required:before{content:'* '}

.optional:before{content:'+ '}
</pre>
<div id="attachment_484" class="wp-caption alignright" style="width: 160px"><a href="http://robertbasic.com/blog/wp-content/uploads/2008/12/zendform1.gif"><img src="http://robertbasic.com/blog/wp-content/uploads/2008/12/zendform1-150x150.gif" alt="Default Zend_Form layout with CSS" title="zendform1" width="150" height="150" class="size-thumbnail wp-image-484" /></a><p class="wp-caption-text">Default Zend_Form layout with CSS</p></div>
<p>Of course, this CSS takes care only of the layout; things like font types and sizes, colors, borders, backgrounds, etc. are not essential for this.</p>
<p>So, with this CSS applied to the generated Zend_Form, you can see on the image what will come up. And you know what&#8217;s the best part? It&#8217;s good for Firefox, Internet Explorer 6, Chrome and Opera, both under Windows and GNU/Linux (sorry, not tested for Internet Explorer 7 and Safari, but they should play along as well).</p>
<p>I almost forgot: I added a class=&#8221;submit&#8221; to the submit button, to be able to float it right. I first tried to do that with input[type=submit], but IE doesn&#8217;t know that, and as I wanted to make a styling that looks (almost) the same in all browsers with no hacks, I decided to add the class attribute.</p>
<p>So there, this little CSS code snippet should get you started with styling your Zend Form&#8217;s.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://robertbasic.com/blog/styling-the-default-zend_form-layout/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

