Posts Tagged ‘MVC’
MVC in Joomla
Components
A component is in fact a seperate application. It contains it’s core, Model, View, Controller.
Module
Modules are extensions which present certain pieces of information on site.
Plugin
A plugin is a function which is performed on a part of Joomla before this part is shown.
Active Record
The Active Record is implemented using JTable. It is used for creating, reading, updating, and deleting records in database table. The Active Record file for component is at:
administrator/components/com_xxx/tables/yyy.php
Using Active Record save data:
$row =& JTable::getInstance('xxx', 'Table');
$post = JRequest::get( 'post' );
if (!$row->bind( $post )) {
JError::raiseError(500, $row->getError() );
}
$isNew = ($row->id == 0);
if (!$row->check()) {
JError::raiseError(500, $row->getError() );
}
if (!$row->store()) {
JError::raiseError(500, $row->getError() );
}
$row->checkin();
Model
The Model is implemented using JModel, the Model file for component is at:
components/com_xxx/models/yyy.php
View
The View is implemented using JView, the view file for component is at:
components/com_xxx/views/yyy/view.html.php
Controller
The Controller is implemented using JController, the controller file for component is at:
components/com_xxx/controller.php
Using Controller object from Component core file:
require_once(JPATH_COMPONENT.DS.'controller.php');
// Create the controller
$controller = new XxxController();
// Register Extra tasks
$controller->registerTask( 'new' , 'edit' );
$controller->registerTask( 'apply', 'save' );
// Perform the Request task
$controller->execute(JRequest::getVar('task', null, 'default', 'cmd'));
$controller->redirect();
MVC in Zend Framework
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.
MVC in Zen Cart
Zen Cart is a fork of osCommerce, but with a more graceful code structure. I have using Zen Cart in my projects more than 5 years. Zen Cart try to isolates business logic from user interface, and we can think Zen Cart has a MVC structure.
Controller
The controller of Zen Cart is the index.php, this is the flow of index.php:
- Load application_top.php
- Set main language directory based on $_SESSION['language']
- Load the model (includes/modules/pages/PAGE_NAME/header_php.php)
- Load html_header.php (this is a common template file)
- Load main_template_vars.php to setup the view: body_code = $template->get_template_dir(’tpl_’ . preg_replace(’/.php/’, ”,$_GET['main_page']) . ‘_default.php’,DIR_WS_TEMPLATE, $current_page_base,’templates’). ‘/tpl_’ . $_GET['main_page'] . ‘_default.php’;
- Load on_load scripts (pages based and site wide)
- Load tpl_main_page.php and load view: require($body_code);
- Load application_bottom.php
Model
Models in Zen Cart will provide the business data used by Views for every page.
Models are called by index.php:
$directory_array = $template->get_template_part($code_page_directory, '/^header_php/');
foreach ($directory_array as $value) {
/**
* We now load header code for a given page.
* Page code is stored in includes/modules/pages/PAGE_NAME/directory
* 'header_php.php' files in that directory are loaded now.
*/
require($code_page_directory . '/' . $value);
}
The models for every page are in includes/modules/PAGE_NAME/header_php.php
View
Views in Zen Cart are user interface for every page. The view are called from tpl_main_page.php:
<?php
/**
* prepares and displays center column
*
*/
require($body_code); ?>
The view for every page are in includes/templates/TEMPLATE_NAME/templates/tpl_PAGE_NAME_default.php
MVC in Magento
Magento is built on Zend Framework and follows MVC architecture. This article will only discuss the different part for Magento MVC architecture. For the common part of Zend Framework please refer MVC in Zend Framework
Module
Module are different functional areas in Magento. You can see the modules in “app/code/core/Mage”. Each module has it’s Blocks, Controllers, Modules.
Router
The Module’s router will let the system know how to handling any urls such as:
http://example.com/index.php/router/controller/action
The Router is setup at Module’s config file (etc/config.xml):
<frontend>
<routers>
<router>
<use>standard</use>
<args>
<module>Module_Name</module>
<frontName>router</frontName>
</args>
</router>
</routers>
</frontend>