Posts Tagged ‘Zend Framework’

MVC in Zend Framework

Tuesday, May 12th, 2009 by hejian

Front Controller
Zend_Controller_Front implements a Front Controller pattern, in which all requests are intercepted by the front controller and dispatched to individual Action Controllers based on the URL requested.

Normally the Front Controller is used in bootstrap:

define('APPLICATION_PATH', dirname(__FILE__));
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');
$frontController->dispatch();

Action Controller
By default, the first segment of a URL path maps to a controller, and the second to an action. For example: http://domain.com/controller/action. If no action is provided, the action index is assumed, and if no controller is provided, the controller index is assumed.

This is an example Action Controller:

class IndexController extends Zend_Controller_Action {
public function indexAction() {
// set view variable
$this->view->baz = 'bat';
}
}

View
The view scripts are found in views/scrips/ directory. The view script for the default controller and action is in views/scripts/index/index.phtml.

We do not need to worry about initializing the view object, or rendering it. The ViewRenderer automatically determines the view script path and renders view script.

Model
Unlike the View and Controller, the Model can vary dramatically in responsibilities and data storage from application. The Zend Framework does not defined a model interface, class, or other formalism.

Plugins
The controller architecture includes a plugin system that allows user code to be called when cartain events occur in the controller process lifetime. These are the most important event:
preDispatch: called before an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior.
postDispatch: called after an action is dispatched by the dispatcher.

Wordpress template made by HeJian