webdev - Zend Framework Tutorials
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:
- Sun: Design Patterns: Front Controller
- Microsoft: Front Controller
- Front Controller Plugins in Zend Framework
- Zend Framework Views and the Front Controller
- Making a custom front controller in Zend Framework
- Akra's DevNotes: Zend Framework Front Controller
- Zend Framework: Zend_Controller
- Zend Framework: Zend_Controller_Front
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.
| < Prev | Next > |
|---|
My Twitter
- feed:
- follow:
- 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 0 tweets in past 30 days from atirjavid:
People talking about '@Linux':




















