<?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 &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/front_controller/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>Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins</title>
		<link>/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/</link>
		<comments>/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 13:01:02 +0000</pubDate>
		<dc:creator><![CDATA[stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[front_controller]]></category>
		<category><![CDATA[front_controller_plugin]]></category>
		<category><![CDATA[quick start]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">/?p=67</guid>
		<description><![CDATA[Why writing a front controller plugin? Almost every application uses a database connection and acl module. Why doing this in the bootstrap and to mantain many lines of code there, instead of making it clear and mantainable. Of course you can have all these lines of code in your bootstrap, but you know for serious &#8230; <a href="/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/" class="more-link">Continue reading <span class="screen-reader-text">Zend Framework &#8211; quick tutorial (part 3) &#8211; front controller plugins</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/06/zend-framework-simple-acl-front-controller-plugin/" rel="bookmark" title="Zend Framework: Simple Acl Front Controller Plugin">Zend Framework: Simple Acl Front Controller Plugin </a></li>
<li><a href="/2008/08/11/zend-framework-quick-tutorial-part-2-directory-layout-and-bootstrapping/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 2)">Zend Framework &#8211; quick tutorial (part 2) </a></li>
<li><a href="/2008/08/05/zend-framework-quick-tutorial-part-1-introduction/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 1)">Zend Framework &#8211; quick tutorial (part 1) </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>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Why writing a front controller plugin?</h2>
<p>Almost every application uses a database connection and acl module. Why doing this in the bootstrap and to mantain many lines of code there, instead of making it clear and mantainable. Of course you can have all these lines of code in your bootstrap, but you know for serious applications that recently will become an obstacle. That&#8217;s why Zend Framework allows you to use Front_Controller plugin.</p>
<h2>First in you bootstrap add those lines of code</h2>
<pre lang="php" escaped="true">
/*
 * add a simple plugin to the controller
 */

$front-&gt;registerPlugin(new Zend_Controller_Plugin_Init())
      -&gt;registerPlugin(new Zend_Controller_Plugin_Acl());
</pre>
<p>These two classes (Zend_Controller_Plugin_Init and Zend_Controller_Plugin_Acl) should be placed in two different files with the same names in Controller/Plugins directory under Zend folder, and garantee you that both classes will be instanciated before starting the front controller.<span id="more-67"></span></p>
<p>There is the example of Zend_Controller_Plugin_Init:</p>
<pre lang="php" escaped="true">&lt;?php

/** Zend_Acl */
require_once 'Zend/Acl.php';

/** Zend_Controller_Plugin_Abstract */
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Front Controller Plugin
 */
final class Zend_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{

/**
 * @var Zend_Acl
 **/
protected $_acl;

/**
 * @var string
 **/
protected $_roleName;

/**
 * @var array
 **/
protected $_errorPage;

/**
 * Constructor
 *
 * @param mixed $aclData
 * @param $roleName
 * @return void
 **/
public function __construct()
{

// define the error controller
$this-&gt;_errorPage = array('module' =&gt; 'default', 'controller' =&gt; 'error',
    'action' =&gt; 'denied');

$this-&gt;_roleName = 'defaultRole';
// if (null !== $this-&gt;_acl) {
$this-&gt;_initAcl();
// }

}

/**
 * Returns the ACL object
 *
 * @return Zend_Acl
 **/
public function getAcl()
{
    return $this-&gt;_acl;
}

/**
 * Sets the ACL role to use
 *
 * @param string $roleName
 * @return void
 **/
public function setRoleName($roleName)
{
    $this-&gt;_roleName = $roleName;
}

/**
 * Returns the ACL role used
 *
 * @return string
 * @author
 **/
public function getRoleName()
{
    return $this-&gt;_roleName;
}

/**
 * Sets the error page
 *
 * @param string $action
 * @param string $controller
 * @param string $module
 * @return void
 **/
public function setErrorPage($action, $controller = 'error', $module = null)
{
$this-&gt;_errorPage = array('module' =&gt; $module,
    'controller' =&gt; $controller,
    'action' =&gt; $action);
}

/**
 * Returns the error page
 *
 * @return array
 **/
public function getErrorPage()
{
    return $this-&gt;_errorPage;
}

/**
 * Predispatch
 * Checks if the current user identified by roleName has rights to the requested url (module/controller/action)
 * If not, it will call denyAccess to be redirected to errorPage
 *
 * @return void
 **/

public function preDispatch(Zend_Controller_Request_Abstract $request)
{
    $resourceName = '';
    if ($request-&gt;getModuleName() != 'default') {
    $resourceName .= $request-&gt;getModuleName() . ':';
}

$resourceName .= $request-&gt;getControllerName();

/** Check if the controller/action can be accessed by the current user */
if (!$this-&gt;getAcl()-&gt;isAllowed($this-&gt;_roleName, $resourceName, $request-&gt;getActionName())) {

/** Redirect to access denied page */
$this-&gt;denyAccess();

}

}

/**
 * Deny Access Function
 * Redirects to errorPage, this can be called from an action using the action helper
 *
 * @return void
 **/
public function denyAccess()
{
   $this-&gt;_request-&gt;setModuleName($this-&gt;_errorPage['module']);
   $this-&gt;_request-&gt;setControllerName($this-&gt;_errorPage['controller']);
   $this-&gt;_request-&gt;setActionName($this-&gt;_errorPage['action']);
}

/**
 * initialize the acl object and resources
 * for the roles used in the application
 *
 */
private function _initAcl()
{
   /*
    * define access control list
    */
   $this-&gt;_acl = new Zend_Acl();

   /**
    * define acl for default role
    */
   $this-&gt;_acl-&gt;addRole(new Zend_Acl_Role('defaultRole'))
        -&gt;add(new Zend_Acl_Resource('index'))
        -&gt;add(new Zend_Acl_Resource('portfolio'))
        -&gt;add(new Zend_Acl_Resource('user'))
        -&gt;allow('defaultRole');

   /**
    * define acl for administrator
    */
   $this-&gt;_acl-&gt;addRole(new Zend_Acl_Role('admin'))
        -&gt;add(new Zend_Acl_Resource('admin:index'))
        -&gt;add(new Zend_Acl_Resource('admin:cpanel'))
        -&gt;add(new Zend_Acl_Resource('admin:user'))
        -&gt;add(new Zend_Acl_Resource('admin:page'))
        -&gt;addRole(new Zend_Acl_Role('default'))
        -&gt;allow('admin');

   }

}

Init.php
&lt;?php

require_once 'Zend/Controller/Plugin/Abstract.php';
class Zend_Controller_Plugin_Init extends Zend_Controller_Plugin_Abstract
{
    public function __construct()
    {
        // require configuration
        require_once 'Zend/Config.php';

        // get configuration from ini file
        $config = new Zend_Config_Ini('../application/config.ini', 'dev');

        // connect to the database
        $db = new Zend_Db_Adapter_Pdo_Mysql($config-&gt;database-&gt;params);

        // assign the db adapter to be default for our models
        Zend_Db_Table::setDefaultAdapter($db);

        // register configuration at the registry
        Zend_Registry::set('config', $config);

        Zend_Registry::set('db', $db);

        // start layout mvc
        Zend_Layout::startMvc(array(
            'layoutPath' =&gt; '../application/layouts',
            'layout' =&gt; 'main'
        ));
    }

}</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/06/zend-framework-simple-acl-front-controller-plugin/" rel="bookmark" title="Zend Framework: Simple Acl Front Controller Plugin">Zend Framework: Simple Acl Front Controller Plugin </a></li>
<li><a href="/2008/08/11/zend-framework-quick-tutorial-part-2-directory-layout-and-bootstrapping/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 2)">Zend Framework &#8211; quick tutorial (part 2) </a></li>
<li><a href="/2008/08/05/zend-framework-quick-tutorial-part-1-introduction/" rel="bookmark" title="Zend Framework &#8211; quick tutorial (part 1)">Zend Framework &#8211; quick tutorial (part 1) </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>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2009/06/19/zend-framework-quick-tutorial-part-3-front-controller-plugins/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Zend_Controller_Front plugins</title>
		<link>/2008/07/31/zend_controller_front-plugins/</link>
		<comments>/2008/07/31/zend_controller_front-plugins/#respond</comments>
		<pubDate>Thu, 31 Jul 2008 08:27:42 +0000</pubDate>
		<dc:creator><![CDATA[stoimen]]></dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[front_controller]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">/?p=16</guid>
		<description><![CDATA[Една от особеностите на Zend Framework Front Controller е фактът, че може лесно да се напишат и ползват plugins към него. Колкото до официалната документация на ZF, поне според мен, не е достатъчно описателна и не дава ясна представа какво точно правят тези plugins и колкото и добре да е показано как се пишат и &#8230; <a href="/2008/07/31/zend_controller_front-plugins/" class="more-link">Continue reading <span class="screen-reader-text">Zend_Controller_Front plugins</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2008/07/31/zend-layout-dont-include-header-every-time/" rel="bookmark" title="Zend_Layout">Zend_Layout </a></li>
<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/04/11/why-everybodys-posting-top-n-jquery-plugins/" rel="bookmark" title="Why Everybody&#8217;s Posting: Top N jQuery Plugins!">Why Everybody&#8217;s Posting: Top N jQuery Plugins! </a></li>
<li><a href="/2009/05/25/ie-developer-tools/" rel="bookmark" title="IE developer tools">IE developer tools </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Една от особеностите на Zend Framework Front Controller е фактът, че може лесно да се напишат и ползват plugins към него. Колкото до официалната документация на ZF, поне според мен, не е достатъчно описателна и не дава ясна представа какво точно правят тези plugins и колкото и добре да е показано как се пишат и как са замислени не става известно какво предимство дават на приложението.</p>
<p>Добра статия за това как да се напише plugin и какви предимства дава може да се намери в една <a href="http://devzone.zend.com/article/3372-Front-Controller-Plugins-in-Zend-Framework">статия</a>, която намерих днес.</p>
<p>Ето и някои от предимствата на плъгините:</p>
<ol>
<li>Инициализация на приложението</li>
<li>Кеширане</li>
<li>Инициализация на router-а</li>
<li>Authentication и Authorization</li>
<li>Филтриране на изходния XHTML</li>
</ol>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2008/07/31/zend-layout-dont-include-header-every-time/" rel="bookmark" title="Zend_Layout">Zend_Layout </a></li>
<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/04/11/why-everybodys-posting-top-n-jquery-plugins/" rel="bookmark" title="Why Everybody&#8217;s Posting: Top N jQuery Plugins!">Why Everybody&#8217;s Posting: Top N jQuery Plugins! </a></li>
<li><a href="/2009/05/25/ie-developer-tools/" rel="bookmark" title="IE developer tools">IE developer tools </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2008/07/31/zend_controller_front-plugins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
