//Zend_Controller_Front 包含插件【zend框架】 //我只不过是把zend前端重写了一遍,只是为了了解思路。仅供学习。 <?php /** * Zend_Controller_Front 精简版 转载请注明 * @author wolf [Qq: 116311316] * @example $front=Custom_Controller_Front::getInstance(); * @version 1.0 * */ class Custom_Controller_Front { protected static $_instance = null; //Zend_Controller_Front 单例模式 protected $_dispatcher; //派遣器 protected $_router = null; //路由器 protected $_plugin; public function __construct() { $this->_plugin = new Custom_Controller_Plugin_Broker(); } /** *获得Front实例 * @return Zend_Controller_Front */ public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; } /** *获得一个派遣器 * @return Zend_Controller_Dispatcher */ public function getDispatcher() { if (! $this->_dispatcher instanceof Custom_Controller_Dispatcher) { $this->_dispatcher = new Custom_Controller_Dispatcher(); } return $this->_dispatcher; } /** * 多模块配置目录方法 迭代器 * @param $path */ public function addModuleDirectory($path) { try { $dir = new DirectoryIterator($path); } catch (Exception $e) { require_once 'Zend/Controller/Exception.php'; throw new Zend_Controller_Exception("Directory $path not readable", 0, $e); } foreach ($dir as $file) { if ($file->isDot() || ! $file->isDir()) { continue; } $module = $file->getFilename(); // Don't use SCCS directories as modules if (preg_match('/^[^a-z]/i', $module) || ('CVS' == $module)) { continue; } $moduleDir = $file->getPathname() . DIRECTORY_SEPARATOR . 'controllers'; $this->addControllerDirectory($moduleDir, $module); } return $this; } /** * 添加单个模块 结合addModuleDirectory * @param $directory * @param $module */ public function addControllerDirectory($directory, $module = null) { $this->getDispatcher()->addControllerDirectory($directory, $module); return $this; } /** * 获得module目录 * @param $name */ public function getControllerDirectory($name = null) { return $this->getDispatcher()->getControllerDirectory($name); } /** * 获得一个路由器 官方copy的 * @return Custom_Controller_Router_Rewrite */ public function getRouter() { if (null == $this->_router) { $this->_router = new Custom_Controller_Router_Rewrite(); } return $this->_router; } /** * 这里自定义设置派遣器 默认继承了Custom_Controller_Dispatcher_Interface接口 * 这里我列举一种 其他的都一样 * @link http://www.cnblogs.com/terryglp/articles/1775778.html * @return Zend_Controller_Dispatcher */ public function setDispatcher(Custom_Controller_Dispatcher_Interface $dispatcher) { $this->_dispatcher = $dispatcher; return $this; } /** * 注册插件 * @param Custom_Controller_Plugin_Abstract $plugin */ public function registerPlugin(Custom_Controller_Plugin_Abstract $plugin){ $this->_plugin->setPlugin($plugin); } /** * 获取插件 */ /** * 非常重要 三步完成任务 request router dispatch * Dispatch an HTTP request to a controller/action. * @return void */ public function dispatch() { $request = new Custom_Controller_Request_Http(); //这里直接得到一个request; $router = $this->getRouter(); //交给路由进行解析 $router->route($request); $this->_plugin->dispatchLoopStartup($request); $this->getDispatcher()->dispatch($request); } } // End ^ LF ^ UTF-8