📄 front.php
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category Zend * @package Zend_Controller * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License *//** Zend_Loader */require_once 'Zend/Loader.php';/** Zend_Controller_Action_HelperBroker */require_once 'Zend/Controller/Action/HelperBroker.php';/** Zend_Controller_Exception */require_once 'Zend/Controller/Exception.php';/** Zend_Controller_Plugin_Broker */require_once 'Zend/Controller/Plugin/Broker.php';/** Zend_Controller_Request_Abstract */require_once 'Zend/Controller/Request/Abstract.php';/** Zend_Controller_Router_Interface */require_once 'Zend/Controller/Router/Interface.php';/** Zend_Controller_Dispatcher_Interface */require_once 'Zend/Controller/Dispatcher/Interface.php';/** Zend_Controller_Response_Abstract */require_once 'Zend/Controller/Response/Abstract.php';/** * @category Zend * @package Zend_Controller * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Controller_Front{ /** * Base URL * @var string */ protected $_baseUrl = null; /** * Directory|ies where controllers are stored * * @var string|array */ protected $_controllerDir = null; /** * Instance of Zend_Controller_Dispatcher_Interface * @var Zend_Controller_Dispatcher_Interface */ protected $_dispatcher = null; /** * Singleton instance * * Marked only as protected to allow extension of the class. To extend, * simply override {@link getInstance()}. * * @var Zend_Controller_Front */ protected static $_instance = null; /** * Array of invocation parameters to use when instantiating action * controllers * @var array */ protected $_invokeParams = array(); /** * Subdirectory within a module containing controllers; defaults to 'controllers' * @var string */ protected $_moduleControllerDirectoryName = 'controllers'; /** * Instance of Zend_Controller_Plugin_Broker * @var Zend_Controller_Plugin_Broker */ protected $_plugins = null; /** * Instance of Zend_Controller_Request_Abstract * @var Zend_Controller_Request_Abstract */ protected $_request = null; /** * Instance of Zend_Controller_Response_Abstract * @var Zend_Controller_Response_Abstract */ protected $_response = null; /** * Whether or not to return the response prior to rendering output while in * {@link dispatch()}; default is to send headers and render output. * @var boolean */ protected $_returnResponse = false; /** * Instance of Zend_Controller_Router_Interface * @var Zend_Controller_Router_Interface */ protected $_router = null; /** * Whether or not exceptions encountered in {@link dispatch()} should be * thrown or trapped in the response object * @var boolean */ protected $_throwExceptions = false; /** * Constructor * * Instantiate using {@link getInstance()}; front controller is a singleton * object. * * Instantiates the plugin broker. * * @return void */ protected function __construct() { $this->_plugins = new Zend_Controller_Plugin_Broker(); } /** * Enforce singleton; disallow cloning * * @return void */ private function __clone() { } /** * Singleton instance * * @return Zend_Controller_Front */ public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; } /** * Resets all object properties of the singleton instance * * Primarily used for testing; could be used to chain front controllers. * * @return void */ public function resetInstance() { $reflection = new ReflectionObject($this); foreach ($reflection->getProperties() as $property) { $name = $property->getName(); switch ($name) { case '_instance': break; case '_controllerDir': case '_invokeParams': $this->{$name} = array(); break; case '_plugins': $this->{$name} = new Zend_Controller_Plugin_Broker(); break; case '_throwExceptions': case '_returnResponse': $this->{$name} = false; break; case '_moduleControllerDirectoryName': $this->{$name} = 'controllers'; break; default: $this->{$name} = null; break; } } } /** * Convenience feature, calls setControllerDirectory()->setRouter()->dispatch() * * In PHP 5.1.x, a call to a static method never populates $this -- so run() * may actually be called after setting up your front controller. * * @param string|array $controllerDirectory Path to Zend_Controller_Action * controller classes or array of such paths * @return void * @throws Zend_Controller_Exception if called from an object instance */ public static function run($controllerDirectory) { self::getInstance() ->setControllerDirectory($controllerDirectory) ->dispatch(); } /** * Add a controller directory to the controller directory stack * * If $args is presented and is a string, uses it for the array key mapping * to the directory specified. * * @param string $directory * @param string $module Optional argument; module with which to associate directory. If none provided, assumes 'default' * @return Zend_Controller_Front * @throws Zend_Controller_Exception if directory not found or readable */ public function addControllerDirectory($directory, $module = null) { $this->getDispatcher()->addControllerDirectory($directory, $module); return $this; } /** * Set controller directory * * Stores controller directory(ies) in dispatcher. May be an array of * directories or a string containing a single directory. * * @param string|array $directory Path to Zend_Controller_Action controller * classes or array of such paths * @param string $module Optional module name to use with string $directory * @return Zend_Controller_Front */ public function setControllerDirectory($directory, $module = null) { $this->getDispatcher()->setControllerDirectory($directory, $module); return $this; } /** * Retrieve controller directory * * Retrieves: * - Array of all controller directories if no $name passed * - String path if $name passed and exists as a key in controller directory array * - null if $name passed but does not exist in controller directory keys * * @param string $name Default null * @return array|string|null */ public function getControllerDirectory($name = null) { return $this->getDispatcher()->getControllerDirectory($name); } /** * Remove a controller directory by module name * * @param string $module * @return bool */ public function removeControllerDirectory($module) { return $this->getDispatcher()->removeControllerDirectory($module); } /** * Specify a directory as containing modules * * Iterates through the directory, adding any subdirectories as modules; * the subdirectory within each module named after {@link $_moduleControllerDirectoryName} * will be used as the controller directory path. * * @param string $path * @return Zend_Controller_Front */ public function addModuleDirectory($path) { try{ $dir = new DirectoryIterator($path); }catch(Exception $e){ throw new Zend_Controller_Exception("Directory $path not readable"); } 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 . $this->getModuleControllerDirectoryName(); $this->addControllerDirectory($moduleDir, $module); } return $this; } /** * Set the directory name within a module containing controllers * * @param string $name * @return Zend_Controller_Front */ public function setModuleControllerDirectoryName($name = 'controllers') { $this->_moduleControllerDirectoryName = (string) $name; return $this; } /** * Return the directory name within a module containing controllers * * @return string */ public function getModuleControllerDirectoryName() { return $this->_moduleControllerDirectoryName; } /** * Set the default controller (unformatted string) * * @param string $controller * @return Zend_Controller_Front */ public function setDefaultControllerName($controller) { $dispatcher = $this->getDispatcher(); $dispatcher->setDefaultControllerName($controller); return $this; } /** * Retrieve the default controller (unformatted string) * * @return string */ public function getDefaultControllerName() { return $this->getDispatcher()->getDefaultControllerName(); } /** * Set the default action (unformatted string) * * @param string $action * @return Zend_Controller_Front */ public function setDefaultAction($action) { $dispatcher = $this->getDispatcher(); $dispatcher->setDefaultAction($action); return $this; } /** * Retrieve the default action (unformatted string) * * @return string */ public function getDefaultAction() { return $this->getDispatcher()->getDefaultAction(); } /** * Set the default module name * * @param string $module * @return Zend_Controller_Front */ public function setDefaultModule($module) { $dispatcher = $this->getDispatcher(); $dispatcher->setDefaultModule($module); return $this; } /** * Retrieve the default module * * @return string */ public function getDefaultModule() { return $this->getDispatcher()->getDefaultModule(); } /** * Set request class/object * * Set the request object. The request holds the request environment. * * If a class name is provided, it will instantiate it * * @param string|Zend_Controller_Request_Abstract $request * @throws Zend_Controller_Exception if invalid request class * @return Zend_Controller_Front */ public function setRequest($request) { if (is_string($request)) { Zend_Loader::loadClass($request); $request = new $request(); } if (!$request instanceof Zend_Controller_Request_Abstract) { throw new Zend_Controller_Exception('Invalid request class'); } $this->_request = $request; return $this; } /** * Return the request object. * * @return null|Zend_Controller_Request_Abstract */ public function getRequest() { return $this->_request; } /** * Set router class/object * * Set the router object. The router is responsible for mapping * the request to a controller and action. * * If a class name is provided, instantiates router with any parameters * registered via {@link setParam()} or {@link setParams()}. * * @param string|Zend_Controller_Router_Interface $router * @throws Zend_Controller_Exception if invalid router class * @return Zend_Controller_Front */ public function setRouter($router) { if (is_string($router)) { Zend_Loader::loadClass($router); $router = new $router(); } if (!$router instanceof Zend_Controller_Router_Interface) { throw new Zend_Controller_Exception('Invalid router class'); } $this->_router = $router; return $this; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -