<?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>ZF Daily</title>
	<atom:link href="http://www.zfdaily.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zfdaily.com</link>
	<description>Developer&#039;s daily encounters with Zend Framework</description>
	<lastBuildDate>Thu, 30 Aug 2012 15:05:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>DluTwBootstrap updated to ZF2 RC6</title>
		<link>http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc6/</link>
		<comments>http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc6/#comments</comments>
		<pubDate>Thu, 30 Aug 2012 15:05:52 +0000</pubDate>
		<dc:creator>dlu</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>
		<category><![CDATA[DluTwBootstrap]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[Twitter bootstrap]]></category>
		<category><![CDATA[Zend framework 2]]></category>

		<guid isPermaLink="false">http://www.zfdaily.com/?p=933</guid>
		<description><![CDATA[DluTwBootstrap &#8211; the Zend Framework 2 module for generating Twitter Bootstrap UI (forms) has been updated to Zend Framework 2 RC6. About DluTwBootstrap is a Zend Framework 2 module consisting primarily of view helpers, which let you easily generate Twitter...]]></description>
			<content:encoded><![CDATA[<p>DluTwBootstrap &#8211; the Zend Framework 2 module for generating Twitter Bootstrap UI (forms) has been updated to Zend Framework 2 RC6.<br />
<span id="more-933"></span></p>
<h3>About</h3>
<p><a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap</a> is a Zend Framework 2 module consisting primarily of view helpers, which let you easily generate <a href="http://getbootstrap.com" target="_blank">Twitter Bootstrap</a> UI <a href="http://twitter.github.com/bootstrap/base-css.html#forms" target="_blank">forms</a> from your standard Zend Framework 2 Form objects without getting your hands dirty with Twitter Bootstrap mark-up. Check the <a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">on-line demo</a> to see it in action.</p>
<h3>Update to Zend Framework 2 RC6</h3>
<h4>BC breaks</h4>
<p>The Select, Radio and MultiCheckbox value options have been moved from attributes into properties (please see <a href="http://framework.zend.com/zf2/blog/entry/Zend-Framework-2-0-0rc6-Released" target="_blank">Zend Framework 2.0.0rc6 Released</a> for details). The option values for Selects, Radios and MultiCheckboxes should be now defined under <code>value_options</code> key like this:</p>
<pre class="brush: php; title: ; notranslate">
//Excerpt from an element definition
'options' =&gt; array(
    'label'          =&gt; 'Element label',
    'value_options'  =&gt; array(
        'opt_value1'     =&gt; 'Label for Option 1',
        'opt_value2'     =&gt; 'Label for Option 2',
        'opt_value3'     =&gt; 'Label for Option 3',
    ),
),
</pre>
<h4>New features</h4>
<h5>The form error messages may be suppressed</h5>
<p>The <code>formRowTwb</code>, <code>formFieldsetTwb</code> and <code>formTwb</code> view helpers now accept a boolean parameter <code>$renderErrors</code>. If set to <code>false</code>, the validation error messages for an element (or for the whole fieldset or form) will not be rendered. Note though, that the form element will still be marked as having errors (i.e. red text and outline), only the error messages are not printed. This parameter is optional and defaults to <code>true</code>. (This feature mimicks the ZF2 <code>formRow</code> view helper behaviour.)</p>
<h5>The Text input supports prepended and appended icons</h5>
<p>The Text input element always supported prepended and appended texts and now you can also prepend / append icons.</p>
<p><img src="http://www.zfdaily.com/wp-content/uploads/2012/08/PrependAppendIcons.png" alt="" /></p>
<p>The icons to prepend / append are specified in the <code>$displayOptions</code> array:</p>
<pre class="brush: php; title: ; notranslate">
//In a view script...
//Display options for an element
$myElemDisplayOptions =&gt; array(
    'class'       =&gt; 'input-medium',
    'prependIcon' =&gt; 'icon-heart',
    'appendIcon'  =&gt; 'icon-glass',
),
//Display the element
echo $this-&gt;formRowTwb($form-&gt;get('myElem'), $formType, $myElemDisplayOptions);
</pre>
<p>You can combine prepended / appended text with icons. The icons are always displayed &#8216;inside&#8217; (i.e. closer to the input element) and texts are displayed &#8216;outside&#8217; (i.e. further from the input element).</p>
<h5>The Password input supports prepended and appended texts and icons</h5>
<p>The Password input now supports prepended / appended text as well as icons. The usage is identical to the Text input.</p>
<h3>Have you found a bug?</h3>
<p>Should you find a bug in DluTwBootstrap or have a feature request, please use the <a href="https://bitbucket.org/dlu/dlutwbootstrap/issues?status=new&#038;status=open" target="_blank">DluTwBootstrap bugtracker</a> to post a new issue. Thanks for your help!</p>
<h3>Links</h3>
<ul>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap module (source repo)</a></li>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo module (source repo)</a></li>
<li><a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo Application (live on-line)</a></li>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap-demo-app" target="_blank">DluTwBootstrap Demo Application (source repo)</a></li>
<li><a href="http://www.zfdaily.com/tag/dlutwbootstrap/">Other blog posts on DluTwBootstrap module</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc6/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>DluTwBootstrap updated to ZF2 RC5 and Twitter Bootstrap 2.1.0</title>
		<link>http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc5-and-twitter-bootstrap-2-1-0/</link>
		<comments>http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc5-and-twitter-bootstrap-2-1-0/#comments</comments>
		<pubDate>Sat, 25 Aug 2012 11:27:47 +0000</pubDate>
		<dc:creator>dlu</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>
		<category><![CDATA[DluTwBootstrap]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[Twitter bootstrap]]></category>
		<category><![CDATA[Zend framework 2]]></category>

		<guid isPermaLink="false">http://www.zfdaily.com/?p=905</guid>
		<description><![CDATA[DluTwBootstrap &#8211; the Zend Framework 2 module for generating Twitter Bootstrap UI (forms) has been updated to Zend Framework 2 RC5 and Twitter Bootstrap v2.1.0. About DluTwBootstrap is a Zend Framework 2 module consisting primarily of view helpers, which let...]]></description>
			<content:encoded><![CDATA[<p>DluTwBootstrap &#8211; the Zend Framework 2 module for generating Twitter Bootstrap UI (forms) has been updated to Zend Framework 2 RC5 and Twitter Bootstrap v2.1.0.<br />
<span id="more-905"></span></p>
<h3>About</h3>
<p><a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap</a> is a Zend Framework 2 module consisting primarily of view helpers, which let you easily generate <a href="http://getbootstrap.com" target="_blank">Twitter Bootstrap</a> UI <a href="http://twitter.github.com/bootstrap/base-css.html#forms" target="_blank">forms</a> from your standard Zend Framework 2 Form objects without getting your hands dirty with Twitter Bootstrap mark-up. Check the <a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">on-line demo</a> to see it in action.</p>
<h3>Update to Zend Framework 2 RC5</h3>
<h4>BC breaks</h4>
<p>There is only a single BC break: The <code>FormButton</code> view helper has been refactored and the button label has been moved from attributes to options. Go to the <a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">on-line demo</a>, select Horizontal form, and check the Form tab, which shows the correct definition of the button component.</p>
<h4>Composer support</h4>
<p>DluTwBootstrap can be installed / updated using Composer. Please see the <a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">Installation &#8211; with Composer</a> paragraph for instructions.</p>
<h3>Update to Twitter Bootstrap 2.1.0</h3>
<p>The new version of Twitter Bootstrap adds a few new form related features but the general mark-up remains the same. There are three things worth noting though.</p>
<h4>Larger font</h4>
<p>The default distribution of Twitter Bootstrap 2.1.0 uses a larger font size and line height as compared to the version 2.0.4. It&#8217;s 14px/20px vs. 13px/18px. (There are other changes, of course, go to the <a href="http://blog.getbootstrap.com/2012/08/20/bootstrap-2-1-0-released/" target="_blank">Bootstrap 2.1.0 released</a> blog post to review them.)</p>
<p>The increased sizes were too much for me, so I used a custom built Twitter Bootstrap 2.1.0 library with the original sizing of 13px/18px. The Twitter Bootstrap library packaged with the DluTwBootstrap module is this customized smaller font version!</p>
<p>If you want to use the default distribution (or another <a href="http://twitter.github.com/bootstrap/customize.html" target="_blank">custom build</a>), just replace the <code>public/css/bootstrap.min.css</code> and optionally the <code>public/js/bootstrap.min.js</code> files.</p>
<p>Although Twitter Bootstrap recalculates other dimensions (padding, margins, inputs, etc.) based on the font size / line height in the custom build, the headings (<code>&lt;h1&gt;</code> through <code>&lt;h6&gt;</code>) remain the same and look disproportionatelly big. The DluTwBootstrap module overrides the default Twitter Bootstrap styling as little as possible, so it&#8217;s left up to you to provide a style override for these elements, if you deem it necessary.</p>
<h4>Twitter Bootstrap 2.1.0 bug</h4>
<p>There is a bug in Twitter Bootstrap v2.1.0 causing a thin gray line to be rendered above radio options in error state. To see this bug, go to the <a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">on-line demo</a>, select the Horizontal form, submit it (do not enter anything) and check the Radio and Radio Inline elements.</p>
<p>This bug has already been fixed in Twitter Bootstrap master and the fix should appear in 2.1.1 release (see <a href="https://github.com/twitter/bootstrap/issues/4526" target="_blank">Issue #4526</a> for details).</p>
<h4>jQuery update</h4>
<p>Together with Twitter Bootstrap update, the jQuery library distributed with DluTwBootstrap has been updated too to version 1.8.0. You might want to update the references in your layouts.</p>
<h3>Links</h3>
<ul>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap module (source repo)</a></li>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo module (source repo)</a></li>
<li><a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo Application (live on-line)</a></li>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap-demo-app" target="_blank">DluTwBootstrap Demo Application (source repo)</a></li>
<li><a href="http://www.zfdaily.com/tag/dlutwbootstrap/">Other blog posts on DluTwBootstrap module</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc5-and-twitter-bootstrap-2-1-0/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>DluTwBootstrap updated to ZF2 RC3</title>
		<link>http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc3/</link>
		<comments>http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc3/#comments</comments>
		<pubDate>Fri, 10 Aug 2012 16:26:10 +0000</pubDate>
		<dc:creator>dlu</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>
		<category><![CDATA[DluTwBootstrap]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[Twitter bootstrap]]></category>
		<category><![CDATA[Zend framework 2]]></category>

		<guid isPermaLink="false">http://www.zfdaily.com/?p=825</guid>
		<description><![CDATA[DluTwBootstrap &#8211; the Zend Framework 2 module for generating Twitter Bootstrap UI (forms) has been updated to Zend Framework 2 RC3. About DluTwBootstrap is a Zend Framework 2 module consisting primarily of view helpers, which let you easily generate Twitter...]]></description>
			<content:encoded><![CDATA[<p>DluTwBootstrap &#8211; the Zend Framework 2 module for generating Twitter Bootstrap UI (forms) has been updated to Zend Framework 2 RC3.<br />
<span id="more-825"></span></p>
<h3>About</h3>
<p>DluTwBootstrap is a Zend Framework 2 module consisting primarily of view helpers, which let you easily generate <a href="http://twitter.github.com/bootstrap/" target="_blank">Twitter Bootstrap</a> UI <a href="http://twitter.github.com/bootstrap/base-css.html#forms" target="_blank">forms</a> from your standard Zend Framework 2 Form objects without getting your hands dirty with Twitter Bootstrap mark-up.</p>
<h3>Reworked from scratch</h3>
<p>The module has been updated to reflect the changes in Zend Framework 2 RC3. There are numerous BC breaks, so please refactor your projects. The Zend Framework 2 API should be changing much less now as we are approaching the stable release, so the DluTwBootstrap module should be now also more stable and should not be that picky about the ZF2 version.</p>
<blockquote><p>Yes, the much asked for <code>echo $form;</code> is here (well, not exactly that but a one-liner nonetheless).</p></blockquote>
<p>The module has been not only updated to the latest ZF2 release, but also reworked from gound up so it is much cleaner and consistent now.</p>
<h3>First go to on-line demo</h3>
<p>As always, you should first visit the on-line demo here:</p>
<p><a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">http://apps.zfdaily.com/dlutwbootstrap-demo</a></p>
<p>The on-line demo shows everything the module can do side-by-side with the source used to produce the demo output. <strong>The demo application is your primary reference.</strong></p>
<h3>Components</h3>
<ul>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap module</a> – a ZF2 module containing view helpers, classes and styles which enable your applications to render Twitter Bootstrap UI (forms).</li>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo module</a> – a ZF2 module demonstrating capabilities of the DluTwBootstrap module. Install and enable this module in your app to check that you have installed DluTwBootstrap correctly.</li>
<li><a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo Application (on-line)</a> – a simple ZF2 application integrating both the modules. You should not need the app&#8217;s source, but it&#8217;s <a href="https://bitbucket.org/dlu/dlutwbootstrap-demo-app" target="_blank">here</a> for your reference.</li>
</ul>
<h3>Installation</h3>
<p>The installation steps are described in <code>README.md</code> files in the root of each package. Go to the respective repositories mentioned above to review them.</p>
<h3>Usage</h3>
<p>First of all: Yes, the much asked for <code>echo $form;</code> is here (well, not exactly that but a one-liner nonetheless). Although Zend Framework 2 itself does not support form display with a single command I decided to implement this feature with the following disclaimer:</p>
<p><em>The one-liner should be used only either for a quick form preview or in situations when there are no special requirements for form presentation. If you need anything else, use the manually controlled form output DluTwBootstrap offers.</em></p>
<p>So you can have it both ways. Either a dead simple fully automated form display on one line or a more laborous fully controlled form output. Your call.</p>
<h5>The simplest case</h5>
<ol>
<li>Create a standard <code>Zend\Form\Form</code> object</li>
<li>Pass your form object to view</li>
<li>Prepare your form: <code>$form->prepare();</code></li>
<li>Output the form: <code>echo $this->formTwb($form);</code></li>
</ol>
<p>This procedure would output a horizontal Twitter Bootstrap form. The horizontal form type is considered default in DluTwBootstrap. Pass second argument to the <code>formTwb()</code> helper to control which form type will be displayed. Use the provided constants:</p>
<p><code>echo $this->formTwb($form, \DluTwBootstrap\Form\FormUtil::FORM_TYPE_VERTICAL);</code></p>
<p>There is also the third optional parameter to <code>formTwb()</code> which contains the display options. You can use it to change the most common display aspects of the form (e.g. the size of input fields) and still use the simple one line form output. The display options also control which fields will be marked as required (more on that later). Please go to the <a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">on-line demo</a> to see how the <code>$displayOptions</code> parameter is used.</p>
<h5>The not so simple case</h5>
<p>If you need more control over your form presentation, your best friend is the <code>formRowTwb()</code> view helper. It&#8217;s used like this:</p>
<pre class="brush: php; title: ; notranslate">
echo $this-&gt;formTwb()-&gt;openTag($form, $formType);
echo $this-&gt;formRowTwb($form-&gt;get('myElement'), $formType, $elementDisplayOptions);
echo $this-&gt;formTwb()-&gt;closeTag();
</pre>
<p>This sequence of commands would output a single form element together with label, hint, description, possible validation errors and any required Twitter Bootstrap mark-up. The <code>formRowTwb()</code> helper can render any supported form element (inputs, checkboxes, buttons, etc.).</p>
<p>This is the optimal level of control. You can insert anything special between form elements yet you still do not have to think about Twitter Bootstrap markup. Just output any number of <code>formRowTwb()</code> interspersed with your custom display code, wrap everything in opening/closing tags and you are done.</p>
<p>As for the level of control, you can go in both directions from here. You can output whole fieldsets using</p>
<p><code>echo $this->formFieldsetTwb($fieldset, $formType, $fieldsetDisplayOptions);</code></p>
<p>or you can dig deeper and manually control the individual parts of the formRow using the appropriate view helpers for label, element, description, errors, etc. In this case though you are the one who is responsible for proper Twitter Bootstrap mark-up and your forms may not render correctly if you mess things up.</p>
<h5>What is obvious</h5>
<p>DluTwBootstrap module mimicks the original Zend Framework view helpers where possible. If there is the <code>formRow()</code> view helper in ZF2, DluTwBootstrap has <code>formRowTwb()</code>. Just add Twb suffix.</p>
<h5>What is not so obvious</h5>
<p>There are two DluTwBootstrap view helpers you should know about which do not have a ZF2 counter part: <code>formFieldsetTwb()</code> which we&#8217;ve already talked about (outputs whole fieldsets) and <code>formActionsTwb()</code>. The <code>formActionsTwb()</code> view helper outputs form buttons with correct Twitter Bootstrap mark-up around. See the <a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">on-line demo</a> for details.</p>
<p>DluTwBootstrap can optionally highlight required fields. This is something you won&#8217;t find in Twitter Bootstrap itself, but I think it&#8217;s useful to indicate to the user which fields are mandatory. (I do not want to start a philosophical debate on this, although your opinion is welcome. This feature is optional, so if you think form fields should be never visually marked as required, just ignore it.)</p>
<p>If you want to visually mark a field as required, use the <code>$displayOptions</code> array parameter and set <code>'required' => true</code>. (See the <a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">on-line demo</a> for details.) There are three places where you can specify the element as &#8216;required&#8217;. Their meaning differs though:</p>
<ul>
<li>&#8216;required&#8217; attribute of the element object &ndash; just adds the HTML5 &#8216;required&#8217; attribute to the tag. Makes the form/element semantically correct.</li>
<li>&#8216;required&#8217; option of the input filter &ndash; forces the user to actually fill out the field otherwise the form won&#8217;t validate</li>
<li>&#8216;required&#8217; option of the DluTwBootstrap display options &ndash; visually marks the element as required/mandatory</li>
</ul>
<h3>Supported elements</h3>
<p>All basic elements are supported by DluTwBootstrap. The TODO list contains all HTML5 elements (date, color, etc.), captcha, image and collections. If you need any of them on your form now, you can add markup for them manually in your view script.</p>
<h3>DluTwBootstrap benefits</h3>
<p>There are two important aspects worth noting: DluTwBootstrap does not force you into a specific form output mode. You can select the required level of control as you wish from fully automated output in one line of code to fully controlled output of the individual atomic form parts. You can also combine these two aproaches at will.</p>
<p>The other strong point of DluTwBootstrap is its clean separation of presentation specifics from the form definition. The form definition (i.e. the form object) contains no information used solely to control output of Twitter Bootstrap markup (e.g. a Twitter Bootstrap CSS class). The user controllable Twitter Bootstrap specifics are contained in the <code>$displayOptions</code> parameter optionaly passed to the Twb view helpers. If you decide to abandon Twitter Bootstrap and use another CSS template system, your form objects are perfectly ok, do not contain any Twitter Bootstrap specific artifacts and do not need any refactoring. Your changes would be kept exclusively inside your view scripts.</p>
<h3>BC breaks</h3>
<p>There are too many BC to mention them individually. The view helpers are renamed and removed, the parameters changed, the form objects should be defined as per ZF2 RC3 API (which is different &ndash; e.g. options instead of attributes), etc. Please check the <a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">on-line demo</a>, review the demo form output and examine the source. DluTwBootstrap is easy to use and the best way to learn it is by example.</p>
<h3>Links</h3>
<ul>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap module (source repo)</a></li>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo module (source repo)</a></li>
<li><a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo Application (live on-line)</a></li>
<li><a href="https://bitbucket.org/dlu/dlutwbootstrap-demo-app" target="_blank">DluTwBootstrap Demo Application (source repo)</a></li>
<li><a href="http://www.michaelgallego.fr/blog/?p=190" target="_blank">New Zend\Form features explained (the best explanation of Zend\Form functionality I know of)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc3/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Getting dependencies into ZF2 Controllers</title>
		<link>http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/</link>
		<comments>http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/#comments</comments>
		<pubDate>Thu, 05 Jul 2012 13:24:59 +0000</pubDate>
		<dc:creator>dlu</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>
		<category><![CDATA[Controller]]></category>
		<category><![CDATA[Dependency injection]]></category>
		<category><![CDATA[Zend framework 2]]></category>

		<guid isPermaLink="false">http://www.zfdaily.com/?p=733</guid>
		<description><![CDATA[Do your controllers need other object instances to do their work? Well, how do you get your hands on those dependencies inside your controllers? Forget new Xyz() &#8211; that&#8217;s a road to hell. There is a better way in ZF2....]]></description>
			<content:encoded><![CDATA[<p>Do your controllers need other object instances to do their work? Well, how do you get your hands on those dependencies inside your controllers? Forget <code>new Xyz()</code> &#8211; that&#8217;s a road to hell. There is a better way in ZF2. It has always been there (mostly undocumented), but only after a recent change it has become more evident and usable.<br />
<span id="more-733"></span></p>
<h3>Controllers are services</h3>
<p>Controllers are services just like any other service you might define. The important difference is they are instantiated not by you at some point in your user code, but automatically as a part of the MVC workflow. How can we do, say, a constructor injection then, when it is not our code which instantiates the controller? This might seem like a problem but it&#8217;s quite easy in fact.</p>
<h3>DI</h3>
<p>Prior to the introduction of the ServiceManager, we had DI to do the dirty work of object instantiation and wiring. As controllers were instantiated by MVC using DI, it was rather easy to instruct the DI instance manager to inject whatever was necessary and voilà we had a fully configured controller with dependencies injected &#8211; almost like magic. But there ain&#8217;t no such thing as a free magic and this magic cost speed. When the ServiceManager (i.e. a <a href="http://en.wikipedia.org/wiki/Service_locator_pattern" target="_blank">Service Locator</a>) came it eliminated the speed penalty of DI by not doing anything too clever and time consuming (e.g. introspection) and it took over the DI&#8217;s responsibilities throughout the ZF2.</p>
<p>(I won&#8217;t go into detail about the DI way of configuring controllers, because the ServiceManager is a preferred way to do things in ZF2 now. It&#8217;s still possible to use DI for this nonetheless. Drop me a line if you are interested in the DI method and I will post some code to demonstrate this.)</p>
<h3>ServiceManager</h3>
<p>Controllers are services, remember? The MVC workflow retrieves the controller instances from the <a href="http://packages.zendframework.com/docs/latest/manual/en/zend.service-manager.html" target="_blank">ServiceManager</a>. But do not rush to trace and debug your app to check it&#8217;s ServiceManager (<code>$application->getServiceManager()</code>) and see with your own eyes the controller instances there. You won&#8217;t find them there. It&#8217;s not <em>this</em> ServiceManager. The controller instances are managed by a separate dedicated scoped ServiceManager instance. So we can use neither the <code>service_manager</code> key in a config file nor the <code>getServiceConfig()</code> method in the Module class to configure our controller instances. We are thus back to our initial question: How do we do a constructor injection on a controller?</p>
<p>The dedicated ServiceManager instance used exclusively for controllers can be configured in two places: <code>controllers</code> key in a config file (please note the recent change from singular <code>controller</code> to plural <code>controllers</code>!) and <code>getControllerConfig()</code> method in the Module class. Both of these are expected to return an array with the ServiceManager configuration.</p>
<p>The <strong>controller classes which do not have any dependencies</strong> are set-up under the <code>invokables</code> subkey as a <code>'controllerServiceName' => 'Fully\Qualified\Controller\Classname'</code> pairs. Please note the recent subkey name change from <code>classes</code> to <code>invokables</code>. Other thant that this is nothing new as you must be doing this already otherwise your controllers would not work:</p>
<pre class="brush: php; title: ; notranslate">
/* module.config.php */
&lt;?php
return array(
    'controllers' =&gt; array(
        'invokables'    =&gt; array(
            //Suppose one of our routes specifies
            //a controller named 'myController'
            'myController'    =&gt; 'MyModule\Controller\MyController',
        ),
    ),
);
</pre>
<p>The easiest method to create <strong>controller instances which do have dependencies</strong> is to use a factory. You can either write a proper factory implementing the <code>Zend\ServiceManager\FactoryInterface</code>:</p>
<pre class="brush: php; title: ; notranslate">
/* MyModule/src/MyModule/ControllerFactory/MyControllerFact.php */
namespace MyModule/ControllerFactory;
use \Zend\ServiceManager\FactoryInterface;
use \Zend\ServiceManager\ServiceLocatorInterface;

class MyControllerFact implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator) {
        /* @var $serviceLocator \Zend\Mvc\Controller\ControllerManager */
        $sm   = $serviceLocator-&gt;getServiceLocator();
        $depA = $sm-&gt;get('depA');
        $depB = $sm-&gt;get('depB');
        $controller = new \MyModule\Controller\MyController($depA, $depB);
        return $controller;
    }
}
</pre>
<p>And then simply put the reference to this factory under the <code>factories</code> subkey into the configuration file:</p>
<pre class="brush: php; title: ; notranslate">
/* module.config.php */
&lt;?php
return array(
    'controllers' =&gt; array(
        'factories'    =&gt; array(
            //Suppose one of our routes specifies
            //a controller named 'myController'
            'myController'    =&gt; 'MyModule\ControllerFactory\MyControllerFact',
        ),
    ),
);
</pre>
<p>Or you can use a closure and define the factory as a part of the configuration. Be careful though not to do this in the configuration file (please see the note <em>Configuration and PHP</em> on the <a href="http://packages.zendframework.com/docs/latest/manual/en/zend.service-manager.quick-start.html" target="_blank">ServiceManager Quick Start</a> page for reasons). The proper place for this is the <code>getControllerConfig()</code> method:</p>
<pre class="brush: php; title: ; notranslate">
/* MyModule/Module.php */
&lt;?php
namespace MyModule;
use \Zend\Mvc\Controller\ControllerManager;

class Module
{
    public function getControllerConfig() {
        return array(
            'factories' =&gt; array(
                //Suppose one of our routes specifies
                //a controller named 'myController'
                'myController'    =&gt; function(ControllerManager $cm) {
                    $sm   = $cm-&gt;getServiceLocator();
                    $depA = $sm-&gt;get('depA');
                    $depB = $sm-&gt;get('depB');
                    $controller = new \MyModule\Controller\MyController($depA,
                                                                        $depB);
                    return $controller;
                },
            ),
        );

    //The getConfig() and getAutoloaderConfig() methods are omitted for brevity
}
</pre>
<p>Both ways you have the application&#8217;s ServiceManager instance available in your factory method (via the <code>getServiceLocator()</code> method) so it&#8217;s easy to pull any collaborators you might need to instantiate your controllers.</p>
<h3>ServiceManager plugin fixes</h3>
<p>This shape of things is a result of a recent ServiceManager rework. Please read <a href="https://github.com/zendframework/zf2/pull/1695" target="_blank">Evan&#8217;s comments on the pull request #1695</a> to get a quick overview of the ServiceManager plugin configuration. The features described in this post are just a subset of the possibilities offered by various ZF2 ServiceManagers.</p>
<h3>Conclusion</h3>
<p>The ServiceManager (and DI) and its systematic utilization in ZF2 is a major improvement over ZF1. There is no similar facility in ZF1 (AFAIK) which would help you inject dependencies into controllers, you are on your own there. (It can be done though. If anyone is interested, I will happily share my ZF1 solution using an action helper, but it&#8217;s way more cumbersome than the elegant ServiceManager approach of ZF2.)</p>
<p>So there you have it. It&#8217;s so easy to configure your controllers in ZF2 now, that you really have no more excuses. Say &#8216;NO!&#8217; to all <code>new</code>s and singletons and do things the right way. You will thank yourself later.</p>
<p><span class="picture-credits">Picture by <a href="http://www.flickr.com/photos/34654941@N02/6136949516/" target="_blank">Ali Hussein</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>DluTwBootstrap updated to ZF2 Beta4</title>
		<link>http://www.zfdaily.com/2012/06/dlutwbootstrap-updated-to-zf2-beta4/</link>
		<comments>http://www.zfdaily.com/2012/06/dlutwbootstrap-updated-to-zf2-beta4/#comments</comments>
		<pubDate>Thu, 14 Jun 2012 15:34:24 +0000</pubDate>
		<dc:creator>dlu</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>
		<category><![CDATA[DluTwBootstrap]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[Twitter bootstrap]]></category>
		<category><![CDATA[Zend framework 2]]></category>

		<guid isPermaLink="false">http://www.zfdaily.com/?p=666</guid>
		<description><![CDATA[The DluTwBootstrap module facilitating usage of Twitter Bootstrap in Zend Framework 2 applications has been updated to reflect the changes brought by Zend Framework 2 Beta 4. DluTwBootstrap module The Form component has been completely rewritten in ZF2 beta4. The...]]></description>
			<content:encoded><![CDATA[<p>The <a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap module</a> facilitating usage of <a href="http://twitter.github.com/bootstrap/" target="_blank">Twitter Bootstrap</a> in <a href="http://framework.zend.com/zf2" target="_blank">Zend Framework 2</a> applications has been updated to reflect the changes brought by <a href="http://framework.zend.com/zf2/blog/entry/Zend-Framework-2-0-0beta4-Released" target="_blank">Zend Framework 2 Beta 4</a>.<br />
<span id="more-666"></span></p>
<h3>DluTwBootstrap module</h3>
<p>The Form component has been completely rewritten in ZF2 beta4. The DluTwBootstrap module has been updated to support the new Form component(s).<br />
<a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap repo</a></p>
<h3>DluTwBootstrap Demo module</h3>
<p>The demo part of the DluTwBootstrap module has been separated into its own module. This way it&#8217;s easy to quickly disable the demo when not needed yet keep it unobtrusively in your project should you need to check anything or experiment. (Just comment the demo module out in <code>config/application.config.php</code>.)<br />
<a href="https://bitbucket.org/dlu/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo repo</a></p>
<h3>DluTwBootstrap Demo Application</h3>
<p>There is also a new application comprising both the forementioned modules. This application is running on-line for you to quickly see the DluTwBootstrap module in action without the need to install any modules yourself. You can thus decide in seconds if the DluTwBootstrap module is a good fit for your project.<br />
<a href="http://apps.zfdaily.com/dlutwbootstrap-demo" target="_blank">DluTwBootstrap Demo Application LIVE</a></p>
<p>The source code of the application is also available, but generaly you should not need it as it contains nothing more than the two DluTwBootstrap modules. It might come in handy though as a reference if you are having troubles wiring the modules into your project.<br />
<a href="https://bitbucket.org/dlu/dlutwbootstrap-demo-app" target="_blank">DluTwBootstrap Demo Application repo</a></p>
<h3>ZF2 version</h3>
<p><strong>IMPORTANT:</strong> The <code>README.md</code> in DluTwBootstrap module states the supported ZF2 version (please see the <a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap repo overview</a> and scroll down to &#8216;Supported versions&#8217;). As ZF2 is in beta the code base changes very quickly even in scope of a single version designation (i.e. Beta 4). If the DluTwBootstrap module does not seem to work, check the version of your ZF2 library and update to the version <strong>and commit</strong> specified in <code>README.md</code>.</p>
<h3>Stay tuned</h3>
<p>Two things are comming:</p>
<ul>
<li>A proper tutorial for Forms with DluTwBootstrap</li>
<li>Support for Navigation elements (menus, buttons, tabs, etc.)
</ul>
<p>Keep an eye on this blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zfdaily.com/2012/06/dlutwbootstrap-updated-to-zf2-beta4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Configuring PHP ini Settings in ZF2</title>
		<link>http://www.zfdaily.com/2012/04/configuring-php-settings-in-zf2/</link>
		<comments>http://www.zfdaily.com/2012/04/configuring-php-settings-in-zf2/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 16:40:14 +0000</pubDate>
		<dc:creator>dlu</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[DluPhpSettings]]></category>
		<category><![CDATA[PHP ini settings]]></category>

		<guid isPermaLink="false">http://www.zfdaily.com/?p=588</guid>
		<description><![CDATA[Spoiled by the ease with which it was possible to configure the PHP ini directives in Zend Framework 1 (in application.ini) I was hoping for a similar facility in ZF2. As far as I know, there is none (as of...]]></description>
			<content:encoded><![CDATA[<p>Spoiled by the ease with which it was possible to configure the PHP ini directives in Zend Framework 1 (in <code>application.ini</code>) I was hoping for a similar facility in ZF2. As far as I know, there is none (as of Beta 4). The remedy is easy though.<br />
<span id="more-588"></span></p>
<div class="post-update">
This post has been updated to reflect the changes in Zend Framework 2 <strong>Beta 4</strong>
</div>
<h3>The task</h3>
<p>We want to be able to configure the PHP ini settings in our ZF2 application, integrate the process naturally into the application workflow and use the existing configuration infrastructure (i.e. merging of configuration files, global / local overrides, etc.). Let&#8217;s build a ZF2 module for this!</p>
<h3>The module</h3>
<p>We should rather say &#8216;a mini-module&#8217; as it really needs just a single file – Module.php. Let&#8217;s call this module PhpSettings. Here is its Module class:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
namespace PhpSettings;

use Zend\EventManager\Event;

class Module
{
    /**
     * Configure PHP ini settings on the bootstrap event
     * @param Event $e
     */
    public function onBootstrap(Event $e) {
        $app         = $e-&gt;getParam('application');
        $config      = $app-&gt;getConfiguration();
        $phpSettings = $config['phpSettings'];
        if($phpSettings) {
            foreach($phpSettings as $key =&gt; $value) {
                ini_set($key, $value);
            }
        }
    }

    /* The getAutoloaderConfig() and getConfig() methods are left out here
       for brevity, as they are completely standard.
     */
}
</pre>
<p>The public method <code>onBootstrap()</code> is automatically recognized as a bootstrap event listener (a new feature in Beta4 &#8211; in Beta3 we had to register the listener in the init() method manually). It retrieves the configuration and iterates over its <code>phpSettings</code> entries passing them to the standard PHP <code>ini_set()</code> function.</p>
<p>Do not forget to enable your new module in your <code>/config/application.config.php</code>.</p>
<h3>Configuration</h3>
<p>The only thing left is to let our module know what directives we want to set. Just place <code>phpSettings</code> key into a configuration file and put all required PHP ini directives as its subkeys. You can find the <a href="http://www.php.net/manual/en/ini.list.php" target="_blank">list of all available PHP ini directives</a> in the PHP docs.</p>
<p>You should follow the global/local concept of ZF2 configuration. <strong>NOTE:</strong> The naming of the default global / local configuration files changed in Beta4 &#8211; the &#8216;config&#8217; part of the file name has been omitted. Watch out for this if your good old config files in the autoload directory stopped working after migration to Beta4.</p>
<p>Place your global environment agnostic options into global configuration files (e.g. <code>/config/autoload/module.DluPhpSettings.global.php</code>). These files will be included in your VCS. Here is an example of a global configuration:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * My Global Configuration
 * /config/autoload/module.DluPhpSettings.global.php
 *
 * You can use this file for overridding configuration values from modules, etc.  
 * You would place values in here that are agnostic to the environment and not 
 * sensitive to security. 
 *
 * @NOTE: In practice, this file will typically be INCLUDED in your source 
 * control, so do not include passwords or other sensitive information in this 
 * file.
 */

return array(
    'phpSettings'   =&gt; array(
        'display_startup_errors'        =&gt; false,
        'display_errors'                =&gt; false,
        'max_execution_time'            =&gt; 60,
        'date.timezone'                 =&gt; 'Europe/London',
        'mbstring.internal_encoding'    =&gt; 'UTF-8',
    ),
);
</pre>
<p>Your environment specific or sensitive information should go into local configuration files (e.g. <code>/config/autoload/module.DluPhpSettings.local.php</code>). Values in these files override the global configuration and they are excluded from the version control by default (if you are using the <a href="https://github.com/zendframework/ZendSkeletonApplication" target="_blank">Zend Skeleton Application</a>).</p>
<p>Here is an example of a local configuration present on a developer&#8217;s machine. It overrides some values from the global configuration:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * My Local Configuration (Development)
 * /config/autoload/module.DluPhpSettings.local.php
 *
 * This configuration override file is for overriding environment-specific and
 * security-sensitive configuration information.
 *
 * @NOTE: This file is ignored from Git by default with the .gitignore included
 * in ZendSkeletonApplication. This is a good practice, as it prevents sensitive
 * credentials from accidentally being comitted into version control.
 */

return array(
    'phpSettings'   =&gt; array(
        'display_startup_errors'        =&gt; true,
        'display_errors'                =&gt; true,
        'max_execution_time'            =&gt; 600,
    ),
);
</pre>
<h3>Conclusion</h3>
<p>We have created an ultra simple module which cleanly sets the PHP environment for our application. If you do not feel like coding even the single class needed for this module, you can download the <a href="https://bitbucket.org/dlu/dluphpsettings" target="_blank">DluPhpSettings module</a> from my git repository.</p>
<p>How do YOU configure your PHP environment in your ZF2 applications? Let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zfdaily.com/2012/04/configuring-php-settings-in-zf2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ZF2 Gets the baseUrl Wrong</title>
		<link>http://www.zfdaily.com/2012/04/zf2-gets-the-baseurl-wrong/</link>
		<comments>http://www.zfdaily.com/2012/04/zf2-gets-the-baseurl-wrong/#comments</comments>
		<pubDate>Thu, 19 Apr 2012 13:55:32 +0000</pubDate>
		<dc:creator>dlu</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>
		<category><![CDATA[Alias]]></category>
		<category><![CDATA[BaseUrl]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[DluBaseUrl]]></category>
		<category><![CDATA[Vhost]]></category>

		<guid isPermaLink="false">http://www.zfdaily.com/?p=518</guid>
		<description><![CDATA[There seems to be a problem in Zend Framework 2 beta 3 which prevents deployment of your application into a vhost subdirectory. There are hacks to circumvent this problem but if you like clean solutions, you will use the DluBaseUrl...]]></description>
			<content:encoded><![CDATA[<p>There seems to be a problem in Zend Framework 2 beta 3 which prevents deployment of your application into a vhost subdirectory. There are hacks to circumvent this problem but if you like clean solutions, you will use the <a href="https://bitbucket.org/dlu/dlubaseurl" target="_blank">DluBaseUrl</a> &#8216;mini&#8217; module to do it right.</p>
<p><span id="more-518"></span></p>
<div class="post-update">
<strong>UPDATE:</strong> This bug has been fixed in Zend Framework 2 <strong>Beta 4</strong>. Update your ZF2 library to Beta 4 and use the latest Skeleton Application and you should be ok out of the box.
</div>
<p>I was hit by this bug on my very first encounter with Zend Framework 2 when I tried to get the<a href="https://github.com/zendframework/ZendSkeletonApplication" target="_blank"> Zend Skeleton Application</a> up and running. I had the application set-up on my localhost on the Apache web server in a vhost &#8216;subdirectory&#8217; (i.e. alias) called <code>zf2skeleton</code>. I was trying to access it like this: <code>http://localhost/zf2skeleton</code> but I was only getting the following error:</p>
<p><code>The requested controller could not be mapped to an existing controller class.</code></p>
<p>After some unsuccessful tracing through the ZF2 MVC code I resorted to Rob Allen&#8217;s excellent <a href="http://akrabat.com/zend-framework-2-tutorial/" target="_blank">Getting Started with ZF2</a> tutorial and followed it to the letter. That included setting up a dedicated vhost (i.e. no vhost subdirectory). And voilà, the Skeleton application worked! But wait, I do not want to create a new Apache vhost for every little ZF2 project I am going to play with. What&#8217;s wrong?</p>
<h3>The bug</h3>
<p>The problem is, that ZF2 is getting the baseUrl wrong. Instead of my actual baseUrl <code>/zf2skeleton</code> the <code>Zend\Http\PhpEnvironment\Request::detectBaseUrl()</code> method returns an empty string. There is an issue for this in the bugtracker here: <a href="http://framework.zend.com/issues/browse/ZF2-206" target="_blank">baseUrl incorrect when installation is within a user&#8217;s document directory</a>. Obviously not all users experience this incorrect behaviour, so it is likely it is related to a specific platform / webserver (I am on Windows / ZendServer / Apache). Anyway until this bug is removed, all those who are affected have to prime the ZF2 with a correct baseUrl manually.</p>
<h3>Easy hack</h3>
<p>If you want to go the quick and dirty road, you just need to squeeze one line of code into your <code>/public/index.php</code>:</p>
<pre class="brush: php; first-line: 19; gutter: true; highlight: [20,21]; title: ; notranslate">
$application = new Zend\Mvc\Application;
//Insert the following line with correct baseUrl
$application-&gt;getRequest()-&gt;setBaseUrl('/my-base-url');
$bootstrap-&gt;bootstrap($application);
$application-&gt;run()-&gt;send();
</pre>
<p>This will get the job done but you might feel uneasy about hacking the <code>index.php</code> in such a way. Although this modification is perfectly ok for experimenting with ZF2, it&#8217;s obviously inadequate for anything more.</p>
<h3>DluBaseUrl module</h3>
<p>There are probably many ways to accomplish the simple task of manually setting the baseUrl &#8216;the right way&#8217;. Using a custom ZF2 module may seem like an overkill but you shall see it is a good fit here.</p>
<p>Download or clone the <a href="https://bitbucket.org/dlu/dlubaseurl" target="_blank">DluBaseUrl module</a>, install and enable it as usual. (The installation is described in detail on the <a href="https://bitbucket.org/dlu/dlubaseurl/overview" target="_blank">module&#8217;s git repository overview page</a>.)</p>
<p>Beside not having to touch the <code>/public/index.php</code> file the main benefit in using the DluBaseUrl module is its inherent relation to the application configuration – you can very easily get your fingers on the merged configuration in module&#8217;s bootstrap. Since the value of baseUrl is directly implied by the local environment, we should configure it in a local configuration file. ZF2 has a facility for this: put a config file with <code>.local.config.php</code> suffix into your <code>/config/autoload</code> directory. (There is a template config file shipped with DluBaseUrl module.) Configure your local baseUrl path in this config file and you are done.</p>
<p>By using this module you are also getting the benefit of an easy deactivation when the bug in ZF2 is removed. Just disable the DluBaseUrl module in your <code>/config/application.config.php</code>.</p>
<p>Modules are very powerful tool in ZF2. You can do great things with them, but you can also do very little things with them. Since they are designed to load and bootstrap quick, you do not have to worry about any negative impact on performance.</p>
<h3>Your mileage may vary</h3>
<p>As this bug is probably related to a specific platform, you might have not experienced it. I would be curious to know who encountered it and in what environment. Please drop me a line below saying if you are running ok out of the box or if you had to hack it somehow. If you have been hit by this bug, do not forget to <a href="http://framework.zend.com/issues/browse/ZF2-206" target="_blank">vote for it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zfdaily.com/2012/04/zf2-gets-the-baseurl-wrong/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Twitter Bootstrap Forms with ZF2. Easily.</title>
		<link>http://www.zfdaily.com/2012/04/twitter-bootstrap-forms-with-zf2-easily/</link>
		<comments>http://www.zfdaily.com/2012/04/twitter-bootstrap-forms-with-zf2-easily/#comments</comments>
		<pubDate>Sun, 08 Apr 2012 14:51:58 +0000</pubDate>
		<dc:creator>dlu</dc:creator>
				<category><![CDATA[Zend Framework 2]]></category>
		<category><![CDATA[DluTwBootstrap]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[Twitter bootstrap]]></category>
		<category><![CDATA[Zend framework 2]]></category>

		<guid isPermaLink="false">http://www.zfdaily.com/?p=363</guid>
		<description><![CDATA[Twitter Bootstrap contains pretty much everything you might need in a web app UI (and more). Its forms are nice and clean so why not use them with ZF2? I wrote a module DluTwBootstrap to make the process dead easy....]]></description>
			<content:encoded><![CDATA[<p><a href="http://twitter.github.com/bootstrap/index.html" target="_blank">Twitter Bootstrap</a> contains pretty much everything you might need in a web app UI (and more). Its <a href="http://twitter.github.com/bootstrap/base-css.html#forms" target="_blank">forms</a> are nice and clean so why not use them with ZF2? I wrote a module <a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank"><strong>DluTwBootstrap</strong> </a>to make the process dead easy.</p>
<p><span id="more-363"></span></p>
<div class="post-update">
<strong>UPDATE 1:</strong> ZF2 <strong>Beta 4</strong> contains a completely rewritten Form package. The Beta 3 form decorators in DluTwBootstrap module described in this post are obsolete and <strong>do not work</strong> in Beta 4. Please stay tuned for a new version of the DluTwBootstrap module which will support ZF2 Beta 4 as well as more Twitter Bootstrap features (navigation).
</div>
<div class="post-update">
<strong>UPDATE 2:</strong> The DluTwBootstrap module has been updated to Zend Framework 2 Beta 4. Please see <a href="/2012/06/dlutwbootstrap-updated-to-zf2-beta4/">this post</a> for more info.
</div>
<div class="post-update">
<strong>UPDATE 3:</strong> The DluTwBootstrap module has been updated to Zend Framework 2 RC3. Please see <a href="http://www.zfdaily.com/2012/08/dlutwbootstrap-updated-to-zf2-rc3/">DluTwBootstrap updated to ZF2 RC3</a> for more info.
</div>
<blockquote><p>DluTwBootstrap supports almost everything Twitter Bootstrap offers for forms.</p></blockquote>
<h3>Wait! Do not reinvent the wheel</h3>
<p>Before I set out to write my own module I first looked for an available one and indeed there were two. Unfortunately neither of them suited my needs. But maybe they will be a better fit for you so here are the links:</p>
<ul>
<li><a href="https://github.com/widmogrod/zf2-twitter-bootstrap-module" target="_blank">Twitter Bootstrap</a> by Gabriel Habryn</li>
<li><a href="https://github.com/mwillbanks/TwitterBootstrapFormDecorators" target="_blank">Twitter Bootstrap Form Decorators</a> by Mike Willbanks</li>
</ul>
<h3>DluTwBootstrap — what can it do for you?</h3>
<p>DluTwBootstrap supports almost everything Twitter Bootstrap offers for <a href="http://twitter.github.com/bootstrap/base-css.html#forms" target="_blank">forms</a>.</p>
<h5>Form types</h5>
<p>All four Twitter Bootstrap form types are supported:</p>
<ul>
<li>Horizontal</li>
<li>Vertical</li>
<li>Inline</li>
<li>Search</li>
</ul>
<h5>Controls</h5>
<p>All ZF2 form elements except Captcha and Image are supported:</p>
<ul>
<li>Button</li>
<li>Checkbox</li>
<li>File</li>
<li>Hash</li>
<li>Hidden</li>
<li>MultiCheckbox</li>
<li>Multiselect</li>
<li>Password</li>
<li>Radio</li>
<li>Reset</li>
<li>Select</li>
<li>Submit</li>
<li>Text</li>
<li>Textarea</li>
</ul>
<h5>Element descriptions</h5>
<p>Various descriptive and help texts are supported depending on the type of the element:</p>
<ul>
<li>Label</li>
<li>Description</li>
<li>Inline help</li>
<li>Placeholder text</li>
</ul>
<p><img class="alignnone size-full wp-image-441" title="Element Descriptions" src="http://www.zfdaily.com/wp-content/uploads/2012/04/ElementDescriptions.png" alt="" width="419" height="76" /></p>
<h5>Error state and required elements</h5>
<p>Elements with validation errors are rendered in red and the validation errors are clearly visible.</p>
<p><a href="http://www.zfdaily.com/wp-content/uploads/2012/04/ElementErrors-e1334239939309.png"><img class="alignnone size-full wp-image-444" title="Element Errors" src="http://www.zfdaily.com/wp-content/uploads/2012/04/ElementErrors-e1334239939309.png" alt="" width="627" height="108" /></a></p>
<p>The required elements stand out.</p>
<p><img class="alignnone size-full wp-image-450" title="Required field" src="http://www.zfdaily.com/wp-content/uploads/2012/04/Required.png" alt="" width="543" height="214" /></p>
<h5>Extras</h5>
<p>The MultiCheckbox and Radio elements can be optionally rendered inline.</p>
<p><img class="alignnone size-full wp-image-453" title="Inline Elements" src="http://www.zfdaily.com/wp-content/uploads/2012/04/InlineElements.png" alt="" width="629" height="408" /></p>
<p>The Text element can optionally have a prepended or appended pieces of text.</p>
<p><img class="alignnone size-full wp-image-456" title="Prepend Append" src="http://www.zfdaily.com/wp-content/uploads/2012/04/PrependAppend.png" alt="" width="597" height="77" /></p>
<p>If the form has a legend set, it is displayed as a form heading.</p>
<p><img class="alignnone size-full wp-image-460" title="Form Legend" src="http://www.zfdaily.com/wp-content/uploads/2012/04/FormLegend.png" alt="" width="592" height="151" /></p>
<h3>Get, install, check</h3>
<p>First you need to get the <a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap module</a>, install it in your ZF2 application and check the demo page to see that everything is working correctly.</p>
<p><em>Please see the <a href="https://bitbucket.org/dlu/dlutwbootstrap/overview" target="_blank">repository overview page</a> with detailed installation instructions.</em></p>
<h3>Twitter Bootstrap libraries</h3>
<p>Enabling the DluTwBootstrap module in your <code>./config/application.config.php</code> <strong>WILL NOT</strong> automatically provide the Twitter Bootstrap environment for your project. You either have to already have the Twitter Bootstrap functioning or you can follow instructions supplied with DluTwBootstrap to manually enable it for your project. All necessary libraries and scripts are packaged with the module as well as an example layout template, so it is pretty easy.</p>
<h3>Demo forms</h3>
<p>There are demo forms included with the module. Go to the URL<br />
<code>http://&lt;your-machine&gt;/tw-bootstrap-demo/form</code><br />
to see them in action. Check the source code to see how the forms are constructed.</p>
<p><a href="http://www.zfdaily.com/wp-content/uploads/2012/04/Horizontal.png"><img class="size-thumbnail wp-image-481 alignleft" title="Horizontal Form" src="http://www.zfdaily.com/wp-content/uploads/2012/04/Horizontal-e1334245943683-150x150.png" alt="" width="150" height="150" /></a></p>
<p><a href="http://www.zfdaily.com/wp-content/uploads/2012/04/Vertical.png"><img class="size-thumbnail wp-image-482 alignnone" title="Vertical Form" src="http://www.zfdaily.com/wp-content/uploads/2012/04/Vertical-e1334245908484-148x150.png" alt="" width="148" height="150" /></a></p>
<p><a href="http://www.zfdaily.com/wp-content/uploads/2012/04/Inline.png"><img class="size-thumbnail wp-image-503 alignleft" title="Inline Form" src="http://www.zfdaily.com/wp-content/uploads/2012/04/Inline-e1334245799605-150x73.png" alt="" width="150" height="73" /></a></p>
<p><a href="http://www.zfdaily.com/wp-content/uploads/2012/04/Search.png"><img class="size-thumbnail wp-image-504  alignnone" title="Search Form" src="http://www.zfdaily.com/wp-content/uploads/2012/04/Search-e1334245859384-150x84.png" alt="" width="150" height="84" /></a></p>
<h3>Basic usage</h3>
<p>The DluTwBootstrap module is basically just a set of forms, elements and decorators extending the original ZF2 classes. The usage is very simple. Just extend one of the supplied form classes and go about the rest of your form as usual and you are done.</p>
<pre class="brush: php; title: ; notranslate">
use DluTwBootstrap\Form\Horizontal;
class MyForm extends Horizontal
{
    public function init() {
        //Add your form controls in any standard ZF2 way
        //...
    }
}
</pre>
<p>By extending one of the supplied form classes (<code>Horizontal, Vertical, Inline, Search</code>) you are configuring the form plugin loader with custom prefix paths to load the supplied elements and decorators before the standard ones. As this is the standard way to extend the form functionality in ZF, you are still able to use any standard way to add elements to the form: <code>addElement(), createElement()</code> or configuration.</p>
<h3>Advanced usage</h3>
<p>There are a few Twitter Bootstrap form features not addressed by any standard ZF2 form means:</p>
<ul>
<li>Inline help text</li>
<li>Placeholder text</li>
<li>Inline Radio and MultiCheckbox elements</li>
<li>Prepend and Append texts for the Text element</li>
<li>Indented action buttons</li>
</ul>
<p>All these are accessible either via setter methods (e.g. <code>setInlineHelp(), setPlaceholderText()</code>) or using configuration. The indented action buttons require a specific display group.</p>
<p><em>Please see the <a href="https://bitbucket.org/dlu/dlutwbootstrap/overview" target="_blank">repository overview page</a> for detailed instructions on basic as well as advanced usage.<br />
</em></p>
<h3>All this in vain (sort of)</h3>
<p>This is all very nice, but it won&#8217;t last! The ZF2 Form package is getting a major overhaul and some very basic concepts are going to change (e.g. we likely lose the decorators in favour of the view helpers). See the <a href="http://framework.zend.com/wiki/display/ZFDEV2/RFC+-+Forms" target="_blank">RFC &#8211; Forms</a> to learn more. When the new Form package arrives in one of the future betas this module will need massive refactoring (and probably your form classes too). But until then we can easily generate nice looking Twitter Bootstrap forms using the <a href="https://bitbucket.org/dlu/dlutwbootstrap" target="_blank">DluTwBootstrap module</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zfdaily.com/2012/04/twitter-bootstrap-forms-with-zf2-easily/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Fighting circular dependencies in Bootstrap</title>
		<link>http://www.zfdaily.com/2012/03/fighting-circular-dependencies-in-bootstrap/</link>
		<comments>http://www.zfdaily.com/2012/03/fighting-circular-dependencies-in-bootstrap/#comments</comments>
		<pubDate>Sun, 11 Mar 2012 16:18:33 +0000</pubDate>
		<dc:creator>dlu</dc:creator>
				<category><![CDATA[Zend Framework 1]]></category>
		<category><![CDATA[Bootstrap]]></category>
		<category><![CDATA[Circular dependency]]></category>
		<category><![CDATA[Zend framework 1]]></category>

		<guid isPermaLink="false">http://www.zfdaily.com/?p=151</guid>
		<description><![CDATA[Have you ever encountered this error message? Fatal error: Uncaught exception &#8216;Zend_Application_Bootstrap_Exception&#8217; with message &#8216;Circular resource dependency detected&#8217; in&#8230; Damn. Two of my resources are trying to bootstrap each other! Depending on the complexity of your Bootstrap you might be...]]></description>
			<content:encoded><![CDATA[<p>Have you ever encountered this error message?</p>
<p class="screen-output"><strong>Fatal error</strong>: Uncaught exception &#8216;Zend_Application_Bootstrap_Exception&#8217; with message &#8216;Circular resource dependency detected&#8217; in&#8230;</p>
<blockquote><p>Damn. Two of my resources are trying to bootstrap each other!</p></blockquote>
<p>Depending on the complexity of your Bootstrap you might be in for a rather long investigation of your resources and their dependencies. If you are initializing only a few resources you will probably identify the culprits straight away but if your Bootstrap is a bit longer the offending resources might be difficult to find and untangle. Let&#8217;s build a simple tool for Zend Framework 1 applications which will make the troubleshooting easier.</p>
<h3>What is circular dependency in Bootstrap?</h3>
<p>Imagine situation when resource A needs to bootstrap resource B and resource B needs to bootstrap A. Like this:</p>
<p><a href="http://www.zfdaily.com/wp-content/uploads/2012/03/CircularDependency1.png"><img class="alignnone size-full wp-image-161 no-border" title="Circular Dependency - direct" src="http://www.zfdaily.com/wp-content/uploads/2012/03/CircularDependency1.png" alt="" width="254" height="74" /></a></p>
<p>The resources may also depend on each other via an intermediary (or more):</p>
<p><img class="size-full wp-image-170 no-border alignnone" title="Circular Dependency - via an intermediary" src="http://www.zfdaily.com/wp-content/uploads/2012/03/CircularDependency2.png" alt="" width="284" height="184" /></p>
<p>The bootstrapping mechanism of Zend Framework detects these dependencies and halts the execution with the forementioned fatal error (otherwise we would end up in an infinite loop). Unfortunately the error itself is of little help when troubleshooting as we only get the names of two resources taking part in the loop and they are in a pretty messy format on top of that. Try to decipher the names of the two resources from the complete error message:</p>
<p class="screen-output"><strong>Fatal error</strong>: Uncaught exception &#8216;Zend_Application_Bootstrap_Exception&#8217; with message &#8216;Circular resource dependency detected&#8217; in C:\WebDev\Libraries\ZF\v1.11.11\library\Zend\Application\Bootstrap\BootstrapAbstract.php:662 Stack trace: #0 C:\WebDev\Libraries\ZF\v1.11.11\library\Zend\Application\Bootstrap\BootstrapAbstract.php(629): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_executeResource(&#8216;resourceC&#8217;) #1 C:\WebDev\PhpStorm\SandBoxMVC-ZF1\application\Bootstrap.php(46): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_bootstrap(&#8216;resourceC&#8217;) #2 C:\WebDev\Libraries\ZF\v1.11.11\library\Zend\Application\Bootstrap\BootstrapAbstract.php(669): Bootstrap-&gt;_initResourceD() #3 C:\WebDev\Libraries\ZF\v1.11.11\library\Zend\Application\Bootstrap\BootstrapAbstract.php(629): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_executeResource(&#8216;resourceD&#8217;) #4 C:\WebDev\PhpStorm\SandBoxMVC-ZF1\application\Bootstrap.php(53): Zend_Application_Bootstrap_BootstrapAbstract-&gt;_bootstrap(&#8216;resourceD&#8217;) #5 C:\WebDev\Libraries\ZF\v1.11.11\library\Zend\Applic in <strong>C:\WebDev\Libraries\ZF\v1.11.11\library\Zend\Application\Bootstrap\BootstrapAbstract.php</strong> on line <strong>662</strong></p>
<p>You are right, it&#8217;s <strong>resourceC</strong> and <strong>resourceD</strong>, but have you had fun extracting these out? And more importantly, can you be sure that it is only these two resources which form the dependency circle? (Hint: you cannot.) What we need is a clearer view of resource dependencies.</p>
<h3>Extend the original ZF Bootstrap</h3>
<p>We will tap into the bootstrap process to display resource names as they are being initialized. To do this we need to extend the <code>Zend_Application_Bootstrap_Bootstrap</code> class and override the <code>_executeResource()</code> method. We will then save our new bootstrap class in a library and use it as a base class for the actual <code>Bootstrap</code> class of our application.</p>
<pre class="brush: php; highlight: [39]; title: ; notranslate">
class Dlu_Wal_Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    /**
     * Turns Bootstrap Debug logging on/off
     * Redefine in descendant
     * @var boolean
     */
    protected $_logging		= false;

    /**
     * Current indentation level
     * @var integer
     * @see _executeResource()
     */
    protected $_indent		= 0;

    /**
     * Resource counter
     * Counts resources as they are being bootstrapped
     * @var int
     */
    protected $_rscCounter  = 0;

    /* ****************************** METHODS ************************** */

    /**
     * Execute a resource with optional logging
     * Echoes the resource name and proxies to the overloaded method
     * @param  string $resource
     */
    protected function _executeResource($resource) {
        $resource   = strtolower($resource);
        if($this-&gt;_logging &amp;&amp; !in_array($resource, $this-&gt;_run)) {
            $this-&gt;_rscCounter++;
            $line   = sprintf('#%03d %s %s', $this-&gt;_rscCounter,
                              str_repeat(' ', $this-&gt;_indent * 3),
                              $resource);
            //NOTE! Wrap the $line variable in html pre tag!
            echo &quot;$line&quot;;
            $startLogged	= true;
            $this-&gt;_indent++;
        } else {
            $startLogged	= false;
        }
        parent::_executeResource($resource);
        if($startLogged) {
            $this-&gt;_indent--;
        }
    }
}
</pre>
<p>Please note the line 39 and the preceding comment if you are going to copy and paste this piece of code. (It is safer to <a href="https://bitbucket.org/dlu/web-app-library/downloads" target="_blank">download</a> the code instead.)</p>
<h3>Use the modified Bootstrap</h3>
<p>Base your application&#8217;s <code>Bootstrap</code> class on <code>Dlu_Wal_Bootstrap</code>, turn on logging (line 7) and you are done:</p>
<pre class="brush: php; highlight: [1,7]; title: ; notranslate">
class Bootstrap extends Dlu_Wal_Bootstrap
{
    /**
     * Turns Bootstrap Debug logging on/off
     * @var boolean
     */
    protected $_logging		= true;

    //Define your resource methods
    //...
}
</pre>
<p>Now it is much easier to see the dependencies between the resources:</p>
<pre>#001  resourcea
#002     resourceb
#003  resourcec
#004     resourcee
#005        resourced
#006           resourcec

Fatal error: Uncaught exception 'Zend_Application_Bootstrap_Exception' with message 'Circular resource dependency detected' in</pre>
<p>The notation is pretty much self explanatory. If a resource initializes other resources, they are indented. As we can see the resource C needs the resource E which needs resource D which needs resource C again:</p>
<p><img class="alignnone size-full wp-image-287 no-border" title="Circular Dependency CED" src="http://www.zfdaily.com/wp-content/uploads/2012/03/CircularDependencyCED.png" alt="" width="284" height="184" /></p>
<p>Now you know that your problem of circular dependency is limited to resources C, E and D and you have to untangle these somehow.</p>
<h3>Breaking the vicious circle</h3>
<p>Well, putting the resources and their dependencies straight is a different story and actually a bit off-topic. Anyway here are two hints:</p>
<h5>Split the resource</h5>
<p>There is very likely a &#8216;hidden&#8217; resource in one (or more) of your resources. In other words some of your resources are trying to do too much and their responsibilities are not clearly focused. Split the resource into two and extract the hidden responsibility and you will eliminate the circular dependency. By doing this you will also improve the overall design. This is the clear and propper solution as you are tackling the root cause of your circular dependency problem. If your &#8216;circle&#8217; is formed by more than two resources though, it might be quite difficult to extract the hidden resource(s).</p>
<p>The Bootstrap is as close to the <a href="http://en.wikipedia.org/wiki/Dependency_injection" target="_blank">dependency injection</a> container as you can get in Zend Framework 1. Initializing another resource from a resource method is akin to the <a href="http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/" target="_blank">constructor injection</a>. Spliting the resource is thus similar to the solution Miško Hevery suggests for circular dependency between <strong>objects</strong> in his blog post <a href="http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/" target="_blank">Circular Dependency in constructors and Dependency Injection</a>. Although it is based on Java you will find more details about this approach there and it is definitely worth reading.</p>
<h5>Use &#8216;setter injection&#8217;</h5>
<p>Do not initialize one of your resources completely in the relevant resource method, leaving out the dependency forming the circle. You will get a partialy initialized resource but you will be able to use it as a dependency in another resource. You will finish the initialization of the partialy initialized resource later (e.g. in a new resource method), when all of the resources in the circle have been (at least partialy) initialized.</p>
<p>But that&#8217;s cheating! We have left the circular dependency intact! You are right. We have not eliminated the circle at all but we have built some resources in two phases instead. This is similar to <a href="http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/" target="_blank">setter injection</a>. You might think this approach has a slightly fishy odour. It has. The devil is in the word &#8216;partialy&#8217;. Your resource method produced an incomplete or not functioning resource. The resource will be ok only after the second phase of its initialization. What if anyone uses the resource in the critical time space after it has been created but before it has been completely initialized? It is up to you to secure this hole and not enable usage of the partialy initialized resource. As we are talking Zend Framework 1 Bootstrap, it should not be too difficult.</p>
<h3>Back to the tool</h3>
<p>The resource logging tool is very simple. It just keeps track of the number of initialized resources and indentation level corresponding to the hierarchy structure of the resource dependencies and prints the correctly indented resource name together with its number to the screen. There is only one thing you should be aware of: The log contains only resources being actually <strong>initialized</strong> (not merely used). If resource A calls <code>bootstrap('b')</code> but the resource B has already been initialized, it will not be logged.</p>
<h5>The good and the bad</h5>
<p>This resource logging tool is very simple to implement and use. Just base your Bootstrap class on our modified base class and keep the logging switched off. If you ever get the <code>'Circular resource dependency detected'</code> error, flip the switch (the <code>$_logging</code> property) and you instantly see the troublemakers.</p>
<p>You might not like the crude screen logging. Of course there are better ways to output diagnostic information (Zend_Log and namely Zend_Log_Writer_Firebug), but remember, we are dealing with Bootstrap and we likely do not have any more sophisticated resources available yet. You might also ask about the performance impact with the logging switched off. I consider this negligible (there are certainly other way more significant bottlenecks in the app) and do not bother to change the Bootstrap base class for production.</p>
<h3>Summary</h3>
<p>We have built a quick and dirty logging tool which helps in discovering dependencies between Bootstrap resources. We have also suggested possible solutions to the circular dependencies in Bootstrap.</p>
<p>The code mentioned in this article forms a part of my Web App Library, which can be downloaded complete with a Bootstrap example here: <a href="https://bitbucket.org/dlu/web-app-library/downloads" target="_blank">https://bitbucket.org/dlu/web-app-library/downloads</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zfdaily.com/2012/03/fighting-circular-dependencies-in-bootstrap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
