View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{composition-setup}

{zone-data:component-name}
Zend_Controller_Action
{zone-data}

{zone-data:proposer-list}
[~ixti]
{zone-data}

{zone-data:revision}
1.1 - 9 January 2008: Initial propose
{zone-data}

{zone-data:overview}
The aim of this proposal is to give a simple way to load models according to Zend Framework code style standards.
{zone-data}

{zone-data:references}
* [MVC Pattern Version 2|http://www.phppatterns.com/docs/design/mvc_pattern_version_2]
h3. Wikipedia resources
* [MVC Pattern|http://en.wikipedia.org/wiki/Model-view-controller]
* [Business logic|http://en.wikipedia.org/wiki/Business_logic]
h3. Similar proposal
* [Zend_Controller_Action_Helper_ModelLoader]
* [Zend_Db_Model - Borja Fernandez]
{zone-data}

{zone-data:requirements}
{zone-data}

{zone-data:dependencies}
* Zend_Loader
{zone-data}

{zone-data:operation}
The component is instantiated with a mind-link that it will make work parts of MVC pattern alltogether with unified style.
{zone-data}

{zone-data:milestones}
{zone-data}

{zone-data:class-list}
* Zend_Controller_Action
{zone-data}

{zone-data:use-cases}
||UC-01 (The simplest)||
{deck:id=UC-01}
{card:label=Controller}
h3. File: /path/to/mvc/controllers/BookshelfController.php
{code:php}
<?php
class BookshelfController extends Zend_Controller_Action
{
public function indexAction()
{
$model = $this->initModel();

$view = $this->initView();
$view ->assign('bookshelf', $model->getList());

$response = $this->getResponse();
$response ->appendBody($view->render());
}
}
{code}
{card}
{card:label=Model}
h3. File: /path/to/mvc/models/BookshelfModel.php
{code:php}
<?php
class BookshelfModel
{
protected $_db;

public function __construct()
{
$this->_db = Zend_Registry::get('db');
}

final public function getList()
{
return $this->fetchAll("SELECT * FROM `bookshelf`");
}
}
{code}
{card}
{card:label=View}
h3. File: /path/to/mvc/views/scripts/bookshelf/index.phtml
{code:php}
<?php if ($this->bookshelf): ?>
<table width="100%">
<thead>
<?php foreach (array_keys($this->bookshelf[0]) as $col_title): ?>
<th><?php echo $this->escape($col_title); ?></th>
<?php endforeach; ?>
</thead>
<?php foreach ($this->bookshelf as $row): ?>
<tr>
<?php foreach ($row as $col): ?>
<td><?php echo $this->escape($col); ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
{code}
{card}
{deck}
{zone-data}

{zone-data:skeletons}
{code}
abstract class Zend_Controller_Action
{
/**
* Directory where MVCs are stored.
*
* @var string
*/
protected $_mvcPath;


/**
* Model object
*
* @var object
*/
protected $model;


/**
* Retrieve MVCs path, where models, views and controllers are stored.
* If optional $appendPath is specified then
*
* @param string $appendPath (optional)
* @return string
* @throws Zend_Controller_Action_Exception if appendix was given and is not a string
*/
protected function _getMvcPath($appendPath = null)
{
if (!isset($this->_mvcPath)) {
$front = $this->getFrontController();
$request = $this->getRequest();
$module = $request->getModuleName();
$dirs = $front->getControllerDirectory();

if (empty($module) || !isset($dirs[$module])) {
$module = $front->getDispatcher()->getDefaultModule();
}

$this->_mvcPath = dirname($dirs[$module]);
}

if (null !== $appendPath) {
if (!is_string($appendPath)) {
throw new Zend_Controller_Action_Exception();
}
return $this->_mvcPath . DIRECTORY_SEPARATOR . $appendPath;
}

return $this->_mvcPath;
}


/**
* Retrieve Model object
*
* If {@link $model} is not set, instantiates a new Model object, using
* the 'modules' subdirectory at the same level as the controller directory
* for the current module as the base directory {@link _getMvcPath()}.
*
* By default (with $modelNameWithAction = false) the name of model
* will be composed from Controller name and suffix Model. If optional
* param $modelNameWithAction = true then name of model will be composed
* from name of Controller followed by underscope, Action name and suffix
* Model.
*
* Assume we calling $this->initModel() within FooController::DooAction().
* $this->initModel() or $this->initModel(false) will retrieve you:
* FooModel stored in models/FooModel.php
* $this->initModel(true) will retrieve you:
* Foo_DooModel stored in models/Foo/DooModel.php
*
* @param $modelNameWithAction
* @return object
*/
public function initModel($modelNameWithAction = false)
{
if (!isset($this->model)) {
$request = $this->getRequest();
$model = ucfirst($request->getControllerName());
if ($modelNameWithAction) {
$model .= '_' . ucfirst($request->getActionName());
}
$model .= 'Model';
Zend_Loader::loadClass($model, $this->_getMvcPath('models'));
$this->model = new $model();
}

return $this->model;
}

/**
* Initialize View object
*
* Initializes {@link $view} if not otherwise a Zend_View_Interface.
*
* If {@link $view} is not otherwise set, instantiates a new Zend_View
* object, using the 'views' subdirectory at the same level as the
* controller directory for the current module as the base directory.
* It uses this to set the following:
* - script path = views/scripts/
* - helper path = views/helpers/
* - filter path = views/filters/
*
* @return Zend_View_Interface
* @throws Zend_Controller_Exception if base view directory does not exist
*/
public function initView()
{
if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
return $this->view;
}

require_once 'Zend/View/Interface.php';
if (isset($this->view) && ($this->view instanceof Zend_View_Interface)) {
return $this->view;
}

$baseDir = $this->_getMvcPath('views');
if (!file_exists($baseDir) || !is_dir($baseDir)) {
throw new Zend_Controller_Exception('Missing base view directory ("' . $baseDir . '")');
}

require_once 'Zend/View.php';
$this->view = new Zend_View(array('basePath' => $baseDir));

return $this->view;
}
}
{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>