Zend Framework教程之分发器Zend

分发器的具体实现

Zend Framework的分发器Zend_Controller_Dispatcher设计主要有,如下类和接口组成:

├── Dispatcher
│   ├── Abstract.php
│   ├── Exception.php
│   ├── Interface.php
│   └── Standard.php

Zend_Controller_Dispatcher_Interface

定义了分发器提供的基本和标准功能。

interface Zend_Controller_Dispatcher_Interface { public function formatControllerName($unformatted); public function formatModuleName($unformatted); public function formatActionName($unformatted); public function isDispatchable(Zend_Controller_Request_Abstract $request); public function setParam($name, $value); public function setParams(array $params); public function getParam($name); public function getParams(); public function clearParams($name = null); public function setResponse(Zend_Controller_Response_Abstract $response = null); public function getResponse(); public function addControllerDirectory($path, $args = null); public function setControllerDirectory($path); public function getControllerDirectory(); public function dispatch(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response); public function isValidModule($module); public function getDefaultModule(); public function getDefaultControllerName(); public function getDefaultAction(); }

Zend_Controller_Dispatcher_Abstract

实现了Zend_Controller_Dispatcher_Interface接口,提供了分发器提供的基本和标准功能的抽象父类。

<?php /** Zend_Controller_Dispatcher_Interface */ require_once 'Zend/Controller/Dispatcher/Interface.php'; abstract class Zend_Controller_Dispatcher_Abstract implements Zend_Controller_Dispatcher_Interface { protected $_defaultAction = 'index'; protected $_defaultController = 'index'; protected $_defaultModule = 'default'; protected $_frontController; protected $_invokeParams = array(); protected $_pathDelimiter = '_'; protected $_response = null; protected $_wordDelimiter = array('-', '.'); public function __construct(array $params = array()) { $this->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, 0, 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_Standard

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/97b888cac825b6e3f20007902c9bf6e0.html