Currently Online

We have 22 guests online
None

Login

             | 

Search

Like it? Share it!

A Modular Directory Structure Quickstart: Module Switcher Front Controller Plugin

webdev - Zend Framework Tutorials

User Rating: / 9
PoorBest 

Hello and welcome to the second installment in the Zend_Application tutorial series.  In this tutorial, we are going to create a front controller plugin that is going to change out modules based on the user's request.

In the front controller design pattern, a single controller takes and handles all user requests. For an application of moderate to severe complexity, a front controller delegates the responsibility to command controllers (also called dispatchers), routers that map user requests and various URIs to actual controllers and actions, registers view helpers and other resources that a user may request.  A front controller can help eliminate duplicate code on a series of requests by routing the requests through the front controller and factoring the duplicate code from the requests into the front controller.

Here are some excellent resources on the subject:

Hopefully by now you have a bit more understanding about what the Front Controller is, and how it operates in the Zend Framework.

Now we will create our Front Controller Plugin that will switch our modules layouts.

create and/or edit library/App/Controller/Plugin/ModuleSwitcher.php

class App_Controller_Plugin_ModuleSwitcher extends Zend_Controller_Plugin_Abstract
{
    protected $_view = null;

    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $moduleName = $request->getModuleName();

        Zend_Layout::startMvc();
        $layout = Zend_Layout::getMvcInstance();  
        $layout->setLayoutPath('../application/modules/' . $moduleName . '/layouts')->setLayout($moduleName);

        $view = new Zend_View();
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->init();      

        $this->_view = $viewRenderer->view;
        $this->_view->headMeta()->setHttpEquiv('content-type', 'text/html; charset=utf-8');
        return $request;
    }
}

Lets go ahead and break this down. You have to understand how the Zend_Controller, Front Controller Design Pattern, and Front Controller Plugins operate in the Zend Framework.Zend_Controller in the Reference Guide is an excellent starting point. Specifically 12.2. Zend_Controller Basics

We need to implement our module layout switcher plugin before any controller dispatching has occured and set our controller settings there. This is why I chose to use the routeShutdown() method. We basically get our request object after the router has finished routering, and before the conrollers are dispatched. Some people use preDispatch() or another method. It really depends on how you want it. I haven't had anyone tell me otherwise yet, so I'll continue to use this method.

$moduleName = $request->getModuleName();

First we have to start the Mvc Layout.  If you don't use the startMvc static method on the Zend_Layout object, when you try to retrieve the instance via getMvcInstance() it will return 'null' and throw an exception.  I am setting the layout filename to be the same as the module name.  You don't have to set this, as the default will be layout.phtml.

Zend_Layout::startMvc();
$layout = Zend_Layout::getMvcInstance();  
$layout->setLayoutPath('../application/modules/' . $moduleName . '/layouts')->setLayout($moduleName);

I also took the liberty of setting up my View object here as well.  Why not? right?  Unless someone has any comments about it, I think I'll keep it in here, register my viewRenderer helper broker and initialize the view renderer.

$view = new Zend_View();
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->init();      

Set the content type, default charset.

$this->_view = $viewRenderer->view;
$this->_view->headMeta()->setHttpEquiv('content-type', 'text/html; charset=utf-8');

 

Alright, thanks for being with me again guys.  In this tutorial we learned how to create the front controller plugin in order to switch our modules layouts.  In the next tutorial I am going to talk about the config.ini file, explain directives corresponding to respective methods and settings.

My Twitter

atirjavid's avatar
Atir Javid atirjavid
  • bio:
    C.E.O Frosted Web, Social Media Marketing & Development | C.T.O. Sky Social Media Inc, former Debian contributor, maintainer and developer. Currently w/FreeBSD.
  • web:
  • location:
    Baton Rouge, LA. USA
  • updates:
    389
  • followers:
    136
  • following:
    15
Last 4 following:
runev's avatar emiltofan's avatar CalEvans's avatar fabpot's avatar
Loading...

Last 0 tweets in past 30 days from atirjavid:

People talking about '@Linux':

Quick Links

CopyLeft Notice

Distribution of the Original Works

You’re free to copy and redistribute the copylefted work — either by itself, or as part of a larger collection of works. All you have to do is (1) be honest - by clearly crediting me as the author, and only redistributing accurate copies of the original (But you don't REALLY have to :) hehe )- and (2) keep it free - by distributing the copy under the same copyleft license, and including either a copy of the license, or a reference to its URL (http://creativecommons.org/licenses/by-sa/1.0/legalcode).

You don’t have to copyleft your whole newsletter to reprint a single article of mine in it (although copylefting your whole newsletter is certainly worthwhile on its own merits). You only have to distribute the copy of my work under the copyleft terms. You’re also free to distribute abridged, translated, or otherwise edited copies of my works — but these count as derivative works, not copies of the original.

Derivative Works

You’re also free to make and distribute derivative works from the copylefted work (such as abridged or edited copies of original works, stories about characters introduced in the original works, etc.). All you have to do is (1) be honest — by clearly identifying your work as a derivative work, and clearly crediting me as the author of the original work on which it was based (But you don't REALLY have to do that either :) Hehehe) — and (2) keep it free — by distributing your derivative work as free content under the terms of the same copyleft license under which the original work was distributed. Please read the (The GNU General Public License ). Thank you very much.

Credit me or don't. Just please keep the information free and flowing.