@H_502_0@本文实例讲述了Zend Framework动作助手(Zend_Controller_Action_Helper)用法。分享给大家供大家参考,具体如下:
@H_
502_0@通过助手模式,可以把一些经常使用的
功能模块做封装,从而在可以在需要的地方灵活使用,主要是在action使用。
@H_
502_0@Zend Framework中有两种助手,动作助手(Zend_Controller_Action_Helper)和试图助手(Zend_View
Helper)。
@H502_0@动作助手可以向任何Zend_Controller_Action的衍生动作控制器中,即时的加入
功能(runtime and/or on-demand functionality),以使得
增加公共的动作控制器
功能时,尽量减少衍生动作控制器类的必要。
@H_
502_0@动作助手在需要
调用时加载,可以在请求的时候(bootstrap)或者动作控制器创建的时候(init())实例化。
@H_
502_0@涉及到的相关
文件
@H_
5020@在/library/Zend/Controller/Action/中
@H502_0@│ Exception.
PHP
│ HelperBroker.
PHP
│ Interface.
PHP
│
├─Helper
│ │ Abstract.
PHP
│ │ ActionStack.
PHP
│ │ AjaxContext.
PHP
│ │ AutoCompleteDojo.
PHP
│ │ AutoCompleteScriptaculous.
PHP
│ │ Cache.
PHP
│ │ ContextSwitch.
PHP
│ │ FlashMessenger.
PHP
│ │ Json.
PHP
│ │ Redirector.
PHP
│ │ Url.
PHP
│ │ ViewRenderer.
PHP
│ │
│ └─AutoComplete
│ Abstract.
PHP
│
└─HelperBroker
PriorityStack.
PHP
@H_
5020@常见的动作助手有
:
@H502_0@FlashMessenger 用来处理Flash Messenger会话;
Json 用来解码和发送 JSON 响应;
Url 用于创建Urls;
Redirector 提供另一种实现方式,帮助程序
重定向到内部或者外部
页面;
ViewRenderer
自动的完成在控制器内建立视图对象并渲染视图的过程;
AutoComplete
自动响应 AJAX 的
自动完成;
ContextSwitch 和 AjaxContext 为你的动作提供替代响应格式;
Cache 实现cache的相关操作;
ActionStack 用于操作动作堆栈。
@H_
5020@动手的几种实例化使用方式
@H502_0@<span style="color: #0000ff">1.通过Zend_Controller_Action的 $_helper成员的getHelper()
方法。直接
调用getHelper(),传入助手的
名称即可。
<div class="jb51code">
<pre class="brush:php;">
$redirector = $this->_helper->getHelper('Redirector');
//$redirector->getName();
$redirector->gotoSimple('index2');
属性对应的助手对象。
_helper->Redirector;
@H_502_0@Zend_Controller_Action_HelperBroker
@H_502_0@中文名称译作"助手经纪人",顾名思义,是动作助手的中间人。
@H_502_0@在动作的实例化使用的方式的第二种方式就是通过Zend_Controller_Action_HelperBroker的魔术方法__get()来实现的。
@H_502_0@助手经纪人用于注册助手对象和助手路径以及获取助手等等功能。
@H_502_0@Zend_Controller_Action_HelperBroker的实现以及常用方法列表
PHP;">
'Zend/Controller/Action/Helper/',));
}
return self::$_pluginLoader;
}
/**
* addPrefix() - Add repository of helpers by prefix
*
* @param string $prefix
*/
static public function addPrefix($prefix)
{
$prefix = rtrim($prefix,'_');
$path = str_replace('_',DIRECTORY_SEPARATOR,$prefix);
self::getPluginLoader()->addPrefixPath($prefix,$path);
}
/**
* addPath() - Add path to repositories where Action_Helpers could be found.
*
* @param string $path
* @param string $prefix Optional; defaults to 'Zend_Controller_Action_Helper'
* @return void
*/
static public function addPath($path,$prefix = 'Zend_Controller_Action_Helper')
{
self::getPluginLoader()->addPrefixPath($prefix,$path);
}
/**
* addHelper() - Add helper objects
*
* @param Zend_Controller_Action_Helper_Abstract $helper
* @return void
*/
static public function addHelper(Zend_Controller_Action_Helper_Abstract $helper)
{
self::getStack()->push($helper);
return;
}
/**
* resetHelpers()
*
* @return void
*/
static public function resetHelpers()
{
self::$_stack = null;
return;
}
/**
* Retrieve or initialize a helper statically
*
* Retrieves a helper object statically,loading on-demand if the helper
* does not already exist in the stack. Always returns a helper,unless
* the helper class cannot be found.
*
* @param string $name
* @return Zend_Controller_Action_Helper_Abstract
*/
public static function getStaticHelper($name)
{
$name = self::_normalizeHelperName($name);
$stack = self::getStack();
if (!isset($stack->{$name})) {
self::_loadHelper($name);
}
return $stack->{$name};
}
/**
* getExistingHelper() - get helper by name
*
* Static method to retrieve helper object. Only retrieves helpers already
* initialized with the broker (either via addHelper() or on-demand loading
* via getHelper()).
*
* Throws an exception if the referenced helper does not exist in the
* stack; use {@link hasHelper()} to check if the helper is registered
* prior to retrieving it.
*
* @param string $name
* @return Zend_Controller_Action_Helper_Abstract
* @throws Zend_Controller_Action_Exception
*/
public static function getExistingHelper($name)
{
$name = self::_normalizeHelperName($name);
$stack = self::getStack();
if (!isset($stack->{$name})) {
require_once 'Zend/Controller/Action/Exception.
PHP';
throw new Zend_Controller_Action_Exception('Action helper "' . $name . '" has not been registered with the helper broker');
}
return $stack->{$name};
}
/**
* Return all registered helpers as helper => object pairs
*
* @return array
*/
public static function getExistingHelpers()
{
return self::getStack()->getHelpersByName();
}
/**
* Is a particular helper loaded in the broker?
*
* @param string $name
* @return boolean
*/
public static function hasHelper($name)
{
$name = self::_normalizeHelperName($name);
return isset(self::getStack()->{$name});
}
/**
* Remove a particular helper from the broker
*
* @param string $name
* @return boolean
*/
public static function removeHelper($name)
{
$name = self::_normalizeHelperName($name);
$stack = self::getStack();
if (isset($stack->{$name})) {
unset($stack->{$name});
}
return false;
}
/**
* Lazy load the priority stack and return it
*
* @return Zend_Controller_Action_HelperBroker_PriorityStack
*/
public static function getStack()
{
if (self::$_stack == null) {
self::$_stack = new Zend_Controller_Action_HelperBroker_PriorityStack();
}
return self::$_stack;
}
/**
* Constructor
*
* @param Zend_Controller_Action $actionController
* @return void
*/
public function __construct(Zend_Controller_Action $actionController)
{
$this->_actionController = $actionController;
foreach (self::getStack() as $helper) {
$helper->setActionController($actionController);
$helper->init();
}
}
/**
* notifyPreDispatch() - called by action controller dispatch method
*
* @return void
*/
public function notifyPreDispatch()
{
foreach (self::getStack() as $helper) {
$helper->preDispatch();
}
}
/**
* notifyPostDispatch() - called by action controller dispatch method
*
* @return void
*/
public function notifyPostDispatch()
{
foreach (self::getStack() as $helper) {
$helper->postDispatch();
}
}
/**
* getHelper() - get helper by name
*
* @param string $name
* @return Zend_Controller_Action_Helper_Abstract
*/
public function getHelper($name)
{
$name = self::_normalizeHelperName($name);
$stack = self::getStack();
if (!isset($stack->{$name})) {
self::_loadHelper($name);
}
$helper = $stack->{$name};
$initialize = false;
if (null === ($actionController = $helper->getActionController())) {
$initialize = true;
} elseif ($actionController !== $this->_actionController) {
$initialize = true;
}
if ($initialize) {
$helper->setActionController($this->_actionController)
->init();
}
return $helper;
}
/**
* Method overloading
*
* @param string $method
* @param array $args
* @return mixed
* @throws Zend_Controller_Action_Exception if helper does not have a direct() method
*/
public function __call($method,$args)
{
$helper = $this->getHelper($method);
if (!method_exists($helper,'direct')) {
require_once 'Zend/Controller/Action/Exception.
PHP';
throw new Zend_Controller_Action_Exception('Helper "' . $method . '" does not support overloading via direct()');
}
return call_user_func_array(array($helper,'direct'),$args);
}
/**
* Retrieve helper by name as object property
*
* @param string $name
* @return Zend_Controller_Action_Helper_Abstract
*/
public function __get($name)
{
return $this->getHelper($name);
}
/**
* Normalize helper name for lookups
*
* @param string $name
* @return string
*/
protected static function _normalizeHelperName($name)
{
if (strpos($name,'_') !== false) {
$name = str_replace(' ','',ucwords(str_replace('_',' ',$name)));
}
return ucfirst($name);
}
/**
* Load a helper
*
* @param string $name
* @return void
*/
protected static function _loadHelper($name)
{
try {
$class = self::getPluginLoader()->load($name);
} catch (Zend_Loader_PluginLoader_Exception $e) {
require_once 'Zend/Controller/Action/Exception.
PHP';
throw new Zend_Controller_Action_Exception('Action Helper by name ' . $name . ' not found',$e);
}
$helper = new $class();
if (!$helper instanceof Zend_Controller_Action_Helper_Abstract) {
require_once 'Zend/Controller/Action/Exception.
PHP';
throw new Zend_Controller_Action_Exception('Helper name ' . $name . ' -> class ' . $class . ' is not of type Zend_Controller_Action_Helper_Abstract');
}
self::getStack()->push($helper);
}
}
@H_502_0@助手经纪人的常见用法:
@H_502_0@注册一个助手
@H_502_0@1.
PHP;">
Zend_Controller_Action_HelperBroker::addHelper($helper);
@H_502_0@2.通过addPrefix()方法带有一个类前缀参数,用来加入自定义助手类的路径。
要求前缀遵循Zend Framework的类命名惯例。
PHP;">
// Add helpers prefixed with My_Action_Helpers in My/Action/Helpers/
Zend_Controller_Action_HelperBroker::addPrefix('My_Action_Helpers');
@H_502_0@3.使用addPath()方法第一个参数为一个目录,第二个为类前缀(默认为'Zend_Controller_Action_Helper')。
@H_502_0@用来将自己的类前缀映射到指定的目录。
PHP;">
// Add helpers prefixed with Helper in Plugins/Helpers/
Zend_Controller_Action_HelperBroker::addPath('./Plugins/Helpers','Helper');
@H_502_0@
@H_502_0@使用hasHelper($name)方法来判定助手经纪人中是否存在某助手,$name是助手的短名称(去掉前缀的):
PHP;">
// Check if 'redirector' helper is registered with the broker:
if (Zend_Controller_Action_HelperBroker::hasHelper('redirector')) {
echo 'Redirector helper registered';
}
@H_502_0@从助手经纪人中获取助手有两个静态方法:getExistingHelper() 和 getStaticHelper() 。getExistingHelper()将获取助手仅当它以前调用过或者显性地通过助手经纪人注册过,否则就抛出一个异常。getStaticHelper() 的做法和getExistingHelper()一样,但如果还没有注册助手堆栈,它将尝试初始化助手,为获取你要配置的的助手,getStaticHelper()是一个好的选择。
@H_502_0@两个方法都带一个参数,$name,它是助手的短名称(去掉前缀)。
PHP;">
// Check if 'redirector' helper is registered with the broker,and fetch:
if (Zend_Controller_Action_HelperBroker::hasHelper('redirector')) {
$redirector =
Zend_Controller_Action_HelperBroker::getExistingHelper('redirector');
}
// Or,simply retrieve it,not worrying about whether or not it was
// prev
IoUsly registered:
$redirector =
Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
}
@H_502_0@删除助手经纪人中的某个助手,$name是助手的短名称。
PHP;">
// Conditionally remove the 'redirector' helper from the broker:
if (Zend_Controller_Action_HelperBroker::hasHelper('redirector')) {
Zend_Controller_Action_HelperBroker::removeHelper('redirector')
}
@H_502_0@更多关于zend相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》
@H_502_0@希望本文所述对大家PHP程序设计有所帮助。 原文链接:/php/20278.html