<?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; Zend_Controller</title>
	<atom:link href="http://zf.gm-ram.com/topics/zend_controller/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>The Login Controller</title>
		<link>http://zf.gm-ram.com/posts/the-login-controller/</link>
		<comments>http://zf.gm-ram.com/posts/the-login-controller/#comments</comments>
		<pubDate>Thu, 28 Oct 2010 19:30:04 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Controller]]></category>
		<category><![CDATA[Zend_Form]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=765</guid>
		<description><![CDATA[Following on from my overview of authentication and access control, I intend to first look at the implementation of a Login controller. This will handle two basic request: /login and /login/logout. These correspond to a user&#8217;s attempt to login via the login form, and a logout request, typically via a page link. The skeleton code [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from <a href="http://zf.gm-ram.com/posts/implementing-authentication-and-access-control-using-the-zend-framework/">my overview of authentication and access control</a>, I intend to first look at the implementation of a Login controller. This will handle two basic request: /login and /login/logout. These correspond to a user&#8217;s attempt to login via the login form, and a logout request, typically via a page link.<span id="more-765"></span></p>
<p>The skeleton code 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> LoginController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<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;">// TODO: Handle login attempt</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> logoutAction<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;">// TODO: Handle logout</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let us look at the index action function, which is concerned with rendering the login form and handling login attempts made using it.</p>
<p>The function starts by creating a login form object:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Create the login form object</span>
    <span style="color: #000088;">$loginForm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Form_Login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The implementation of this class will be discussed in a subsequent post.</p>
<p>The function now needs to check whether the form has been POSTed back to the application:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Check whether the user has submitted the login form</span>
    <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</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;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// TODO: Handle form being submitted</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>If he has been submitted, the application needs to perform some basic validation, to check that the user has entered a username and password; if this validation fails, the cause is passed to the view script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #666666; font-style: italic;">// Validate form</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loginForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// TODO: Perform authentication of username and password</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$loginForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFirstFormError</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'message'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorField</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$error</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'field'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>If the submitted data passes this basic validation, the application needs to authenticate the username and password. If this is successful, the user is redirected to an appropriate page; otherwise, an error message is passed to the view script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">        <span style="color: #666666; font-style: italic;">// Get submitted values</span>
        <span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$loginForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValues</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;">// Attempt login</span>
        <span style="color: #000088;">$dbAdapter</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;">-&gt;</span><span style="color: #004000;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db'</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;">$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;">-&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>
        <span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_Login<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbAdapter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$log</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;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">login</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</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;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Invalid username or password'</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorField</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that the authentication code is encapsulated inside a class (Application_Model_Login) which will be discussed in a subsequent post.</p>
<p>Also note that the application assumes that both the Db and Log resource plugins have been properly initialised by the bootstrapped (e.g. via the configuration file). See <a href="http://zf.gm-ram.com/posts/bootstrapping-the-application-the-standard-database-resource-plugin/">here</a> and <a href="http://zf.gm-ram.com/posts/creating-a-datestamped-log-file-using-the-standard-log-resource-plugin/">here</a> for further details of these two resource plugins.</p>
<p>Finally, the form object needs to be passed to the view script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$loginForm</span><span style="color: #339933;">;</span></pre></div></div>

<p>The view script will be looked at in the next post.</p>
<p>The implementation of the logout action is simpler:</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> logoutAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Application_Model_Login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">logout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<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: #009900;">&#125;</span></pre></div></div>

<p>Again the Application_Model_Login class is used, this time to clear the user&#8217;s status as logged in. Then the user is redirected to an appropriate page.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-login-controller/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Implementing Authentication and Access Control using the Zend Framework</title>
		<link>http://zf.gm-ram.com/posts/implementing-authentication-and-access-control-using-the-zend-framework/</link>
		<comments>http://zf.gm-ram.com/posts/implementing-authentication-and-access-control-using-the-zend-framework/#comments</comments>
		<pubDate>Sat, 23 Oct 2010 18:31:22 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Acl]]></category>
		<category><![CDATA[Zend_Auth]]></category>
		<category><![CDATA[Zend_Controller]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=759</guid>
		<description><![CDATA[One of the most common, but often complex to implement, features of websites is the facility for users to log in and out of the site, thus allowing them to view content and perform actions that would otherwise be denied to them. The Zend Framework does not provide a single all-in-one component, since the possible [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most common, but often complex to implement, features of websites is the facility for users to log in and out of the site, thus allowing them to view content and perform actions that would otherwise be denied to them. The Zend Framework does not provide a single all-in-one component, since the possible approaches are so diverse, but rather provides separate components for authentication (Zend_Auth) and access control (Zend_Acl), and a mechanism for incorporating them into the dispatch process (controller plugins).<span id="more-759"></span></p>
<p>In subsequent posts, I shall look at the various components that need to be created to create a fairly basic login system, and how to combine these components together. These components are roughly as follows:</p>
<ul>
<li><strong>Login controller.</strong> This handles the login and logout actions by these user.</li>
<li><strong>Login form.</strong> This allows the user to enter a username and password.</li>
<li><strong>Authentication class.</strong> This handles the process of logging in or out.</li>
<li><strong>Identity class.</strong> This handles the current login status of the user, and details of the user&#8217;s identity.</li>
<li><strong>Access Control List class.</strong> This specifies what the user can or cannot see and do.</li>
<li><strong>Access Control plugin.</strong> This checks whether the user is allowed to view a particular page, and redirects to the login page if necessary.</li>
</ul>
<p>The posts won&#8217;t be concerned with the implementation of the pages on the site, other than the Login page. However, it will make reference to what those pages might contain or do, if this is relevant to the login process.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/implementing-authentication-and-access-control-using-the-zend-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding an automatically-generated sitemap.xml to a Zend Framework site</title>
		<link>http://zf.gm-ram.com/posts/adding-an-automatically-generated-sitemap-xml-to-a-zend-framework-site/</link>
		<comments>http://zf.gm-ram.com/posts/adding-an-automatically-generated-sitemap-xml-to-a-zend-framework-site/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 17:08:32 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Controller]]></category>
		<category><![CDATA[Zend_Navigation]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=695</guid>
		<description><![CDATA[This article looks at how to dynamically create a Google sitemap.xml file for your Zend Framework site. It assumes that you are using Zend_Navigation to describe the basic layout of your site. It also assumes that the application is based on Zend_Application and uses Zend_Layout. First, we need to create the controller class used to [...]]]></description>
			<content:encoded><![CDATA[<p>This article looks at how to dynamically create a Google <em>sitemap.xml</em> file for your Zend Framework site. It assumes that you are using Zend_Navigation to describe the basic layout of your site. It also assumes that the application is based on Zend_Application and uses Zend_Layout.<span id="more-695"></span></p>
<p>First, we need to create the controller class used to field the request for the sitemap. This is called <strong>SitemapController</strong> and is located in <em>controllers/SitemapController.php</em>. The class has the following 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> SitemapController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// TODO: Implement the class action handlers here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first action is the <strong>index</strong> action, which is handled by the follow method of the class:</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> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableLayout</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>This disable the standard layout, so that only the XML of the sitemap is rendered.</p>
<p>The accompanying view script file is <em>application/views/scripts/sitemap/index.phtml</em> and 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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">navigation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sitemap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFormatOutput</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">navigation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sitemap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This takes the pages in the navigator and uses them to generate a sitemap.</p>
<p>Now, if you navigate to <strong>http://<em>[your-domain]</em>/sitemap</strong>, the sitemap will be rendered in the browser.</p>
<p>Now we need to set up the router, so that a request for <strong>http://<em>[your-domain]</em>/sitemap.xml</strong> also brings up the sitemap.</p>
<p>To do this, we should add the route to the application configuration file:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">resources.router.routes.sitemap.type <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;Zend_Controller_Router_Route_Static&quot;</span>
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>
resources.router.routes.sitemap.defaults.action <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;redirect&quot;</span></pre></div></div>

<p>We now need to add another action handler to the SitemapController class:</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> redirectAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/sitemap'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The reason for doing this indirect redirection is that if we route directly to <strong>sitemap/index</strong>, the navigator becomes confused and renders all the URLs in the sitemap incorrectly. You can confirm this by changing the ini file setting above from &#8220;redirect&#8221; to &#8220;index&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/adding-an-automatically-generated-sitemap-xml-to-a-zend-framework-site/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Pagination using Zend_Paginator</title>
		<link>http://zf.gm-ram.com/posts/pagination-using-zend_paginator/</link>
		<comments>http://zf.gm-ram.com/posts/pagination-using-zend_paginator/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 12:54:51 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Zend_Controller]]></category>
		<category><![CDATA[Zend_Db]]></category>
		<category><![CDATA[Zend_Paginator]]></category>
		<category><![CDATA[Zend_View]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=652</guid>
		<description><![CDATA[Any web application that retrieves large amounts of data for display is bound to have the need to split that data over several pages. Developing the code to do this from scratch is not trivial, so it is extremely useful that the Zend Framework has the Zend_Paginator module, which assists with this task. Zend_Paginator works [...]]]></description>
			<content:encoded><![CDATA[<p>Any web application that retrieves large amounts of data for display is bound to have the need to split that data over several pages. Developing the code to do this from scratch is not trivial, so it is extremely useful that the Zend Framework has the Zend_Paginator module, which assists with this task.<span id="more-652"></span></p>
<p>Zend_Paginator works as follows:</p>
<ol>
<li>The paginator is passed either an actual set of data, or a database query that can be used to retrieve the data.</li>
<li>The paginator will by default retrieve the first page and 10 items on that page; the page and the items per page can be set at this point if necessary.</li>
<li>The paginator can be used to render a navigator, by retrieving the current page and the number of pages.</li>
<li>The paginator can be used to render the current page, by iterating over the paginator to retrieve each item in order.</li>
</ol>
<p>I shall give a concrete example of how this can be implemented below, based on an application I am currently developing.</p>
<p>First, let us start with the controller. In this instance, the controller is <strong>user</strong> and the action is <strong>index</strong>. The skeleton for this code 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> UserController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<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;">// Insert implementation here</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The current page will be passed via the URL as a parameter. The first task in the action handler is therefore to retrieve the page number. If it is not specified, it will default to 1.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Get the current page</span>
    <span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The next step is to construct a Zend_Db_Select object that represents the database query. To do this we need a database adapter; below, we assume that this has been created during the bootstrapping process and can be retrieved via the bootstrap object. The query simply retrieves all records from the user table, ordering them by the user&#8217;s display name.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Get the boostrap object</span>
    <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>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Get the database adapter</span>
    <span style="color: #000088;">$dbAdapter</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;">'db'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Create the selection object</span>
    <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">order</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now we have the select object, we can use it to create the paginator object.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Get a Paginator object using Zend_Paginator's built-in factory</span>
    <span style="color: #000088;">$paginator</span> <span style="color: #339933;">=</span> Zend_Paginator<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can then set the number of items per page and the current page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Set the number of items to show per page</span>
    <span style="color: #000088;">$paginator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setItemCountPerPage</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Select the requested page</span>
    <span style="color: #000088;">$paginator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCurrentPageNumber</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Finally, we should pass the paginator object to the view script.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginator</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$paginator</span><span style="color: #339933;">;</span></pre></div></div>

<p>The view script used is given below. Note that the application uses Zend_Layout, so only the unique content of the page needs to be generated here.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h3&gt;User Management&lt;/h3&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Display pagination control</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginationControl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginator</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Elastic'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'paginator.phtml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th width=&quot;25%&quot;&gt;Username&lt;/th&gt;
      &lt;th width=&quot;25%&quot;&gt;Role&lt;/th&gt;
      &lt;th width=&quot;50%&quot;&gt;Name&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #666666; font-style: italic;">// Render each item for the current page in a table row</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">paginator</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;tr&gt;
      &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
      &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'role'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
      &lt;td&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;/tbody&gt;
&lt;/table&gt;</pre></div></div>

<p>The script uses the <strong>PaginationControl</strong> view helper to render the navigator. The view script for the navigator markup is specified as one of the parameters as <em>paginator.phtml</em>; this script is found in the <em>/application/views/scripts</em> directory. The following standard script is used:</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pageCount</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;div class=&quot;paginator&quot;&gt;
&nbsp;
&lt;!-- First page link --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">previous</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">first</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
    First
  &lt;/a&gt; |
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;span class=&quot;disabled&quot;&gt;First&lt;/span&gt; |
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!-- Previous page link --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">previous</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">previous</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
    &lt; Previous
  &lt;/a&gt; |
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;span class=&quot;disabled&quot;&gt;&lt; Previous&lt;/span&gt; |
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!-- Next page link --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
    Next &gt;
  &lt;/a&gt; |
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;span class=&quot;disabled&quot;&gt;Next &gt;&lt;/span&gt; |
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!-- Last page link --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
    Last
  &lt;/a&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;span class=&quot;disabled&quot;&gt;Last&lt;/span&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The section used to render the current page as a table makes use of the fact that the paginator object can be iterated over to retrieve each item on the current page in turn.</p>
<p>The following styling is applied to the navigator:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.paginator</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The following styling is applied to the table:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">table <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
th<span style="color: #00AA00;">,</span> td <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
th <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#4E7DD1</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFFFFF</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/pagination-using-zend_paginator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Blogs Page: The feed/digest action handler</title>
		<link>http://zf.gm-ram.com/posts/the-blogs-page-the-feeddigest-action-handler/</link>
		<comments>http://zf.gm-ram.com/posts/the-blogs-page-the-feeddigest-action-handler/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 06:18:23 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Controller]]></category>
		<category><![CDATA[Zend_Db]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=594</guid>
		<description><![CDATA[The feed controller digest action handler is used to render a list of the five most recent blog posts on the specified blog, as exposed by the blog&#8217;s RSS feed. The feed controller is implemented as follows: class FeedController extends Zend_Controller_Action &#123; public function digestAction&#40;&#41; &#123; try &#123; $this-&#62;_helper-&#62;layout-&#62;disableLayout&#40;&#41;; &#160; $id = $this-&#62;getRequest&#40;&#41;-&#62;getParam&#40;'id'&#41;; &#160; if [...]]]></description>
			<content:encoded><![CDATA[<p>The feed controller digest action handler is used to render a list of the five most recent blog posts on the specified blog, as exposed by the blog&#8217;s RSS feed.<span id="more-594"></span></p>
<p>The feed controller is implemented 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> FeedController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> digestAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    try
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$dbBlog</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Default_Model_Db_Blog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$blog</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbBlog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchBlog</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$blog</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blog</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$blog</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Blog ID not found in database'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Blog ID not specified'</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</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 function implementation is wrapped in an exception handler, mainly to catch any database errors. This is the basic use case:</p>
<ol>
<li>As it is not going to render an entire page, but rather respond to an AJAX request, it calls the layout helper to disable layout rendering.</li>
<li>It checks the value of the <strong>id</strong> parameter, which should have been passed via the URL.</li>
<li>If the <strong>id</strong> is set, it retrieves the database record in the blog table for that <strong>id</strong>.</li>
<li>If a blog record is returned, it passes that to the view script.</li>
</ol>
<p>There are three deviations from this:</p>
<ol>
<li>If the id is not set, it reports an error (&#8216;Blog ID not specified&#8217;).</li>
<li>If no database record is returned for the specified id, it reports an error (&#8216;Blog ID not found in database&#8217;).</li>
<li>If an exception is throw, it reports an error; the exact message will depend on the exception.</li>
</ol>
<p>The implementation of Default_Model_Db_Blog has been covered in previous posts (see <a href="/posts/the-blogs-page-the-data-classes/">the post on the data classes</a> and <a href="/posts/the-blogs-page-the-mapper-classes/">the post on the mapper classes</a>).</p>
<p>How the blog record and any error messages are handled in the associated view script is the subject of the next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-blogs-page-the-feeddigest-action-handler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Blogs Page: The index/blogs action handler</title>
		<link>http://zf.gm-ram.com/posts/the-blogs-page-the-indexblogs-action-handler/</link>
		<comments>http://zf.gm-ram.com/posts/the-blogs-page-the-indexblogs-action-handler/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 23:47:48 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Controller]]></category>
		<category><![CDATA[Zend_Db]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=504</guid>
		<description><![CDATA[The blogs page is handled by the blogs action of the index controller. This fetches information about the blogs from a database table; it then passes this information to the view script. The action handler is implemented by the following code: public function blogsAction&#40;&#41; &#123; try &#123; $dbBlog = new Default_Model_Db_Blog&#40;&#41;; $this-&#62;view-&#62;blogs = $dbBlog-&#62;fetchBlogs&#40;&#41;; &#125; [...]]]></description>
			<content:encoded><![CDATA[<p>The blogs page is handled by the <strong>blogs</strong> action of the <strong>index</strong> controller. This fetches information about the blogs from a database table; it then passes this information to the view script.<span id="more-504"></span></p>
<p>The action handler is implemented by 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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> blogsAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  try
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$dbBlog</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Default_Model_Db_Blog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blogs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbBlog</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchBlogs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</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>There are five classes used to handle access to the database:</p>
<ul>
<li>Default_Model_Db_Blog</li>
<li>Default_Model_Db_Abstract</li>
<li>Default_Model_Db_Mapper_Blog</li>
<li>Default_Model_Db_Mapper_Abstract</li>
<li>Default_Model_Db_Table_Blog</li>
</ul>
<p>Let us consider Default_Model_Db_Table_Blog first. This class extends Zend_Db_Table_Abstract. The implementation of the class is very simple:</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_Model_Db_Table_Blog <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Abstract
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'blog'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Setting the member variable <strong>$_name</strong> to the name of a table in the database binds the class to that table, and allows it to be used to perform operations against the table. Note that the database connection itself has been set up during the bootstrapping process, as described in the post <a href="/posts/bootstrapping-the-application-the-standard-database-resource-plugin/">Bootstrapping the Application: the Standard Database Resource Plugin</a>.</p>
<p>The data in the rows of the blog table is handled by the Default_Model_Db_Blog class; this is linked to the blog table class by a mapper class called Default_Model_Db_Mapper_Blog. Each class contains the code specific to the blog table; they are both derived from an abstract base class which contains the generic code for these type of classes. This allows the easy addition of classes handling other database tables.</p>
<p>In the next couple of posts, we shall look at the implementation of first the data classes, then the mapper classes.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-blogs-page-the-indexblogs-action-handler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Blogs Page: Overview</title>
		<link>http://zf.gm-ram.com/posts/the-blogs-page-overview/</link>
		<comments>http://zf.gm-ram.com/posts/the-blogs-page-overview/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 19:03:11 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Controller]]></category>
		<category><![CDATA[Zend_View]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=499</guid>
		<description><![CDATA[The blogs page on the GM-RAM website is used to display details of the company-sponsored blogs, including a digest of the five most recent posts. Like the other pages on the site, it displays its content within a series of tabs. The first tab simply contains some general comments about the blogs; each blog then [...]]]></description>
			<content:encoded><![CDATA[<p>The blogs page on the GM-RAM website is used to display details of the company-sponsored blogs, including a digest of the five most recent posts. Like the other pages on the site, it displays its content within a series of tabs. The first tab simply contains some general comments about the blogs; each blog then has its own tab.<span id="more-499"></span></p>
<p>In this next series of posts, I shall look at how the page is implemented. The blog digest is not fetched before the bulk of the page is rendered, as there may be a noticeable delay before it is fetched and therefore before the visitor receives a response. Instead, the page renders each blog tab with a placeholder section, into which the digest feed is loaded using AJAX techniques.</p>
<p>The posts will deal with the following aspects of the implementation:</p>
<ol>
<li>The index/blogs action handler</li>
<li>The index/blogs view script</li>
<li>The AJAX feed digest helper</li>
<li>The feed/digest action handler</li>
<li>The feed/digest view script</li>
<li>The feed digest helper</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-blogs-page-overview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Contact Page: The Action Handler</title>
		<link>http://zf.gm-ram.com/posts/the-contact-page-the-action-handler/</link>
		<comments>http://zf.gm-ram.com/posts/the-contact-page-the-action-handler/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 21:35:44 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Zend_Controller]]></category>
		<category><![CDATA[Zend_Filter]]></category>
		<category><![CDATA[Zend_Form]]></category>
		<category><![CDATA[Zend_Layout]]></category>

		<guid isPermaLink="false">http://zf.gm-ram.com/?p=436</guid>
		<description><![CDATA[The visual implementation of the contact page is similar to that of the home page, as described in the post The Home Page. However, it also makes use of Zend_Form and Zend_Validate to render and process the contact form, and Zend_Layout, Zend_Filter and Zend_Mail to render and send the contact e-mail to the site admin. [...]]]></description>
			<content:encoded><![CDATA[<p>The visual implementation of the contact page is similar to that of the home page, as described in the post <a href="/posts/the-home-page/">The Home Page</a>. However, it also makes use of Zend_Form and Zend_Validate to render and process the contact form, and Zend_Layout, Zend_Filter and Zend_Mail to render and send the contact e-mail to the site admin.<span id="more-436"></span></p>
<p>As the implementation is complex and is spread over a number of files and classes, I shall be looking at it over the course of several posts. In this post I shall be concentrating on the action handler.</p>
<p>The action handler starts by retrieving the request object:</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> contactAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will be used to determine whether the user has submitted the contact form and what data was submitted.</p>
<p>Then the function creates the form object:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Default_Form_Contact<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We shall look at the implementation of this class in a later post.</p>
<p>Then the function determines whether the user has posted the form:</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: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span></pre></div></div>

<p>If the user has posted the form, the function retrieves the data posted and validates it against the form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</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;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span></pre></div></div>

<p>If the data is valid, the function then creates and sends the e-mail.</p>
<p>The first step is to create a Zend_Layout object, then set the layout script and its location:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      try
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$layout</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Layout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLayoutPath</span><span style="color: #009900;">&#40;</span>APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/layouts/scripts&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLayout</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'contact-email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The Zend_Layout will now look for a file called <em>contact-email.phtml</em> in the <em>application/layouts/scripts</em> directory.</p>
<p>The next step is to create a filter and use it to eliminate any possible HTML injection, before passing the parameters to the layout script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">        <span style="color: #000088;">$htmlEntities</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Filter_HtmlEntities<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$htmlEntities</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$htmlEntities</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">message</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$htmlEntities</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'message'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then the layout is rendered and stored in a variable:</p>

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

<p>The sending of the mail is handled by the Default_Model_Mail_Contact class, which will be examined in a later post. The function creates an object of that class and call its send method, passing the HTML generated above as the parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">        <span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Default_Model_Mail_Contact<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bodyHtml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then a variable is passed to the view script to flag that the mail has been sent:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sent</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Any exceptions thrown during the process of sending the mail are handled in the following catch block:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</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>If the form validation fails, the error is passed in a variable to the view script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFirstFormError</span><span style="color: #009900;">&#40;</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>Finally, the form object is always passed to the view script, to allow it to be rendered if necessary.</p>

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

<p>The layout script for the e-mail is quite simple:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;div&gt;
  &lt;div&gt;&lt;strong&gt;Name:&lt;/strong&gt; 
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
  &lt;div&gt;
    &lt;strong&gt;E-mail:&lt;/strong&gt; 
    &lt;a href=&quot;mailto:<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;
  &lt;/div&gt;
  &lt;div&gt;&lt;strong&gt;Message:&lt;/strong&gt; 
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">message</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p>The fields submitted by the user are injected into the appropriate places in the e-mail.</p>
<p>In the next post, I shall look at the view script for the contact page.</p>
]]></content:encoded>
			<wfw:commentRss>http://zf.gm-ram.com/posts/the-contact-page-the-action-handler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

