无为清净楼资源网 Design By www.qnjia.com

本文实例讲述了Zend Framework教程之Zend_Controller_Plugin插件用法。分享给大家供大家参考,具体如下:

通过Zend_Controller_Plugin可以向前端控制器增加附加的功能。便于w一些特殊功能。以下是Zend_Controller_Plugin的简单介绍。

Zend_Controller_Plugin的基本实现

├── Plugin
│   ├── Abstract.php
│   ├── ActionStack.php
│   ├── Broker.php
│   ├── ErrorHandler.php
│   └── PutHandler.php

Zend_Controller_Plugin_Abstract

abstract class Zend_Controller_Plugin_Abstract
{
 protected $_request;
 protected $_response;
 public function setRequest(Zend_Controller_Request_Abstract $request)
 {
  $this->_request = $request;
  return $this;
 }
 public function getRequest()
 {
  return $this->_request;
 }
 public function setResponse(Zend_Controller_Response_Abstract $response)
 {
  $this->_response = $response;
  return $this;
 }
 public function getResponse()
 {
  return $this->_response;
 }
 /**
  * Called before Zend_Controller_Front begins evaluating the
  * request against its routes.
  *
  * @param Zend_Controller_Request_Abstract $request
  * @return void
  */
 public function routeStartup(Zend_Controller_Request_Abstract $request)
 {}
 /**
  * Called after Zend_Controller_Router exits.
  *
  * Called after Zend_Controller_Front exits from the router.
  *
  * @param Zend_Controller_Request_Abstract $request
  * @return void
  */
 public function routeShutdown(Zend_Controller_Request_Abstract $request)
 {}
 /**
  * Called before Zend_Controller_Front enters its dispatch loop.
  *
  * @param Zend_Controller_Request_Abstract $request
  * @return void
  */
 public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
 {}
 /**
  * Called before an action is dispatched by Zend_Controller_Dispatcher.
  *
  * This callback allows for proxy or filter behavior. By altering the
  * request and resetting its dispatched flag (via
  * {@link Zend_Controller_Request_Abstract::setDispatched() setDispatched(false)}),
  * the current action may be skipped.
  *
  * @param Zend_Controller_Request_Abstract $request
  * @return void
  */
 public function preDispatch(Zend_Controller_Request_Abstract $request)
 {}
 /**
  * Called after an action is dispatched by Zend_Controller_Dispatcher.
  *
  * This callback allows for proxy or filter behavior. By altering the
  * request and resetting its dispatched flag (via
  * {@link Zend_Controller_Request_Abstract::setDispatched() setDispatched(false)}),
  * a new action may be specified for dispatching.
  *
  * @param Zend_Controller_Request_Abstract $request
  * @return void
  */
 public function postDispatch(Zend_Controller_Request_Abstract $request)
 {}
 /**
  * Called before Zend_Controller_Front exits its dispatch loop.
  *
  * @return void
  */
 public function dispatchLoopShutdown()
 {}
}

Zend_Controller_Plugin_Abstract声明定义了Zend_Controller运行过程中的几个关键事件位置。用户可以通过指定的方法,对指定位置的请求和相应对象进行相关操作。

Zend_Controller_Plugin_Abstract中方法的描述如下:

routeStartup() 在 Zend_Controller_Front 向注册的 路由器 发送请求前被调用。
routeShutdown()在 路由器 完成请求的路由后被调用。
dispatchLoopStartup() 在 Zend_Controller_Front 进入其分发循环(dispatch loop)前被调用。
preDispatch() 在动作由 分发器 分发前被调用。该回调方法允许代理或者过滤行为。通过修改请求和重设分发标志位(利用 Zend_Controller_Request_Abstract::setDispatched(false) )当前动作可以跳过或者被替换。
postDispatch() 在动作由 分发器 分发后被调用。该回调方法允许代理或者过滤行为。通过修改请求和重设分发标志位(利用 Zend_Controller_Request_Abstract::setDispatched(false) )可以指定新动作进行分发。
dispatchLoopShutdown() 在 Zend_Controller_Front 推出其分发循环后调用。

Zend_Controller_Plugin提供的默认插件:

Zend_Controller_Plugin_Broker:插件经纪人,用于注册,管理自定义的Zend_Controller插件。具体用法,可以参考类代码。
Zend_Controller_Plugin_ActionStack:用于管理动作堆栈。具体用法,可以参考类代码。
Zend_Controller_Plugin_ErrorHandler:用来处理抛出的异常。具体用法,可以参考类代码。
Zend_Controller_Plugin_PutHandler:用于处理请求操作 PUT 。具体用法,可以参考类代码。

Zend_Controller_Plugin_Broker

<"' . $stackIndex . '" already registered');
   }
   $this->_plugins[$stackIndex] = $plugin;
  } else {
   $stackIndex = count($this->_plugins);
   while (isset($this->_plugins[$stackIndex])) {
    ++$stackIndex;
   }
   $this->_plugins[$stackIndex] = $plugin;
  }
  $request = $this->getRequest();
  if ($request) {
   $this->_plugins[$stackIndex]->setRequest($request);
  }
  $response = $this->getResponse();
  if ($response) {
   $this->_plugins[$stackIndex]->setResponse($response);
  }
  ksort($this->_plugins);
  return $this;
 }
 /**
  * Unregister a plugin.
  *
  * @param string|Zend_Controller_Plugin_Abstract $plugin Plugin object or class name
  * @return Zend_Controller_Plugin_Broker
  */
 public function unregisterPlugin($plugin)
 {
  if ($plugin instanceof Zend_Controller_Plugin_Abstract) {
   // Given a plugin object, find it in the array
   $key = array_search($plugin, $this->_plugins, true);
   if (false === $key) {
    require_once 'Zend/Controller/Exception.php';
    throw new Zend_Controller_Exception('Plugin never registered.');
   }
   unset($this->_plugins[$key]);
  } elseif (is_string($plugin)) {
   // Given a plugin class, find all plugins of that class and unset them
   foreach ($this->_plugins as $key => $_plugin) {
    $type = get_class($_plugin);
    if ($plugin == $type) {
     unset($this->_plugins[$key]);
    }
   }
  }
  return $this;
 }
 /**
  * Is a plugin of a particular class registered"htmlcode">
<"htmlcode">
<"htmlcode">
<"_blank" href="https://www.jb51.net/Special/546.htm">Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

标签:
Zend,Framework,Zend_Controller_Plugin,插件

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?