<?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>The Zend Framework in Practice &#187; Bootstrapping</title>
	<atom:link href="http://zf.gm-ram.com/topics/bootstrapping/feed/" rel="self" type="application/rss+xml" />
	<link>http://zf.gm-ram.com</link>
	<description>Developing Web Applications with the Zend Framework</description>
	<lastBuildDate>Mon, 02 Jan 2012 10:20:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Simple Access Control</title>
		<link>http://zf.gm-ram.com/posts/simple-access-control/</link>
		<comments>http://zf.gm-ram.com/posts/simple-access-control/#comments</comments>
		<pubDate>Fri, 28 Jan 2011 18:26:39 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Auth]]></category>
		<category><![CDATA[Zend_Controller]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=896</guid>
		<description><![CDATA[In my previous post, I looked at using a controller plugin as part of a more complex access control system based on an access control list. However, sometimes a simpler solution makes sense. For instance, in a recent project, I just needed to restrict access to the admin section of the site. The admin section [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://zf.gm-ram.com/posts/the-access-control-plugin/">my previous post</a>, I looked at using a controller plugin as part of a more complex access control system based on an access control list. However, sometimes a simpler solution makes sense. For instance, in a recent project, I just needed to restrict access to the admin section of the site. The admin section was contained within the admin module. I therefore need to check only two things:</p>
<ol>
<li>Was the user trying to access something in the admin section?</li>
<li>Was the user logged in as admin?</li>
</ol>
<p>I still needed to add the mechanism for logging in, but there was no need for an access control list, and the implementation of the Access Control Plugin was much simpler.<span id="more-896"></span></p>
<p>As before, the plugin needs to be registered in the configuration file:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.frontController.plugins.accessControl <span style="color: #000066; font-weight:bold;">=</span> 
  <span style="color: #933;">&quot;Application_Plugin_AccessControl&quot;</span></pre></div></div>

<p>The implementation of the plugin is much simpler, however:</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: #000000; font-weight: bold;">class</span> Application_Plugin_AccessControl <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Plugin_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preDispatch<span style="color: #009900;">&#40;</span>Zend_Controller_Request_Abstract <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Get the current user role</span>
    <span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> Application_Model_Identity_Current<span style="color: #339933;">::</span><span style="color: #004000;">getRole</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;">// Get the request module</span>
    <span style="color: #000088;">$module</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleName</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;">// Determine whether the user is allowed to access the module</span>
    <span style="color: #000088;">$accessDenied</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'admin'</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$role</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'admin'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$accessDenied</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// If access is denied, redirect to the login page</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$accessDenied</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setControllerName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setActionName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The workings of the class should be self-evident from the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/simple-access-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Access Control Plugin</title>
		<link>http://zf.gm-ram.com/posts/the-access-control-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/the-access-control-plugin/#comments</comments>
		<pubDate>Wed, 08 Dec 2010 19:28:34 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Acl]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Auth]]></category>
		<category><![CDATA[Zend_Controller]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=877</guid>
		<description><![CDATA[This post deals with the last piece of the access control/authentication jigsaw: the Access Control plugin. It shows how an application can makes use of the Zend Framework&#8217;s controller plugin mechanism to apply the access control list to a user request. Let us start with the plugin itself. It is called Application_Plugin_AccessControl and located in [...]]]></description>
			<content:encoded><![CDATA[<p>This post deals with the last piece of the access control/authentication jigsaw: the Access Control plugin. It shows how an application can makes use of the Zend Framework&#8217;s controller plugin mechanism to apply the access control list to a user request.<span id="more-877"></span></p>
<p>Let us start with the plugin itself. It is called Application_Plugin_AccessControl and located in the <em>application/plugins</em> directory. To locate the class definition, the autoloader strips the standard prefix <strong>Application_</strong>, which is declared in the configuration file, from the class name, then maps the <strong>Plugin_</strong> part to that directory. The plugin itself is declared in the configuration file as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.frontController.plugins.accessControl <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Application_Plugin_AccessControl&quot;</span></pre></div></div>

<p>This ensures that the plugin is initialised and used during the despatch process.</p>
<p>The plugin itself is derived from Zend_Controller_Plugin_Abstract and implements the standard preDispatch method:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Application_Plugin_AccessControl <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Plugin_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> preDispatch<span style="color: #009900;">&#40;</span>Zend_Controller_Request_Abstract <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// TODO: Apply access control</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>By implementing this method, the plugin will be called via it before any request is despatched to the relevant controller. This allows access control to be applied by redirecting a request, if the user is after something that it out of bounds.</p>
<p>The process of applying access control involves a series of steps. These are given below.</p>
<p>Step 1: Get the authentication object. This is used to determine whether the user is logged in, and if so, then as whom.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> Zend_Auth<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;">;</span>
    <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setStorage</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Storage_Session<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>Step 2: Get the access control list. This is done by using the static getAcl method of the Application_Model_Acl class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$acl</span> <span style="color: #339933;">=</span> Application_Model_Acl<span style="color: #339933;">::</span><span style="color: #004000;">getAcl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Step 3: Get the current user role and determine whether the user is logged in.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> Application_Model_Identity_Current<span style="color: #339933;">::</span><span style="color: #004000;">getRole</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$loggedIn</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasRole</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$role</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$role</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'guest'</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$loggedIn</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></pre></div></div>

<p>Step 4: Use the module name and controller name as the resource against which to check. Note that the module is added as a prefix, if it is not the default module.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$module</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getControllerName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$module</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'default'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$module</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$controller</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span>
      <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$module</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">':'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$controller</span><span style="color: #339933;">;</span></pre></div></div>

<p>Step 5: Use the action name as the privilege. The privilege is a sub-division of a resource, to allow finer-grained control of access. In our case, we use it to distinguish between different actions under the same controller.</p>

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

<p>Step 6: If the resource is not in the ACL, use the default resource and privilege.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">has</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$resource</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$privilege</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Step 7: Check whether the user is allowed access to the specified resource. If not, display the home page instead if the user is logged in, otherwise display the login page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$acl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isAllowed</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$role</span><span style="color: #339933;">,</span> <span style="color: #000088;">$resource</span><span style="color: #339933;">,</span> <span style="color: #000088;">$privilege</span><span style="color: #009900;">&#41;</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;">$loggedIn</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setControllerName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setActionName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</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;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setModuleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'default'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setControllerName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setActionName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</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>

]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-access-control-plugin/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A custom resource plugin for populating the registry</title>
		<link>http://zf.gm-ram.com/posts/a-custom-resource-plugin-for-populating-the-registry/</link>
		<comments>http://zf.gm-ram.com/posts/a-custom-resource-plugin-for-populating-the-registry/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 15:21:19 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Registry]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=686</guid>
		<description><![CDATA[This post looks at a simple custom resource plugin that I&#8217;ve recently created for populating the registry with values from the application configuration file. I have already examined adding a Log plugin and a View plugin in previous posts; the basic technique for adding this one is the same. First we need a line like [...]]]></description>
			<content:encoded><![CDATA[<p>This post looks at a simple custom resource plugin that I&#8217;ve recently created for populating the registry with values from the application configuration file. I have already examined adding a <a href="http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-log-resource-plugin/">Log plugin</a> and a <a href="http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-view-resource-plugin/">View plugin</a> in previous posts; the basic technique for adding this one is the same.<span id="more-686"></span></p>
<p>First we need a line like this in the configuration file to allow the bootstrapper to find our plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">pluginPaths.Application_Resource <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> APPLICATION_PATH </span><span style="color: #933;">&quot;/resources&quot;</span></pre></div></div>

<p>The key <strong>Application_Resource</strong> indicates the prefix given to the class that implements the plugin, minus its trailing underscore. The value indicates the location in which the plugins with this prefix are stored.</p>
<p>The class itself is implemented in <em>Registry.php</em> in the <em>resources</em> directory. It contains the following code:</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: #000000; font-weight: bold;">class</span> Application_Resource_Registry 
  <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Resource_ResourceAbstract
<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;">// Get the registry object</span>
    <span style="color: #000088;">$registry</span> <span style="color: #339933;">=</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;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Get the options that should be placed in the registry</span>
    <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOptions</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;">// Add the options to the registry</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$registry</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$registry</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The code should be fairly self-explanatory. The options are key/value pairs retrieved from the configuration file, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.registry.phone <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;phone number&quot;</span></pre></div></div>

<p>In the above case, the value <strong>&#8220;phone number&#8221;</strong> is stored under the index <strong>phone</strong> in the registry.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/a-custom-resource-plugin-for-populating-the-registry/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating a Datestamped Log File using the Standard Log Resource Plugin</title>
		<link>http://zf.gm-ram.com/posts/creating-a-datestamped-log-file-using-the-standard-log-resource-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/creating-a-datestamped-log-file-using-the-standard-log-resource-plugin/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 22:40:43 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Log]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=600</guid>
		<description><![CDATA[In a previous post, I looked at creating a custom resource plugin to create a logger. Since I wrote that code, a standard resource plugin to perform this function has been created. At first sight the standard plugin is inferior, in that the custom plugin allows for the insertion of a date into the log [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="/posts/bootstrapping-the-application-a-custom-log-resource-plugin/">previous post</a>, I looked at creating a custom resource plugin to create a logger. Since I wrote that code, a standard resource plugin to perform this function has been created. At first sight the standard plugin is inferior, in that the custom plugin allows for the insertion of a date into the log filename, which allows a new log to be created every day, while the standard plugin requires a fixed filename specified in the configuration file. However, I have worked out a simple mechanism for adding a date stamp to the filename, which I shall share in this article.<span id="more-600"></span></p>
<p>The key things is that it is possible to use constants in the configuration file. The first step is therefore to define a constant to represent the current date stamp in <em>htdocs/index.php</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Define current date</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DATESTAMP'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This should come after the definitions of APPLICATION_PATH and APPLICATION_ENV (see <a href="/posts/bootstrapping-the-application-the-index-php-file/">the earlier post on index.php</a>).</p>
<p>This constant can now be used in the configuration file (<em>application/configs/application.ini</em>) to declare the log filename, along with the other necessary settings:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.log.stream.writerName <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Stream&quot;</span>
resources.log.stream.writerParams.stream <span style="color: #000066; font-weight:bold;">=</span> 
  APPLICATION_PATH <span style="color: #933;">&quot;/logs/log_&quot;</span> DATESTAMP
resources.log.stream.writerParams.mode <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;a&quot;</span></pre></div></div>

<p>To use the logger, you can fetch the object created like this from within the controller:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bootstrap</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getInvokeArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bootstrap'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bootstrap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'log'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then use the logger in the normal fashion, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$log</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">err</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Page not found'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/creating-a-datestamped-log-file-using-the-standard-log-resource-plugin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Bootstrapping the Application: the Bootstrap Class</title>
		<link>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-bootstrap-class/</link>
		<comments>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-bootstrap-class/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 17:31:43 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=211</guid>
		<description><![CDATA[In addition to initialising application components via resource plugins. it is also possible to do so via the bootstrap class itself. The bootstrap class location and name is specified in the configuration file; see Bootstrapping the Application: Configuration File Overview. The basic skeleton of the class is as follows: class Bootstrap extends Zend_Application_Bootstrap_Bootstrap &#123; // [...]]]></description>
			<content:encoded><![CDATA[<p>In addition to initialising application components via resource plugins. it is also possible to do so via the bootstrap class itself.<span id="more-211"></span></p>
<p>The bootstrap class location and name is specified in the configuration file; see <a href="/posts/bootstrapping-the-application-configuration-file-overview/"><strong>Bootstrapping the Application: Configuration File Overview</strong></a>.</p>
<p>The basic skeleton of the class is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Bootstrap <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Bootstrap_Bootstrap
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Add the initialisation methods here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that Zend_Application_Bootstrap_Bootstrap is the base class; this adds the basic functionality, which essentially involves registering and initialising the front controller plugin.</p>
<p>The class can contain methods that initialise resources programmatically (as opposed to via the configuration file). These methods all have the prefix <strong>_init</strong> and return a reference to the resource that is initialised. The sample application contains one such method:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _initAutoload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$autoloader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Application_Module_Autoloader<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'namespace'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Default_'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'basePath'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</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: #b1b100;">return</span> <span style="color: #000088;">$autoloader</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This sets up the autoloader to look for classes with the prefix <strong>Default</strong> and located in the <em>application</em> directory. In addition, the autoloader class automatically declares a number of additional directories and associated prefixes:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Prefix: Model_DbTable    Directory: models/DbTable
Prefix: Form             Directory: forms
Prefix: Model            Directory: models
Prefix: Plugin           Directory: plugins
Prefix: Service          Directory: services
Prefix: View_Helper      Directory: views/helpers
Prefix: View_Filter      Directory: views/filters</pre></div></div>

<p>These prefixes and directories are applied to the base prefix and directory, and use the standard Zend Framework autoloader conventions. For instance, the class <strong>Default_Model_Db_Blog</strong> uses the prefix <strong>Default</strong> followed by the prefix <strong>Model</strong>, and is therefore located in the <em>application/models/Db directory</em> in a file called <em>Blog.php</em>; this is because the root directory, associated with <strong>Default</strong>, is <em>application</em>, the directory associated with <strong>Model</strong> is <em>models</em>, and the remaining part of the name (<strong>Db_Blog</strong>) decomposes to <em>Db/Blog.php</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-bootstrap-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bootstrapping the Application: a Custom View Resource Plugin</title>
		<link>http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-view-resource-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-view-resource-plugin/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 23:02:18 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_View]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=199</guid>
		<description><![CDATA[As well as writing a completely new resource plugin to use with Zend_Application, it is also possible to write a plugin that replaces one of the standard ones. The reason for doing this is that the existing plugins sometimes don&#8217;t do all the things that you want them to. Zend_Application_Resource_View is a common plugin to [...]]]></description>
			<content:encoded><![CDATA[<p>As well as writing a completely new resource plugin to use with Zend_Application, it is also possible to write a plugin that replaces one of the standard ones. The reason for doing this is that the existing plugins sometimes don&#8217;t do all the things that you want them to.<span id="more-199"></span></p>
<p><strong>Zend_Application_Resource_View</strong> is a common plugin to replace. This is because it doesn&#8217;t allow you to set the DOCTYPE. The sample application contains a replacement for this plugin, which sets this, among other things.</p>
<p><strong>Default_Resource_View</strong> has the following basic skeleton:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Default_Resource_View
  <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Resource_ResourceAbstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Add class methods and variables here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Zend_Application_Resource_ResourceAbstract is the base class, as is required.</p>
<p>A variable is declared to store the Zend_View:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_view</span><span style="color: #339933;">;</span></pre></div></div>

<p>Zend_Application will call the <strong>init</strong> method of the class to retrieve the resource it initialises. The class uses the singleton pattern to implement this.</p>
<p>First, the <strong>init</strong> method is declared; this in turn calls the <strong>getView</strong> method, which will create the Zend_View if necessary:</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> init<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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getView</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <strong>getView</strong> method starts by checking whether the Zend_View has already been created; if not, it gets the options from the configuration file and creates the view:</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> getView<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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_view<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Get the resource options</span>
    <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOptions</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;">// Create the view</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></pre></div></div>

<p>The method then checks whether the doctype option is set; if it is, it sets the view&#8217;s doctype to the specified value:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Set the doctype</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'doctype'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</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: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'doctype'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This option is set as follows in the sample application:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.view.doctype <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;XHTML1_STRICT&quot;</span></pre></div></div>

<p>The next option to check is the helpers option; this allows the application to add view helper paths to the view; these specify the possible prefixes and locations of view helper classes:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Add the custom view helper paths</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'helpers'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'helpers'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addHelperPath</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</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>The view helpers are declared as key/value pairs in the configuration file; the prefix is the key and the location the value:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.view.helpers.Default_View_Helper <span style="color: #000066; font-weight:bold;">=</span> 
  APPLICATION_PATH <span style="color: #933;">&quot;/views/helpers&quot;</span>
resources.view.helpers.ZendX_JQuery_View_Helper <span style="color: #000066; font-weight:bold;">=</span> 
  <span style="color: #933;">&quot;ZendX/JQuery/View/Helper&quot;</span>
resources.view.helpers.Test_View_Helper <span style="color: #000066; font-weight:bold;">=</span> 
  APPLICATION_PATH <span style="color: #933;">&quot;/tests/helpers&quot;</span></pre></div></div>

<p>The first set of helpers are the generally-used helpers defined by the application; the second set are those in the Zend extras jQuery module; the third set are used exclusive to the test controller. We shall see examples of these in use in later posts.</p>
<p>The last option is placeholders. These are pieces of text that are used in more than one place in the layout and view scripts, and which may need to be changed, such as the contact phone number. They are defined in the configuration file as key/value pairs:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Add the placeholders</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'placeholders'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'placeholders'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">placeholder</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</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: #000088;">$value</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>The following placeholders are defined in the configuration file:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.view.placeholders.phoneNumber <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;+44 (0)1403 753721&quot;</span> 
resources.view.placeholders.copyrightDate <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;2009&quot;</span></pre></div></div>

<p>Finally, the Zend_View is attached to the View Renderer and a reference to it is stored in <strong>$_view</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Set the view renderer to use the view</span>
    <span style="color: #000088;">$viewRenderer</span> <span style="color: #339933;">=</span> Zend_Controller_Action_HelperBroker<span style="color: #339933;">::</span><span style="color: #004000;">getStaticHelper</span>
      <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ViewRenderer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$viewRenderer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setView</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</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>_view <span style="color: #339933;">=</span> <span style="color: #000088;">$view</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>That ends the block that is executed when a Zend_View does not yet exist. At this point, a Zend_View has been created, and the reference to this is returned from the method:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_view<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-view-resource-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bootstrapping the Application: a Custom Log Resource Plugin</title>
		<link>http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-log-resource-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-log-resource-plugin/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 08:57:27 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Log]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=182</guid>
		<description><![CDATA[So far, we have been looking at how to use several of the standard resource plugins that come with Zend_Application in order to initialise application resources. It is possible, indeed encouraged, to create your own custom resource plugins to handle the initialisation of resources that fall outside their scope. One very useful class is Zend_Log, [...]]]></description>
			<content:encoded><![CDATA[<p>So far, we have been looking at how to use several of the standard resource plugins that come with Zend_Application in order to initialise application resources. It is possible, indeed encouraged, to create your own custom resource plugins to handle the initialisation of resources that fall outside their scope.<span id="more-182"></span></p>
<p>One very useful class is Zend_Log, which provides a handy mechanism for logging application errors or user activity. I have created a custom resource plugin called <strong>Default_Resource_Log</strong>, which handles the initialisation of a Zend_Log, and storage of it in the registry for future retrieval.</p>
<p>To use custom resource plugins, you need to register a plugin prefix and associated directory. In the case of the sample application, the prefix is <strong>Default_Resource</strong> and the directory is <em>resources</em>; these are added to the configuration file like this to perform the registration:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">pluginPaths.Default_Resource <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> APPLICATION_PATH </span><span style="color: #933;">&quot;/resources&quot;</span></pre></div></div>

<p>The plugin itself is implemented in a file called <em>Log.php</em>, which is in the <em>resources</em> directory. Note that although the class is called Default_Resource_Log, the prefix is excluded when naming the file in which it is contained.</p>
<p>The basic class skeleton is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Default_Resource_Log 
  <span style="color: #000000; font-weight: bold;">extends</span> Zend_Application_Resource_ResourceAbstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Add class methods and variables here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that Zend_Application_Resource_ResourceAbstract is the base class for resource plugins.</p>
<p>It has a member variable used to store the Zend_Log:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_log</span><span style="color: #339933;">;</span></pre></div></div>

<p>Zend_Application will call the <strong>init</strong> method of the class to retrieve the resource it initialises. The class uses the singleton pattern to implement this:</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> init<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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLog</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> getLog<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: #009900; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOptions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Log<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Log_Writer_Stream<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Log'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log <span style="color: #339933;">=</span> <span style="color: #000088;">$log</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_log<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If Zend_Log has already been created, it returns the reference stored in <strong>$_log</strong>; otherwise, it creates the Zend_Log, stores a reference to it and returns that.</p>
<p>The reference to Zend_Log is also stored in the registry under the key Zend_Log; this allows it to be retrieved later by:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> Zend_Registry<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Log'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note the call to <strong>getOptions</strong>; this allows the resource plugin to retrieve any settings in the configuration file that apply to it. Here, the following line has been added to the configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.log.file <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> APPLICATION_PATH </span><span style="color: #933;">&quot;/logs/application-%d.log&quot;</span></pre></div></div>

<p>This sets the location of the log file. Note that the resource plugin will look for the string <strong>%d</strong> in the name and replace it with the current date; this leads to a different log file being created every day.</p>
<p>Note that it is necessary to add at least one line relating to the resource plugin to the configuration file in order to have it initialised; if there were no parameters, you would just add:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.log<span style="">&#91;</span><span style="">&#93;</span> <span style="color: #000066; font-weight:bold;">=</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-log-resource-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bootstrapping the Application: the Standard Router Resource Plugin</title>
		<link>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-router-resource-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-router-resource-plugin/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 13:03:49 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Controller]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=171</guid>
		<description><![CDATA[The Zend Framework uses the Zend_Controller_Router_Rewrite class to decompose requests into module/controller/action/parameters, which are then despatched to the appropriate class for handling. It is possible to attach some custom handling to the router to allow for special handling of certain requests. For instance, you may want to allow Google to retrieve your sitemap via a [...]]]></description>
			<content:encoded><![CDATA[<p>The Zend Framework uses the Zend_Controller_Router_Rewrite class to decompose requests into module/controller/action/parameters, which are then despatched to the appropriate class for handling. It is possible to attach some custom handling to the router to allow for special handling of certain requests. For instance, you may want to allow Google to retrieve your sitemap via a request for <strong>http://www.example.com/sitemap.xml</strong>. The router allows this to be converted into a request for a particular combination of module/controller/action/parameters, which can then be handled in the usual way. E.g. the above request might be treated as equivalent to <strong>http://www.example.com/default/sitemap/index</strong>.<span id="more-171"></span></p>
<p><strong>Zend_Application_Resource_Router</strong>, the standard router resource plugin, presents a simple mechanism for adding routes to the router. For each route you wish to add, you need to add a block like this to the configuration file:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.router.routes.sitemap.route <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;sitemap.xml&quot;</span>
resources.router.routes.sitemap.defaults.controller <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;sitemap&quot;</span></pre></div></div>

<p>The sub-element after <strong>routes</strong> (here <strong>sitemap</strong>) is set to a unique name for the route. There are many parameters that can be set; here we set two:</p>
<ul>
<li><strong>route</strong>: This represents the actual request, which is for <strong>sitemap.xml</strong>.</li>
<li><strong>defaults</strong>: This represents what the request is converted to; here we only specify the <strong>controller</strong>, but it is also possible to specify other things such as the module, action or parameters.</li>
</ul>
<p>Now any request for <strong>sitemap.xml</strong> will be redirected to the index action of the sitemap controller.</p>
<p>We will look at how the request is actually handled in a later post.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-router-resource-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bootstrapping the Application: the Standard Navigation Resource Plugin</title>
		<link>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-navigation-resource-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-navigation-resource-plugin/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 14:10:06 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Navigation]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=159</guid>
		<description><![CDATA[Zend_Navigation is a Zend Framework component used to represent the pages on a web site and transform this into things such as page navigation and Google sitemaps. The standard navigation resource plugin Zend_Application_Resource_Navigation can be used to configure an instance of Zend_Navigation. In the sample application, each of the site pages is added via a [...]]]></description>
			<content:encoded><![CDATA[<p>Zend_Navigation is a Zend Framework component used to represent the pages on a web site and transform this into things such as page navigation and Google sitemaps. The standard navigation resource plugin <strong>Zend_Application_Resource_Navigation</strong> can be used to configure an instance of Zend_Navigation.<span id="more-159"></span></p>
<p>In the sample application, each of the site pages is added via a block of settings in the configuration file, which take the following form:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.navigation.pages.home.type <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Zend_Navigation_Page_Mvc&quot;</span>
resources.navigation.pages.home.label <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Home&quot;</span>
resources.navigation.pages.home.id <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;home_page&quot;</span>
resources.navigation.pages.home.controller <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;index&quot;</span>
resources.navigation.pages.home.action <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;index&quot;</span></pre></div></div>

<p>The sub-element after <strong>pages</strong> (here <strong>home</strong>) is set to a unique name for the page. The purpose of all the settings for this element are as follows:</p>
<ul>
<li><strong>type</strong>: This inidicates how the page location is represented. It is either Zend_Navigation_Page_Mvc, when the module/controller/action associated with the page is specified, or Zend_Navigation_Page_Uri, when a straightforward URI for the page is given.</li>
<li><strong>label</strong>: This is the string used when rendering the page name.</li>
<li><strong>id</strong>: This is the unique ID used to identify the page.</li>
<li><strong>controller</strong>: This is the controller associated with the page (see type above).</li>
<li><strong>action</strong>: This is the action associated with the page (see type above).</li>
</ul>
<p>All the pages are of the type Zend_Navigation_Page_Mvc; the other settings are as follows:</p>
<ul>
<li><strong>home</strong>: Home, home_page, index, index</li>
<li><strong>about</strong>: About Us, about_page, index, about</li>
<li><strong>contact</strong>: Contact Us, contact_page, index, contact</li>
<li><strong>blogs</strong>: Our Blogs, blogs_page, index, blogs</li>
</ul>
<p>Zend_Application_Resource_Navigation will create a container object. It will then populate it with page objects based on the settings in the configuration file.</p>
<p>We will look at how the Zend_Navigation object is used in rendering the page navigation and sitemap in later posts.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-navigation-resource-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bootstrapping the Application: the Standard Layout Resource Plugin</title>
		<link>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-layout-resource-plugin/</link>
		<comments>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-layout-resource-plugin/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 14:07:18 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Bootstrapping]]></category>
		<category><![CDATA[Zend_Application]]></category>
		<category><![CDATA[Zend_Layout]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=145</guid>
		<description><![CDATA[The Zend Framework offers a template module called Zend_Layout. This is primarily used to render the standard parts of a web page, such as the header, sidebars and footer, and to insert the variable content into placeholders. However, it can also be used to render other things with standard layouts, such as e-mails. We shall [...]]]></description>
			<content:encoded><![CDATA[<p>The Zend Framework offers a template module called Zend_Layout. This is primarily used to render the standard parts of a web page, such as the header, sidebars and footer, and to insert the variable content into placeholders. However, it can also be used to render other things with standard layouts, such as e-mails. We shall have a look at examples of both in later posts.<span id="more-145"></span></p>
<p>To use Zend_Layout, you need to specify the location of the layout scripts, then invoke Zend_Layout::startMvc(). The standard resource plugin <strong>Zend_Application_Resource_Layout</strong> is used by Zend_Application to automate this process. To use this plugin, you should add the following line to the configuration file:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.layout.layoutPath <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> APPLICATION_PATH </span><span style="color: #933;">&quot;/layouts/scripts&quot;</span></pre></div></div>

<p>This specifies the location of the layout scripts. If your scripts are in a different location, you should change the value of layoutPath.</p>
<p>We shall look at Zend_Layout in more detail in later posts.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-layout-resource-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

