Zend Framework教程之分发器Zend_Controller_Dispatcher用法详解
|
本文实例讲述了Zend Framework教程之分发器Zend_Controller_Dispatcher用法。分享给大家供大家参考,具体如下: 分发器的具体实现Zend Framework的分发器Zend_Controller_Dispatcher设计主要有,如下类和接口组成: ├── Dispatcher │ ├── Abstract.php │ ├── Exception.php │ ├── Interface.php │ └── Standard.php Zend_Controller_Dispatcher_Interface定义了分发器提供的基本和标准功能。 Zend_Controller_Dispatcher_Abstract实现了Zend_Controller_Dispatcher_Interface接口,提供了分发器提供的基本和标准功能的抽象父类。 setParams($params);
}
public function formatControllerName($unformatted)
{
return ucfirst($this->_formatName($unformatted)) . 'Controller';
}
public function formatActionName($unformatted)
{
$formatted = $this->_formatName($unformatted,true);
return strtolower(substr($formatted,1)) . substr($formatted,1) . 'Action';
}
public function _verifyDelimiter($spec)
{
if (is_string($spec)) {
return (array) $spec;
} elseif (is_array($spec)) {
$allStrings = true;
foreach ($spec as $delim) {
if (!is_string($delim)) {
$allStrings = false;
break;
}
}
if (!$allStrings) {
require_once 'Zend/Controller/Dispatcher/Exception.php';
throw new Zend_Controller_Dispatcher_Exception('Word delimiter array must contain only strings');
}
return $spec;
}
require_once 'Zend/Controller/Dispatcher/Exception.php';
throw new Zend_Controller_Dispatcher_Exception('Invalid word delimiter');
}
public function getWordDelimiter()
{
return $this->_wordDelimiter;
}
public function setWordDelimiter($spec)
{
$spec = $this->_verifyDelimiter($spec);
$this->_wordDelimiter = $spec;
return $this;
}
public function getPathDelimiter()
{
return $this->_pathDelimiter;
}
public function setPathDelimiter($spec)
{
if (!is_string($spec)) {
require_once 'Zend/Controller/Dispatcher/Exception.php';
throw new Zend_Controller_Dispatcher_Exception('Invalid path delimiter');
}
$this->_pathDelimiter = $spec;
return $this;
}
protected function _formatName($unformatted,$isAction = false)
{
// preserve directories
if (!$isAction) {
$segments = explode($this->getPathDelimiter(),$unformatted);
} else {
$segments = (array) $unformatted;
}
foreach ($segments as $key => $segment) {
$segment = str_replace($this->getWordDelimiter(),' ',strtolower($segment));
$segment = preg_replace('/[^a-z0-9 ]/','',$segment);
$segments[$key] = str_replace(' ',ucwords($segment));
}
return implode('_',$segments);
}
public function getFrontController()
{
if (null === $this->_frontController) {
require_once 'Zend/Controller/Front.php';
$this->_frontController = Zend_Controller_Front::getInstance();
}
return $this->_frontController;
}
public function setFrontController(Zend_Controller_Front $controller)
{
$this->_frontController = $controller;
return $this;
}
public function setParam($name,$value)
{
$name = (string) $name;
$this->_invokeParams[$name] = $value;
return $this;
}
public function setParams(array $params)
{
$this->_invokeParams = array_merge($this->_invokeParams,$params);
return $this;
}
public function getParam($name)
{
if(isset($this->_invokeParams[$name])) {
return $this->_invokeParams[$name];
}
return null;
}
public function getParams()
{
return $this->_invokeParams;
}
public function clearParams($name = null)
{
if (null === $name) {
$this->_invokeParams = array();
} elseif (is_string($name) && isset($this->_invokeParams[$name])) {
unset($this->_invokeParams[$name]);
} elseif (is_array($name)) {
foreach ($name as $key) {
if (is_string($key) && isset($this->_invokeParams[$key])) {
unset($this->_invokeParams[$key]);
}
}
}
return $this;
}
public function setResponse(Zend_Controller_Response_Abstract $response = null)
{
$this->_response = $response;
return $this;
}
public function getResponse()
{
return $this->_response;
}
public function setDefaultControllerName($controller)
{
$this->_defaultController = (string) $controller;
return $this;
}
public function getDefaultControllerName()
{
return $this->_defaultController;
}
public function setDefaultAction($action)
{
$this->_defaultAction = (string) $action;
return $this;
}
public function getDefaultAction()
{
return $this->_defaultAction;
}
public function setDefaultModule($module)
{
$this->_defaultModule = (string) $module;
return $this;
}
public function getDefaultModule()
{
return $this->_defaultModule;
}
}
Zend_Controller_Dispatcher_StandardZendFramework继承抽象类Zend_Controller_Dispatcher_Abstract,定义了Zend_Controller_Dispatcher_Standard。Zend_Controller_Dispatcher_Standard是ZendFramework提供的基本的分发器,完成了分发功能。 _curModule = $this->getDefaultModule();
}
public function addControllerDirectory($path,$module = null)
{
if (null === $module) {
$module = $this->_defaultModule;
}
$module = (string) $module;
$path = rtrim((string) $path,'/');
$this->_controllerDirectory[$module] = $path;
return $this;
}
public function setControllerDirectory($directory,$module = null)
{
$this->_controllerDirectory = array();
if (is_string($directory)) {
$this->addControllerDirectory($directory,$module);
} elseif (is_array($directory)) {
foreach ((array) $directory as $module => $path) {
$this->addControllerDirectory($path,$module);
}
} else {
require_once 'Zend/Controller/Exception.php';
throw new Zend_Controller_Exception('Controller directory spec must be either a string or an array');
}
return $this;
}
public function getControllerDirectory($module = null)
{
if (null === $module) {
return $this->_controllerDirectory;
}
$module = (string) $module;
if (array_key_exists($module,$this->_controllerDirectory)) {
return $this->_controllerDirectory[$module];
}
return null;
}
public function removeControllerDirectory($module)
{
$module = (string) $module;
if (array_key_exists($module,$this->_controllerDirectory)) {
unset($this->_controllerDirectory[$module]);
return true;
}
return false;
}
public function formatModuleName($unformatted)
{
if (($this->_defaultModule == $unformatted) && !$this->getParam('prefixDefaultModule')) {
return $unformatted;
}
return ucfirst($this->_formatName($unformatted));
}
public function formatClassName($moduleName,$className)
{
return $this->formatModuleName($moduleName) . '_' . $className;
}
public function classToFilename($class)
{
return str_replace('_',DIRECTORY_SEPARATOR,$class) . '.php';
}
public function isDispatchable(Zend_Controller_Request_Abstract $request)
{
$className = $this->getControllerClass($request);
if (!$className) {
return false;
}
$finalClass = $className;
if (($this->_defaultModule != $this->_curModule)
|| $this->getParam('prefixDefaultModule'))
{
$finalClass = $this->formatClassName($this->_curModule,$className);
}
if (class_exists($finalClass,false)) {
return true;
}
$fileSpec = $this->classToFilename($className);
$dispatchDir = $this->getDispatchDirectory();
$test = $dispatchDir . DIRECTORY_SEPARATOR . $fileSpec;
return Zend_Loader::isReadable($test);
}
public function dispatch(Zend_Controller_Request_Abstract $request,Zend_Controller_Response_Abstract $response)
{
$this->setResponse($response);
/**
* Get controller class
*/
if (!$this->isDispatchable($request)) {
$controller = $request->getControllerName();
if (!$this->getParam('useDefaultControllerAlways') && !empty($controller)) {
require_once 'Zend/Controller/Dispatcher/Exception.php';
throw new Zend_Controller_Dispatcher_Exception('Invalid controller specified (' . $request->getControllerName() . ')');
}
$className = $this->getDefaultControllerClass($request);
} else {
$className = $this->getControllerClass($request);
if (!$className) {
$className = $this->getDefaultControllerClass($request);
}
}
/**
* Load the controller class file
*/
$className = $this->loadClass($className);
/**
* Instantiate controller with request,response,and invocation
* arguments; throw exception if it's not an action controller
*/
$controller = new $className($request,$this->getResponse(),$this->getParams());
if (!($controller instanceof Zend_Controller_Action_Interface) &&
!($controller instanceof Zend_Controller_Action)) {
require_once 'Zend/Controller/Dispatcher/Exception.php';
throw new Zend_Controller_Dispatcher_Exception(
'Controller "' . $className . '" is not an instance of Zend_Controller_Action_Interface'
);
}
/**
* Retrieve the action name
*/
$action = $this->getActionMethod($request);
/**
* Dispatch the method call
*/
$request->setDispatched(true);
// by default,buffer output
$disableOb = $this->getParam('disableOutputBuffering');
$obLevel = ob_get_level();
if (empty($disableOb)) {
ob_start();
}
try {
$controller->dispatch($action);
} catch (Exception $e) {
// Clean output buffer on error
$curObLevel = ob_get_level();
if ($curObLevel > $obLevel) {
do {
ob_get_clean();
$curObLevel = ob_get_level();
} while ($curObLevel > $obLevel);
}
throw $e;
}
if (empty($disableOb)) {
$content = ob_get_clean();
$response->appendBody($content);
}
// Destroy the page controller instance and reflection objects
$controller = null;
}
public function loadClass($className)
{
$finalClass = $className;
if (($this->_defaultModule != $this->_curModule)
|| $this->getParam('prefixDefaultModule'))
{
$finalClass = $this->formatClassName($this->_curModule,false)) {
return $finalClass;
}
$dispatchDir = $this->getDispatchDirectory();
$loadFile = $dispatchDir . DIRECTORY_SEPARATOR . $this->classToFilename($className);
if (Zend_Loader::isReadable($loadFile)) {
include_once $loadFile;
} else {
require_once 'Zend/Controller/Dispatcher/Exception.php';
throw new Zend_Controller_Dispatcher_Exception('Cannot load controller class "' . $className . '" from file "' . $loadFile . "'");
}
if (!class_exists($finalClass,false)) {
require_once 'Zend/Controller/Dispatcher/Exception.php';
throw new Zend_Controller_Dispatcher_Exception('Invalid controller class ("' . $finalClass . '")');
}
return $finalClass;
}
public function getControllerClass(Zend_Controller_Request_Abstract $request)
{
$controllerName = $request->getControllerName();
if (empty($controllerName)) {
if (!$this->getParam('useDefaultControllerAlways')) {
return false;
}
$controllerName = $this->getDefaultControllerName();
$request->setControllerName($controllerName);
}
$className = $this->formatControllerName($controllerName);
$controllerDirs = $this->getControllerDirectory();
$module = $request->getModuleName();
if ($this->isValidModule($module)) {
$this->_curModule = $module;
$this->_curDirectory = $controllerDirs[$module];
} elseif ($this->isValidModule($this->_defaultModule)) {
$request->setModuleName($this->_defaultModule);
$this->_curModule = $this->_defaultModule;
$this->_curDirectory = $controllerDirs[$this->_defaultModule];
} else {
require_once 'Zend/Controller/Exception.php';
throw new Zend_Controller_Exception('No default module defined for this application');
}
return $className;
}
public function isValidModule($module)
{
if (!is_string($module)) {
return false;
}
$module = strtolower($module);
$controllerDir = $this->getControllerDirectory();
foreach (array_keys($controllerDir) as $moduleName) {
if ($module == strtolower($moduleName)) {
return true;
}
}
return false;
}
public function getDefaultControllerClass(Zend_Controller_Request_Abstract $request)
{
$controller = $this->getDefaultControllerName();
$default = $this->formatControllerName($controller);
$request->setControllerName($controller)
->setActionName(null);
$module = $request->getModuleName();
$controllerDirs = $this->getControllerDirectory();
$this->_curModule = $this->_defaultModule;
$this->_curDirectory = $controllerDirs[$this->_defaultModule];
if ($this->isValidModule($module)) {
$found = false;
if (class_exists($default,false)) {
$found = true;
} else {
$moduleDir = $controllerDirs[$module];
$fileSpec = $moduleDir . DIRECTORY_SEPARATOR . $this->classToFilename($default);
if (Zend_Loader::isReadable($fileSpec)) {
$found = true;
$this->_curDirectory = $moduleDir;
}
}
if ($found) {
$request->setModuleName($module);
$this->_curModule = $this->formatModuleName($module);
}
} else {
$request->setModuleName($this->_defaultModule);
}
return $default;
}
public function getDispatchDirectory()
{
return $this->_curDirectory;
}
public function getActionMethod(Zend_Controller_Request_Abstract $request)
{
$action = $request->getActionName();
if (empty($action)) {
$action = $this->getDefaultAction();
$request->setActionName($action);
}
return $this->formatActionName($action);
}
}
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
