Contents |
In 2006 Zend Framework introduced a conventional modular directory structure that allows users to group applications into self-contained units.
To load models within a module you can:
library/
Core/
Controller/
Action/
Helper/
GetModel.php
Zend//** * Core Library * * @category Core * @package Core_Controller * @author Federico Cargnelutti <fede.carg@gmail.com> * @license New BSD License */ /** Zend_Controller_Action_Helper_Abstract */ require_once 'Zend/Controller/Action/Helper/Abstract.php'; /** * @category Core * @package Core_Controller * @author Federico Cargnelutti <fede.carg@gmail.com> * @license New BSD License */ class Core_Controller_Action_Helper_GetModel extends Zend_Controller_Action_Helper_Abstract { /** * Zend_Registry instance * @var Zend_Registry */ protected $_container; /** * Return a single instance of the model object * * @param string $filename Filename. * @param null|string $module Module name. * @return object */ public function getModel($filename, $module = null) { if ($module === null) { $module = $this->getRequest()->getModuleName(); } $class = (string) ucfirst($module) . '_' . $filename; if (!$this->getContainer()->isRegistered($class)) { $moduleDir = $this->getFrontController()->getControllerDirectory($module); if ($moduleDir === null) { /** Zend_Controller_Action_Exception */ require_once 'Zend/Controller/Action/Exception.php'; throw new Zend_Controller_Action_Exception($module . ': The requested module does not exist.'); } $modelDir = dirname($moduleDir) . DIRECTORY_SEPARATOR . 'models'; $file = $filename . '.php'; Zend_Loader::loadFile($file, $modelDir, true); $model = new $class; $this->getContainer()->set($class, $model); } return $this->getContainer()->get($class); } /** * Call helper as broker method * * @param string $class Class name. * @param null|string $module Module name. * @return object */ public function direct($class, $module = null) { return $this->getModel($class, $module); } /** * Retrieve container * * @return Zend_Registry */ public function getContainer() { if ($this->_container === null) { require_once 'Zend/Registry.php'; $this->_container = Zend_Registry::getInstance(); } return $this->_container; } /** * Set the container instance * * @param Zend_Registry $container * @return Core_Controller_Action_Helper_GetModel */ public function setContainer(Zend_Registry $container) { $this->_container = $container; return $this; } }
Zend_Controller_Action_HelperBroker::addPrefix('Core_Controller_Action_Helper');
class Default_IndexModel extends Zend_Db_Table { protected $_name = 'default'; }
class Default_IndexController extends Zend_Controller_Action { public function indexAction() { $model = $this->_helper->getModel('IndexModel'); $sharedModel = $this->_helper->getModel('PostsModel', 'blog'); } }
library/
Core/
Controller/
Action.php
Zend//** * Core Library * * @category Core * @package Core_Controller * @author Federico Cargnelutti <fede.carg@gmail.com> * @license New BSD License */ /** Zend_Controller_Action */ require_once 'Zend/Controller/Action.php'; /** * @category Core * @package Core_Controller * @author Federico Cargnelutti <fede.carg@gmail.com> * @license New BSD License */ class Core_Controller_Action extends Zend_Controller_Action { /** * Zend_Registry instance * @var Zend_Registry */ protected $_container; /** * Return a single instance of the model object * * @param string $filename Filename. * @param null|string $module Module name. * @return object */ public function getModel($filename, $module = null) { if ($module === null) { $module = $this->getRequest()->getModuleName(); } $class = (string) ucfirst($module) . '_' . $filename; if (!$this->getContainer()->isRegistered($class)) { $moduleDir = $this->getFrontController()->getControllerDirectory($module); if ($moduleDir === null) { /** Zend_Controller_Action_Exception */ require_once 'Zend/Controller/Action/Exception.php'; throw new Zend_Controller_Action_Exception($module . ': The requested module does not exist.'); } $modelDir = dirname($moduleDir) . DIRECTORY_SEPARATOR . 'models'; $file = $filename . '.php'; Zend_Loader::loadFile($file, $modelDir, true); $model = new $class; $this->getContainer()->set($class, $model); } return $this->getContainer()->get($class); } /** * Retrieve container * * @return Zend_Registry */ public function getContainer() { if ($this->_container === null) { require_once 'Zend/Registry.php'; $this->_container = Zend_Registry::getInstance(); } return $this->_container; } /** * Set the container instance * * @param Zend_Registry $container * @return Core_Controller_Action */ public function setContainer(Zend_Registry $container) { $this->_container = $container; return $this; } }
class Default_IndexModel extends Zend_Db_Table { protected $_name = 'default'; }
class Default_IndexController extends Core_Controller_Action { public function indexAction() { $model = $this->getModel('IndexModel'); $sharedModel = $this->getModel('PostsModel', 'blog'); } }