<?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>Front Controller pattern &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/front-controller-pattern/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Setting Up Zend Framework with Modules</title>
		<link>/2010/08/06/setting-up-zend-framework-with-modules/</link>
		<comments>/2010/08/06/setting-up-zend-framework-with-modules/#respond</comments>
		<pubDate>Fri, 06 Aug 2010 12:59:21 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[Front Controller pattern]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[modular zend]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Software architecture]]></category>
		<category><![CDATA[Software design patterns]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Software framework]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[zend framework modules]]></category>

		<guid isPermaLink="false">/?p=1826</guid>
		<description><![CDATA[Typical Zend App Typically you&#8217;ve one module in your Zend App &#8211; the default one. In the basic installation of the framework, you put all the controllers, models and views directly in the application folder, as described below. /application - /controllers - /IndexController.php - /models - /MyModel.php - /views - /scripts - /index/index.phtml /library - &#8230; <a href="/2010/08/06/setting-up-zend-framework-with-modules/" class="more-link">Continue reading <span class="screen-reader-text">Setting Up Zend Framework with Modules</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/01/29/escaping-strings-in-a-zend-framework-view-prevent-unclosed-tags/" rel="bookmark" title="Escaping strings in a Zend Framework view. Prevent unclosed tags!">Escaping strings in a Zend Framework view. Prevent unclosed tags! </a></li>
<li><a href="/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/" rel="bookmark" title="Zend Framework: Inject JavaScript Code in a Action/View">Zend Framework: Inject JavaScript Code in a Action/View </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Typical Zend App</h2>
<p>Typically you&#8217;ve one module in your Zend App &#8211; the default one. In the basic installation of the framework, you put all the controllers, models and views directly in the application folder, as described below.</p>
<pre lang="php">
/application
	- /controllers
		- /IndexController.php
	- /models
		- /MyModel.php
	- /views
		- /scripts
			- /index/index.phtml
/library
	- /Zend
/public_html
	- /images
	- /scripts
</pre>
<h2>Bigger Apps &#8211; More Code</h2>
<p>When the application becomes bigger and bigger the controller, models and views/scripts directories contain more and more files. That&#8217;s a bit odd, because it becomes difficult to maintain, and than the modules come in hand.</p>
<h2>Modules in a Zend App</h2>
<p>When it comes to setting up modular Zend App there are tons of articles in the web, but let me show you a simple directory layout and &#8230; sample code that sets up the framework.</p>
<pre lang="php">
/application
	- /modules
		+ /admin
			- /controllers
				- /IndexController.php
			- /views
				- /scripts
					- /index/index.phtml
		+ /default
			- /controllers
				- /IndexController.php
			- /views
				- /scripts
					- /index/index.phtml
	- /models
/library
	- /Zend
/public_html
	- /images
	- /scripts
</pre>
<h2>Source</h2>
<p>Simply add this into the bootstrap:</p>
<pre lang="php">
$frontController->addModuleDirectory(APPLICATION_PATH . '/modules');
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/01/29/escaping-strings-in-a-zend-framework-view-prevent-unclosed-tags/" rel="bookmark" title="Escaping strings in a Zend Framework view. Prevent unclosed tags!">Escaping strings in a Zend Framework view. Prevent unclosed tags! </a></li>
<li><a href="/2010/06/24/zend-framework-inject-javascript-code-in-a-actionview/" rel="bookmark" title="Zend Framework: Inject JavaScript Code in a Action/View">Zend Framework: Inject JavaScript Code in a Action/View </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/08/06/setting-up-zend-framework-with-modules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting Up Global Cache in Zend Framework</title>
		<link>/2010/07/21/setting-up-global-cache-in-zend-framework/</link>
		<comments>/2010/07/21/setting-up-global-cache-in-zend-framework/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 11:17:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Front Controller pattern]]></category>
		<category><![CDATA[Human Interest]]></category>
		<category><![CDATA[large scale web application]]></category>
		<category><![CDATA[PHP For Applications]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Software framework]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Zend Technologies]]></category>

		<guid isPermaLink="false">/?p=1830</guid>
		<description><![CDATA[In a large scale web application, especially based on Zend Framework, there are lot&#8217;s of components that support built in cache support. Such are Zend_Db, Zend_Translate, Zend_Date, etc. Also you may need cache support wherever in the app, so my advice is to setup a cache instance in the &#8220;beginning&#8221;, into the bootstrap.php or even &#8230; <a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" class="more-link">Continue reading <span class="screen-reader-text">Setting Up Global Cache in Zend Framework</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/08/09/models-in-zend-framework-initialize-all-methods-with-init/" rel="bookmark" title="Models in Zend Framework &#8211; Initialize All Methods with init()">Models in Zend Framework &#8211; Initialize All Methods with init() </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>In a large scale web application, especially based on Zend Framework, there are lot&#8217;s of components that support built in cache support. Such are <a href="http://framework.zend.com/manual/en/zend.db.html">Zend_Db</a>, <a href="http://zendframework.com/manual/en/zend.translate.html">Zend_Translate</a>, <a href="http://framework.zend.com/manual/en/zend.date.html">Zend_Date</a>, etc. Also you may need cache support wherever in the app, so my advice is to setup a cache instance in the &#8220;beginning&#8221;, into the bootstrap.php or even better &#8211; into a front controller plugin, and to store it into the Zend_Registry. Thus you&#8217;ve to change only the lifetime for specific needs:</p>
<pre lang="php">
<?php

class CacheInit extends Zend_Controller_Plugin_Abstract
{
    public function __construct()
    {
        $frontendOptions = array(
            'automatic_serialization' => true,
            'lifetime' => 60
        );

        $backendOptions  = array(
            'cache_dir' => realpath(APPLICATION_PATH . '/../cache')
        );

        $cache = Zend_Cache::factory('Core',
                                     'File',
                                     $frontendOptions,
                                     $backendOptions);

        Zend_Registry::set('cache', $cache);
    }
}
</pre>
<p>Than add it as a front controller plugin:</p>
<pre lang="php">
// cache plugin
$frontController->registerPlugin(new CacheInit());
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/08/09/models-in-zend-framework-initialize-all-methods-with-init/" rel="bookmark" title="Models in Zend Framework &#8211; Initialize All Methods with init()">Models in Zend Framework &#8211; Initialize All Methods with init() </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/21/setting-up-global-cache-in-zend-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zend Framework: Simple Acl Front Controller Plugin</title>
		<link>/2010/07/06/zend-framework-simple-acl-front-controller-plugin/</link>
		<comments>/2010/07/06/zend-framework-simple-acl-front-controller-plugin/#respond</comments>
		<pubDate>Tue, 06 Jul 2010 19:00:59 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[access control list]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Front Controller pattern]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Software design patterns]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>

		<guid isPermaLink="false">/?p=1771</guid>
		<description><![CDATA[Almost every web site need some abstraction over the access control list (ACL) to grant access of its users. As usual Zend Framework has quite good mechanism to deal with this &#8211; Zend_Acl. Out in the web there are a lot of resources about Zend_Acl&#8217;s usage, so I ain&#8217;t going to cover it one more &#8230; <a href="/2010/07/06/zend-framework-simple-acl-front-controller-plugin/" class="more-link">Continue reading <span class="screen-reader-text">Zend Framework: Simple Acl Front Controller Plugin</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins">Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/06/10/json-and-zend-framework-zend_json/" rel="bookmark" title="JSON and Zend Framework? &#8211; Zend_Json">JSON and Zend Framework? &#8211; Zend_Json </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/07/access.jpg"><img class="aligncenter size-full wp-image-1776" title="access" src="/wp-content/uploads/2010/07/access.jpg" alt="access with Zend_Acl" width="430" height="286" srcset="/wp-content/uploads/2010/07/access.jpg 430w, /wp-content/uploads/2010/07/access-300x199.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<p>Almost every web site need some abstraction over the access control list (ACL) to grant access of its users. As usual Zend Framework has quite good mechanism to deal with this &#8211; <a title="Zend_Acl" href="http://framework.zend.com/manual/en/zend.acl.html" target="_blank">Zend_Acl</a>.</p>
<p>Out in the web there are a lot of resources about Zend_Acl&#8217;s usage, so I ain&#8217;t going to cover it one more time, but simply copy/paste a very small front controller plugin implementing the basic usage of Zend_Acl.</p>
<p>Note that instead of defining the __construct() here is called preDispatch where the request is passed as a parameter. However only by copy pasting not every answer will be given. That&#8217;s why I&#8217;m going to write more about Zend_Acl in my future posts, for now only the source code:</p>
<pre lang="php">
<?php

class AclInit extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // create new acl object
        $acl = new Zend_Acl();

        // define resources. typically there are
        // only four resources from the CRUD functionality
        // but there can be added more resources
        $acl->add(new Zend_Acl_Resource('index'))
            ->add(new Zend_Acl_Resource('create'))
            ->add(new Zend_Acl_Resource('read'))
            ->add(new Zend_Acl_Resource('update'))
            ->add(new Zend_Acl_Resource('delete'));

        // define roles
        $acl->addRole(new Zend_Acl_Role('guest'))
            ->addRole(new Zend_Acl_Role('admin'));

        // define privileges
        $acl->allow('guest', array('index', 'read'))
            ->allow('admin');

        // setup acl in the registry for more
        Zend_Registry::set('acl', $acl);

        // check permissions
        if (!$acl->isAllowed('guest', $request->getActionName())) {
            $request->setControllerName('error');
            $request->setActionName('error');
        }
    }
}
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins">Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins </a></li>
<li><a href="/2010/08/06/setting-up-zend-framework-with-modules/" rel="bookmark" title="Setting Up Zend Framework with Modules">Setting Up Zend Framework with Modules </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/06/10/json-and-zend-framework-zend_json/" rel="bookmark" title="JSON and Zend Framework? &#8211; Zend_Json">JSON and Zend Framework? &#8211; Zend_Json </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/06/zend-framework-simple-acl-front-controller-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Routes with Zend_Controller_Router_Route</title>
		<link>/2010/04/20/custom-routes-with-zend_controller_router_route/</link>
		<comments>/2010/04/20/custom-routes-with-zend_controller_router_route/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 09:39:53 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Apache HTTP Server]]></category>
		<category><![CDATA[Bracket]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Front Controller pattern]]></category>
		<category><![CDATA[index controller]]></category>
		<category><![CDATA[Latin alphabet]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Punctuation]]></category>
		<category><![CDATA[Typography]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">/?p=1458</guid>
		<description><![CDATA[Rewrite The Url? Actually this is really a common task to do. You&#8217;ve to rewrite the url to be more &#8220;user friendly&#8221;. To be more clear I&#8217;ll give you an example. Let&#8217;s imagine you have a index controller and an index action. Thus if you have to load an Id from the database at least &#8230; <a href="/2010/04/20/custom-routes-with-zend_controller_router_route/" class="more-link">Continue reading <span class="screen-reader-text">Custom Routes with Zend_Controller_Router_Route</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/06/16/zend-examples-get-parameters-default-value/" rel="bookmark" title="Zend Examples: GET Parameters Default Value">Zend Examples: GET Parameters Default Value </a></li>
<li><a href="/2009/10/27/zend-framework-custom-urls/" rel="bookmark" title="Zend Framework custom URLs">Zend Framework custom URLs </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2009/12/20/redirecting-with-zend-framework-part-2/" rel="bookmark" title="Redirecting with Zend Framework &#8211; part 2">Redirecting with Zend Framework &#8211; part 2 </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Rewrite The Url?</h2>
<p>Actually this is really a common task to do. You&#8217;ve to rewrite the url to be more &#8220;user friendly&#8221;. To be more clear I&#8217;ll give you an example. Let&#8217;s imagine you have a index controller and an index action. Thus if you have to load an Id from the database at least with the build in <a title="Zend Framework" href="http://framework.zend.com/" target="_blank">Zend Framework</a> capabilities, you&#8217;ve to use this link:</p>
<pre>www.example.com/index/index/id/33949</pre>
<p>This is not good. It&#8217;s really bad. It will be great if you can use something like this:</p>
<pre>www.example.com/33949</pre>
<p>The solution in Zend Framework is called <a title="Zend_Controller_Router_Route" href="http://framework.zend.com/manual/en/zend.controller.router.html" target="_blank">Zend_Controller_Router_Route</a>. In a short snippet this will look like so:</p>
<pre lang="php" escaped="true">
$frontController-&gt;addRoute("my-route", new Zend_Controller_Router_Route("/:id",
    array("controller" =&gt; "index", "action" =&gt; "index", 'id' =&gt; 1),
    array('id' =&gt; '\d+')));
</pre>
<p>Here as you can see id is set to be 1. But don&#8217;t worry that&#8217;s not a hard code. The real work is done by the last parameter where the id is matched against a number value. Thus www.example.com/3 will be OK, but www.example.com/3a won&#8217;t.</p>
<h2>Important Note</h2>
<p>Don&#8217;t forget to add this rewrite rule in the .htaccess file.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/06/16/zend-examples-get-parameters-default-value/" rel="bookmark" title="Zend Examples: GET Parameters Default Value">Zend Examples: GET Parameters Default Value </a></li>
<li><a href="/2009/10/27/zend-framework-custom-urls/" rel="bookmark" title="Zend Framework custom URLs">Zend Framework custom URLs </a></li>
<li><a href="/2010/06/23/bind-zend-action-with-non-default-view-part-2/" rel="bookmark" title="Bind Zend Action with Non-Default View &#8211; Part 2">Bind Zend Action with Non-Default View &#8211; Part 2 </a></li>
<li><a href="/2009/12/20/redirecting-with-zend-framework-part-2/" rel="bookmark" title="Redirecting with Zend Framework &#8211; part 2">Redirecting with Zend Framework &#8211; part 2 </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/04/20/custom-routes-with-zend_controller_router_route/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Zend_Date &#8211; make it work and benefit with locales</title>
		<link>/2010/01/22/zend_date-make-it-work-and-benefit-with-locales/</link>
		<comments>/2010/01/22/zend_date-make-it-work-and-benefit-with-locales/#respond</comments>
		<pubDate>Fri, 22 Jan 2010 09:50:36 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Europe]]></category>
		<category><![CDATA[Front Controller pattern]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[United States]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">/?p=890</guid>
		<description><![CDATA[Although the documentation of Zend Framework is one of the most well structured documentation I’ve ever read it happened to me to stuck into it. And that happened exactly when I tried to use the date component of ZF &#8211; Zend_Date. Why I’d prefer Zend_Date? We all know what the date function of PHP is, &#8230; <a href="/2010/01/22/zend_date-make-it-work-and-benefit-with-locales/" class="more-link">Continue reading <span class="screen-reader-text">Zend_Date &#8211; make it work and benefit with locales</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2009/05/12/zend_date-with-dates-before-1901/" rel="bookmark" title="Zend_Date with dates before 1901">Zend_Date with dates before 1901 </a></li>
<li><a href="/2010/04/28/zend_datesetoptions-and-format_type-in-zend-framework-1-10-3/" rel="bookmark" title="Zend_Date::setOptions and format_type in Zend Framework 1.10.3">Zend_Date::setOptions and format_type in Zend Framework 1.10.3 </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Although the documentation of Zend Framework is one of the most well structured documentation I’ve ever read it happened to me to stuck into it. And that happened exactly when I tried to use the date component of ZF &#8211; <a title="Zend_Date doc" href="http://framework.zend.com/manual/en/zend.date.html" target="_blank">Zend_Date</a>.</p>
<h2>Why I’d prefer Zend_Date?</h2>
<p>We all know what the date function of PHP is, and what it does. It has reach set of formating string that help you do the job but as I’m working mostly on multilingual projects almost every project needs multilingual dates, and more or less nobody wants only numbers into the dates. And all this is pretty natural, instead of using the simple</p>
<blockquote>
<pre>01.01.2010</pre>
</blockquote>
<p>format, which beside it’s natural ugliness is not clear. Because as you may know in the <strong>US</strong> this can be understood as month.date.year, while in <strong>Europe</strong> this is date.month.year. And when it comes to dates like <strong>02.01.2010</strong> this can be really frustrating.</p>
<p>Beside this ugly format, you can choose to format your dates either with:</p>
<blockquote>
<pre>1 January 2010 or 1 Jan 2010</pre>
</blockquote>
<p>which is clear enough, but it’s only for english speakers. You know that in a spanish speaking country you should format the same date as:</p>
<blockquote>
<pre>1 Ene 2010</pre>
</blockquote>
<p>where “ene” means enero &#8211; january in spanish. How should you overcome this natural PHP problem?</p>
<h2>Well the answer is by using Zend_Date</h2>
<p>The problem I found in Zend_Date usage is that the native copy/paste technique from the doc page doesn’t seem to work. What the doc page says you is:</p>
<blockquote>
<pre>// setup the default timezone
date_default_timezone_set('Europe/London');</pre>
</blockquote>
<p>and than you can simply request a date with:</p>
<blockquote>
<pre>$date = new Zend_Date(‘2010-01-02’);
echo $date-&gt;get('d MMM yyyy');</pre>
</blockquote>
<p>where this should print 1 Jan 2010.</p>
<p>It’s a shame that this doesn’t work. The good part is that this problem can be overcome very simply. You just need to setup some more things just before calling the get method.</p>
<p>First setup a cache mechanism where you can cache the date locales. This is really good practice because it speeds up the framework when dealing with dates. Just add those lines after the default timezone is set:</p>
<blockquote>
<pre>// setup the default timezone
date_default_timezone_set('Europe/London');

$locale = new Zend_Locale(‘es_ES’);
Zend_Registry::set('Zend_Locale', $locale);

$frontendOptions = array(
		'lifetime' =&gt; 600, // 10 minutes
		'automatic_serialization' =&gt; true
);

$backendOptions = array(
                'cache_dir' =&gt; '../cache/'
		);

// getting a Zend_Cache_Core object
$cache = Zend_Cache::factory('Core',
		             'File',
		              $frontendOptions,
		              $backendOptions);</pre>
</blockquote>
<p>First you setup the <a title="Zend_Locale docs" href="http://framework.zend.com/manual/en/zend.locale.html" target="_blank">Zend_Locale</a> &#8211; a locale for the entire application and set it up in this example for spanish, than setup the cache mechanism.</p>
<p>This is a typical cache setup for Zend Framework. If you’d like to dive more into cacheing you can checkout the doc pages for <a title="Zend_Cache doc" href="http://framework.zend.com/manual/en/zend.cache.html" target="_blank">Zend_Cache</a>.</p>
<p>After these lines just setup Zend_Date options with:</p>
<blockquote>
<pre>Zend_Date::setOptions(array('format_type' =&gt; 'php',
                   'cache' =&gt; $cache));</pre>
</blockquote>
<p>This is really what you need to do to start. All these lines of code you may place into the bootstrap.php or into some front controller plugin.</p>
<p>Then you can call <a title="Zend_Date doc" href="http://framework.zend.com/manual/en/zend.date.html" target="_blank">Zend_Date’s</a> get method wherever you’d like in every controller you need it!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2009/05/12/zend_date-with-dates-before-1901/" rel="bookmark" title="Zend_Date with dates before 1901">Zend_Date with dates before 1901 </a></li>
<li><a href="/2010/04/28/zend_datesetoptions-and-format_type-in-zend-framework-1-10-3/" rel="bookmark" title="Zend_Date::setOptions and format_type in Zend Framework 1.10.3">Zend_Date::setOptions and format_type in Zend Framework 1.10.3 </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/01/22/zend_date-make-it-work-and-benefit-with-locales/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
