<?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>TBNL &#187; zend framework</title>
	<atom:link href="http://www.tibobeijen.nl/blog/tag/zend-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tibobeijen.nl</link>
	<description>...another view on the web and how it's built</description>
	<lastBuildDate>Tue, 15 Jun 2010 17:17:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Zend_Form without Zend Framework MVC</title>
		<link>http://www.tibobeijen.nl/blog/2009/12/07/using-zend_form-without-zend-framework-mvc/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/12/07/using-zend_form-without-zend-framework-mvc/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 10:13:13 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[articles]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zend_form]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=568</guid>
		<description><![CDATA[Most components of Zend Framework can be used without using the entire framework and Zend_Form is no exception. It&#8217;s a versatile component that can be customized to great extent. The payoff is that seemingly easy tasks can seem quite complex to complete and involve concepts like Decorators and View Helpers. Complexity is increased by the [...]]]></description>
			<content:encoded><![CDATA[<p>Most components of <a href="http://framework.zend.com/">Zend Framework</a> can be used without using the entire framework and <a href="http://framework.zend.com/manual/en/zend.form.html">Zend_Form</a> is no exception. It&#8217;s a versatile component that can be customized to great extent. The payoff is that seemingly easy tasks can seem quite complex to complete and involve concepts like Decorators and View Helpers. Complexity is increased by the fact that most tasks can be achieved in multiple ways.</p>
<p>Forms in general are elements where a lot of parts of an application &#8216;meet&#8217;: Frontend code (HTML/CSS), behavior (JS) and backend processing (validation, filtering and storage). In this post I&#8217;ll show how one can:</p>
<ul>
<li>Use Zend_Form outside the Zend Framework MVC (most likely an existing project)</li>
<li>Separate Form rendering from it&#8217;s structure</li>
<li>Use custom validators and decorators</li>
</ul>
<p>The Form used in this tutorial can be <a href="/static/zend_form_without_mvc/">viewed here</a>. The code can be browsed or downloaded <a href="http://github.com/TBeijen/Zend_Form_Without_MVC">on GitHub</a>.</p>
<h3>Zend_Form components at a glance</h3>
<p>Let&#8217;s start with a quick look at how Zend_Form&#8217;s different components are tied together:</p>
<ul>
<li>A Zend_Form consists of elements, descendants of Zend_Form_Element</li>
<li>Elements can be supplied with Validators and Filters</li>
<li>Both the form and it&#8217;s elements depend on Decorators to generate HTML. Decorators form a &#8216;chain&#8217; where each decorator adds HTML to the result of the previous decorators.</li>
<li>The standard decorators provided by Zend_Form delegate the rendering of the actual HTML to <a href="http://framework.zend.com/manual/en/zend.view.helpers.html">View Helpers</a></li>
</ul>
<h3>Using Zend_Form without the MVC</h3>
<p>As the last point above shows, Zend_Form, through Decorators, makes extensive use of Zend_View helpers. In a Zend_Framework project Zend_Form is able to retrieve the View itself. In other projects (if one wants to use any of Zend_Form&#8217;s default decorators) a View instance has to be supplied to Zend_Form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_View<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doctype</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'XHTML1_TRANSITIONAL'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setView</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_View<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this example the doctype is specified in the view. This controls if input elements are rendered with XHTML self-closing tags.</p>
<h3>Separate structure and rendering</h3>
<p>In this tutorial I create a form for an admin interface where basic user-details can be edited. Let&#8217;s assume an ACL implementation dictates that some people can actually <em>edit </em>the details while others are only allowed to <em>view </em>them. I&#8217;ve decided to keep the form definition (the &#8216;model&#8217; side of a form) to a bare minimum and let different renderers (the &#8216;view&#8217; side) control the output. </p>
<p>For re-usability and keeping controller code clean one can best create form objects by creating an instance of a subclass of Zend_Form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Form_User <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// username</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'validators'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #666666; font-style: italic;">// arguments: type, breakchain, validator constructor options</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Alnum'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'StringLength'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// email</span>
        <span style="color: #000088;">$EmailValidate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_Validator_Email<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'validators'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'EmailAddress'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$EmailValidate</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>(As can be seen I create an instance of My_Validate_Email and add that to the email element. We&#8217;ll look into that later. First, let&#8217;s continue with the form definition.)</p>
<p>The init() method is designed for this type of form definition, thereby avoiding the need to duplicate the constructor and it&#8217;s parameter scheme. As can be seen, I only define the form elements and validators. Labels and such I defer to the renderer (more on that later). As this is about integrating Zend_Form into existing projects that are not based on Zend Framework, I assume there is some sort of language-aware component. In this example I use a language pack that returns language-specific results based on tags. Adding a set of checkboxes then looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Form_User <span style="color: #000000; font-weight: bold;">extends</span> Zend_Form
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
        <span style="color: #000088;">$lang</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_LanguagePack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$groupIds</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$groupOptions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$groupIds</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$groupOptions</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lang</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'group.label.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$elmGroup</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Form_Element_MultiCheckbox<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'group'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$elmGroup</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMultiOptions</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$groupOptions</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$elmGroup</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequired</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$elmGroup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now I create two renderer classes for both the &#8216;edit&#8217; and &#8216;view&#8217; display mode. Both accepting a Zend_Form instance in the constructor. Code below shows how the form is displayed using either rendering class:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_Form_User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$RendererEdit</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_Form_Renderer_Edit<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_edit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$RendererEdit</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$RendererView</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_Form_Renderer_View<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Form</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_view'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$RendererView</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But before moving onto the renderers, let&#8217;s look into the aforementioned My_Email_Validator class.</p>
<h3>Adding a custom validator</h3>
<p>When specifying validators by string, as is done with &#8216;EmailAddress&#8217;, Zend_Form adds one of the types of <a href="http://framework.zend.com/manual/en/zend.validate.html">Zend_Validate</a>. For validation not covered by the standard validators one can create a custom validator by extending Zend_Validate_Abstract or implementing Zend_Validator_Interface. For email addresses, a typical scenario would be a custom validator that connects to a database and checks if the given email address is allready used by another user. For this demo I skip the database part, resulting in:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Validator_Email <span style="color: #000000; font-weight: bold;">extends</span> Zend_Validate_Abstract
<span style="color: #009900;">&#123;</span>
    protected <span style="color: #000088;">$isValid</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isValid<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span> <span style="color: #339933;">=</span> <span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'duplicate@test.com'</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMessages<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'duplicateEmail'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'This email address is allready used'</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getErrors<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessages</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now let&#8217;s continue with the two renderer classes:</p>
<h3>Rendering the edit mode</h3>
<p>The basics of the My_Form_Renderer_Edit class constructor are shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Form_Renderer_Edit
<span style="color: #009900;">&#123;</span>
    protected <span style="color: #000088;">$form</span><span style="color: #339933;">;</span>
&nbsp;
    protected <span style="color: #000088;">$lang</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>Zend_Form <span style="color: #000088;">$form</span><span style="color: #339933;">,</span> <span style="color: #000088;">$form_id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_View<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doctype</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'XHTML1_TRANSITIONAL'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setView</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_View<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttrib</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'form_edit'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttrib</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$form_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lang</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_LanguagePack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As can be seen this is where I setup the view (as mentioned before) and set id and classname that can be used in CSS. I want the HTML to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;username-label&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;required&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>* Username<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;element&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;description&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>(Min 6, max 16 char.)<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>When the render() method is called on the renderer class I do four things:</p>
<ul>
<li>Add a submit element</li>
<li>Setup element properties that are common for all elements</li>
<li>Setup properties on a per-element basis</li>
<li>Setup properties of the form element</li>
</ul>
<p>I&#8217;ll briefly run through those steps. <a href="http://github.com/TBeijen/Zend_Form_Without_MVC/blob/master/htdocs/My_Form_Renderer_Edit.php">Complete code for My_Form_Renderer_Edit</a> can be seen on Github.</p>
<h4>Add a submit element</h4>
<p>As long as the submit value doesn&#8217;t play a role in the processing of entered data I consider this part of the view layer. For example, interaction design might dictate that on long forms submit buttons are placed both above and below the form.</p>
<h4>Setup common properties</h4>
<p>By using the method setElementDecorators() of Zend_Form, all default decorators are replaced by the ones specified. The decorators that fit my needs are ViewHelper (renders the element itself) and Description, resulting in:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    protected <span style="color: #000000; font-weight: bold;">function</span> setupElementsCommon<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setElementDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'ViewHelper'</span><span style="color: #339933;">,</span>
            <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Description'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'placement'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'append'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'span'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'description'</span>
            <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
         <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<h4>Setup per-element properties</h4>
<p>Here I add additional decorators. First I setup the element&#8217;s label and description properties by using the language pack, then I add the appropriate decorators. Furthermore I add some additional classnames to elements. If the element has an error I not only want to display the errors (using a custom decorator, more on that later). I also want to give the HTML element wrapped around all of the parts an &#8216;error&#8217; classname.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">        <span style="color: #000088;">$elmHasError</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$elm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessages</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$liClass</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$elmHasError</span> ? <span style="color: #0000ff;">'error'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$elm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span>
            <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'outerLi'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'HtmlTag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'li'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$liClass</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>Setup the form object</h4>
<p>Finally the Form is configured. After removing all existing decorators, three decorators are added. The only difference with the default set of decorators is the UL element used in the HtmlTag decorator:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    protected <span style="color: #000000; font-weight: bold;">function</span> setupForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearDecorators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FormElements'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HtmlTag'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ul'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Form'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Adding a custom decorator</h3>
<p>The complete code shows that instead of the default &#8216;Errors&#8217; decorator I provide my own. This way I can retrieve the error messages provided by the form element and then replace them with messages retrieved from the languagePack. A custom decorator needs to extend Zend_Form_Decorator_Abstract or implement Zend_Form_Decorator_Interface. Excerpts from <a href="http://github.com/TBeijen/Zend_Form_Without_MVC/blob/master/htdocs/My_Decorator_Errors.php">My_Decorator_Errors</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$errorMessages</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessages</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$errorMessages</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// iterate over errorMessages, replace or use default</span>
        <span style="color: #000088;">$errorLinesHtml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$errorMessages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$errorCode</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$errorMsg</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// make an exception for isEmpty and array representing elements</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$errorCode</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'isEmpty'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$msgLangPack</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lang</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'form.error.array.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$errorCode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$msgLangPack</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lang</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'form.error.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$errorCode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000088;">$errorMsg</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$msgLangPack</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$msgLangPack</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$errorMsg</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$errorLinesHtml</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;li&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$errorMsg</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// combine</span>
        <span style="color: #000088;">$errorHtml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;ul class=&quot;errors&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$errorLinesHtml</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/ul&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// ...</span></pre></div></div>

<p>Errormessages are fetched by an element&#8217;s getMessages() method that returns an associative array. The error-codes (keys) can be used to translate the message. As can be seen I treat elements representing an array differently when fetching the &#8216;isEmpty&#8217; error message.</p>
<div class="sidenote">
<p>Why using a custom errors decorator and not setting the messages on the element?</p>
<p>setErrors() can be used to set own errors on Zend_Form_Element. Those errors are stored in property _errorMessages which is indeed cleared first. This doesn&#8217;t remove the messages received from the validators though as they are stored in _messages. Upon adding an error by setErrors() both arrays are merged into _messages (this happens in markAsError()) and that&#8217;s the one returned by getMessages(). So trying to replace the existing isEmpty error by &#8217;setting&#8217; a custom error effectively adds an error instead of replacing it. getErrorMessages() does return just the new errors but&#8230; the viewHelper formErrors (which is invoked by the default &#8216;Errors&#8217; decorator) uses getMessages()&#8230;</p>
</div>
<h3>Rendering the view mode</h3>
<p>The view renderer is a simplified version of the edit renderer. Main differences are:</p>
<ul>
<li>The form doesn&#8217;t have a Form decorator and thereby no form tag. (I&#8217;ll keep calling the no-form a form though ;)).</li>
<li>All elements have only one decorator common for all elements: My_Decorator_View_Element</li>
<li>As the only element decorator doesn&#8217;t use Zend_View, the form has no need for a View element.</li>
</ul>
<h4>My_Decorator_View_Element</h4>
<p>This decorator retrieves value and label from the element and creates. It then creates HTML making exceptions for:</p>
<ul>
<li>Submit elements (which are ignored)</li>
<li>Array representing elements (where more than one value needs to be displayed)</li>
</ul>
<h3>Concluding</h3>
<p>Above examples give an impression of how Zend_Form can be suited to virtually anyone&#8217;s needs. One can argue that separating the rendering from the basic definition adds unnecessary complexity. It does however pull HTML specifics (which during a project life-cycle often are subject to change) out of the &#8216;model&#8217; layer. Both the stripped down forms and renderers are easy to reuse: The renderer can be applied to a number of forms and the forms themselves can be used in projects requiring different HTML.</p>
<p>Even without considering the rendering part, Zend_Form is a valuable platform offering a lot of ways to customize it. As the view renderer example shows it can also operate without Zend_View, provided one doesn&#8217;t use any of the built in decorators. Being able to provide self-built elements, validators and decorators makes it possible to use Zend_Form in virtually any project.</p>
<p>Further reading:</p>
<ul>
<li><a href="http://framework.zend.com/apidoc/core/">Zend Framework API documentation</a></li>
<li><a href="http://devzone.zend.com/article/3450">Decorators with Zend_Form</a></li>
<li><a href="http://weierophinney.net/matthew/archives/215-Rendering-Zend_Form-decorators-individually.html">Rendering Zend Form decorators individually</a></li>
<li><a href="http://giorgiosironi.blogspot.com/2009/10/advanced-zendform-usage.html">Advanced Zend_Form usage</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/12/07/using-zend_form-without-zend-framework-mvc/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHPBenelux meeting at Freshheads</title>
		<link>http://www.tibobeijen.nl/blog/2009/09/30/phpbenelux-meeting-at-freshheads/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/09/30/phpbenelux-meeting-at-freshheads/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 18:48:53 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[report]]></category>
		<category><![CDATA[freshheads]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpbenelux]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=515</guid>
		<description><![CDATA[Yesterday (sept. 29th) I went to the Freshheads office in Tilburg to attend the monthly PHPBenelux meeting. As it appeared it was right around the corner of the 013 venue so it was an easy find. 
Two talks were scheduled and Stefan Koopmanschap kicked off the meeting with a presentation titled &#8220;Integrating Symfony and Zend [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday (sept. 29th) I went to the <a href="http://www.freshheads.com/">Freshheads</a> office in Tilburg to attend the monthly <a href="http://www.phpbenelux.eu/">PHPBenelux</a> meeting. As it appeared it was right around the corner of the 013 venue so it was an easy find. </p>
<p>Two talks were scheduled and Stefan Koopmanschap kicked off the meeting with a presentation titled &#8220;Integrating Symfony and Zend Framework&#8221; (<a href="http://www.slideshare.net/skoop/integrating-symfony-and-zend-framework-2097969">slides</a>). After a short introduction pointing out the benefits of using any framework at all, Stefan showed how both Symfony&#8217;s and Zend Framework&#8217;s autoloaders can be initialized in the application&#8217;s bootstrap code. After that using ZF components in symfony was showcased by using Zend_Service_Twitter inside a Symfony project. Being quite familiar with Zend Framework that was not really surprising (to me) but the part following was: Integrating Symfony into a Zend Framework project. First of all it was new to me that there are &#8220;<a href="http://components.symfony-project.org/">Symfony Components</a>&#8220;. Apparently Symfony is not an all-or-nothing affair anymore. Most of the components were briefly covered of which I especially found the <a href="http://components.symfony-project.org/event-dispatcher/">Event Dispatcher</a> highly interesting.</p>
<p>After a brief pause, Juliette Reinders Folmer hosted the big &#8220;Why Equal Doesn&#8217;t Equal Quiz&#8221;. 65 Questings testing the knowledge of the attendees about the type conversions going on when writing code like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'empty'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'0'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'etc...'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'has value'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Questions like the ones above where the easy ones&#8230; Especially interesting where the <a href="http://nl.php.net/manual/en/book.ctype.php">ctype family of functions</a>. They are especially usefull for checking database result or GPC data. Although I had seen the ctype functions before I kind of forgot about them so especially there I had wrong answers. Score: 45 out of 65.</p>
<p>The evening concluded with some informal chatting and some beers generously provided by Freshheads. As usual I went home with some new things on my &#8216;got to check that out&#8217; list&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/09/30/phpbenelux-meeting-at-freshheads/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Zend_Test, Zend_Layout and the need to reset</title>
		<link>http://www.tibobeijen.nl/blog/2009/06/01/zend_test-zend_layout-and-the-need-to-reset/</link>
		<comments>http://www.tibobeijen.nl/blog/2009/06/01/zend_test-zend_layout-and-the-need-to-reset/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 11:55:30 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.tibobeijen.nl/?p=374</guid>
		<description><![CDATA[In a recent Zend Framework project I&#8217;ve used Zend_Test to test the functioning of the website &#8216;as a whole&#8217;. So besides testing the separate (authorization) components, the website was tested in the same way a visitor would use it. This is especially useful for testing login scenarios, so I added the test below:

public function testLogoutShouldDenyAccess&#40;&#41;
&#123;
 [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent Zend Framework project I&#8217;ve used Zend_Test to test the functioning of the website &#8216;as a whole&#8217;. So besides testing the separate (authorization) components, the website was tested in the same way a visitor would use it. This is especially useful for testing login scenarios, so I added the test below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLogoutShouldDenyAccess<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">login</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// verify that profile page now doesn't contain login form</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/profile'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertQueryCount</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'form#login'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// dispatch logout page</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/login/logout'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// verify that profile now holds login form</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/profile'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertQueryCount</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'form#login'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertNotController</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'profile'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This failed on the last assertQueryCount() which left me puzzled. Performing above steps manually seemed to work fine so I was overlooking something either in my app-code or the test-code.<br />
<span id="more-374"></span><br />
Adding a var_dump at the end of the test method gave some insight.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Showed a response object with a protected variable _body containing (simplified for clarity):</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;login&lt;/h1&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h1&gt;logout&lt;/h1&gt;
    &lt;/body&gt;
    &lt;/html&gt;
&lt;form id=&quot;login&quot; action=&quot;&quot; method=&quot;post&quot;&gt;
&lt;!-- form contents --&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Apparantly the total content of a previous request is prepended to the view part of the next request, resulting in incorrect html the assertQuery() method can&#8217;t handle. Looking into the login() helper method I made earlier:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//login</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span>
         <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPost</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
              <span style="color: #0000ff;">'un'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'testun'</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">'pw'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'testpw'</span>
         <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/login/login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertRedirectTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// reset</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resetRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
         <span style="color: #339933;">-&gt;</span><span style="color: #004000;">resetResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// reset post</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPost</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Apparently I forgot to reset the response object as adding that to the test method solved the issue. So for testing multiple requests in one test method one needs to reset the response to prevent incorrect html. This can be simplified by using below custom dispatch method that also allows setting POST data:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doDispatch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$post</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// reset everything</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resetRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
         <span style="color: #339933;">-&gt;</span><span style="color: #004000;">resetResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPost</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// add post-data if given</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPost</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'un'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'testun'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'pw'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'testpw'</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// dispatch</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2009/06/01/zend_test-zend_layout-and-the-need-to-reset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend_Config strategies</title>
		<link>http://www.tibobeijen.nl/blog/2008/11/30/zend_config-strategies/</link>
		<comments>http://www.tibobeijen.nl/blog/2008/11/30/zend_config-strategies/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 18:24:30 +0000</pubDate>
		<dc:creator>Tibo Beijen</dc:creator>
				<category><![CDATA[articles]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.yunademo.nl/preview/tibobeijen.nl/?p=44</guid>
		<description><![CDATA[As applications often need to run on different setups (think: develop, test, production), configuration settings can usually be divided in a static &#8216;application&#8217; part and a more dynamic &#8216;environment&#8217; part. Zend Framework offers a very flexible set of classes that help reading and organizing configuration data and making it available throughout the entire application. But, [...]]]></description>
			<content:encoded><![CDATA[<p>As applications often need to run on different setups (think: develop, test, production), configuration settings can usually be divided in a static &#8216;application&#8217; part and a more dynamic &#8216;environment&#8217; part. Zend Framework offers a very flexible set of classes that help reading and organizing configuration data and making it available throughout the entire application. But, as very often, there is no &#8216;only way&#8217;.
</p>
<p><span id="more-44"></span></p>
<h3>Loading configuration data</h3>
<p>Basically there are three ways of loading configuration data:
</p>
<ul>
<li>1.Using Zend_Config and passing a PHP array as parameter</li>
<li>2.Using Zend_Config_Ini to load an ini configuration file</li>
<li>3.Using Zend_Config_Xml to load an xml configuration file</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$configData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'somecategory'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'somesetting'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'somevalue'</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// 1. Load array</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config<span style="color: #009900;">&#40;</span><span style="color: #000088;">$configData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// 2. Load ini file</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config_Ini<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/config.ini'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// 3. Load xml file</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config_Xml<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/config.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Alternatively, you can specify a file containing an array as require() returns the return value of a PHP script being included:
</p>
<p>config.php</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$CONFIG</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'somecategory'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'somesetting'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'somevalue'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$CONFIG</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// 1b Load array</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config<span style="color: #009900;">&#40;</span><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'path/to/config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Using configuration data</h3>
<p>Zend_Config creates a hierachy of Zend_Config instances. So a specific part of the total configuration data can be extracted and passed to a specific component. All subparts behave the same. If the component requires configuration to be passed as an array the toArray() method can be used. Instead of accessing values as properties one can also use the get() method. The advantage of get() is that a default value can be supplied to be used when the requested variable isn&#8217;t available.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// configure an instance using a specific part of the config</span>
<span style="color: #000088;">$modelComponent</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ModelComponent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">components</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">componentName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Or, if the component requires an array</span>
<span style="color: #000088;">$modelComponent</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ModelComponent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">components</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">componentName</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Get an optional config setting, supplying a default value</span>
<span style="color: #000088;">$someValue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">common</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'someValue'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'myDefaultValue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The data contained in Zend_Config is read-only which is normally a good thing. In case the configuration data has to be changed this can be overridden by supplying an additional parameter in the constructor. Then after making the neccessary changes the config object can be &#8217;sealed&#8217; by calling setReadOnly().</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// read config with $allowModifications set to true</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config<span style="color: #009900;">&#40;</span><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'path/to/config.php'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// perhaps change some bits here and there</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullMoonMadness</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">site</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">theme</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'silver'</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">allowComments</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// and prevent accidental modification</span>
<span style="color: #000088;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setReadOnly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To make the loaded configuration available throughout the entire application, Zend_Registry is probably the tool of choice.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config<span style="color: #009900;">&#40;</span><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'path/to/config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// and store in registry</span>
Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$config</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Organizing configuration data</h3>
<p>When using ini or xml configuration files, sections can be declared. On loading the file the section to be  used can be specified. A section can extend other sections thereby allowing configuration data to have a certain hierarchy.</p>
<p>Ini-file example:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>production<span style="">&#93;</span></span>
<span style="color: #000099;">www_root</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> www.mysite.com</span>
site.title <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;My Website - &quot;</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>test : production<span style="">&#93;</span></span>
<span style="color: #000099;">www_root</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> test.mysite.com</span></pre></div></div>

<p>The root node of XML files can be named arbitrarily. If you want to use sections the first level in the XML has to correspond to the section requested. There are two reserved attributes: value and extends. Other attributes can hold values that otherwise would be childnodes. See example that holds the same data as the Ini file above:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;production<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;www_root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>www.mysite.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/www_root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;site</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;My Website - &quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/production<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;test</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">&quot;production&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;www_root</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;test.mysite.com&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/test<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now the section-specific configuration can be loaded by specifying a second parameter in the constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Config_Xml<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/config.xml'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Ini, XML or Array?</h3>
<p>As mentioned at the top of this article: There is no &#8216;only&#8217; way and there is no &#8216;right&#8217; or &#8216;wrong&#8217; way. Of course there are aspects that can make you favor one approach over the other, the most notable being:</p>
<ul>
<li>Only Ini and XML provide sections, allowing you to use hierarchic configuration data</li>
<li>Ini and especially XML are better suited for tooling: Software other than PHP can read or modify configuration data.</li>
<li>When using ini files it is not possible to use quotes in a value.
<li>PHP arrays have the benefit that you can put program logic <em>in</em> them. For example, you can construct variables based on defaults that are set initially. See example below</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$CONFIG</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'defaults'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fs_root'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/var/www/mysite_com/'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #000088;">$CONFIG</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'locations'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'libraries'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'zf'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$CONFIG</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'defaults'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fs_root'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'lib/zf/'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$CONFIG</span><span style="color: #339933;">;</span></pre></div></div>

<p><em>2009-05-15: added example of loading specific section by passing section parameter to constructor</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tibobeijen.nl/blog/2008/11/30/zend_config-strategies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
