Home >php教程 >PHP开发 >Detailed explanation of creating your own action assistant with Zend Framework

Detailed explanation of creating your own action assistant with Zend Framework

高洛峰
高洛峰Original
2017-01-03 13:32:291150browse

本文实例讲述了Zend Framework创建自己的动作助手实现方法。分享给大家供大家参考,具体如下:

助手的抽象基类是Zend_Controller_Action_Helper_Abstract,如要定义自己的助手,需要继承此类。

类的源代码如下:

<?php
/**
 * @see Zend_Controller_Action
 */
require_once &#39;Zend/Controller/Action.php&#39;;
abstract class Zend_Controller_Action_Helper_Abstract
{
  /**
   * $_actionController
   *
   * @var Zend_Controller_Action $_actionController
   */
  protected $_actionController = null;
  /**
   * @var mixed $_frontController
   */
  protected $_frontController = null;
  /**
   * setActionController()
   *
   * @param Zend_Controller_Action $actionController
   * @return Zend_Controller_ActionHelper_Abstract Provides a fluent interface
   */
  public function setActionController(Zend_Controller_Action $actionController = null)
  {
    $this->_actionController = $actionController;
    return $this;
  }
  /**
   * Retrieve current action controller
   *
   * @return Zend_Controller_Action
   */
  public function getActionController()
  {
    return $this->_actionController;
  }
  /**
   * Retrieve front controller instance
   *
   * @return Zend_Controller_Front
   */
  public function getFrontController()
  {
    return Zend_Controller_Front::getInstance();
  }
  /**
   * Hook into action controller initialization
   *
   * @return void
   */
  public function init()
  {
  }
  /**
   * Hook into action controller preDispatch() workflow
   *
   * @return void
   */
  public function preDispatch()
  {
  }
  /**
   * Hook into action controller postDispatch() workflow
   *
   * @return void
   */
  public function postDispatch()
  {
  }
  /**
   * getRequest() -
   *
   * @return Zend_Controller_Request_Abstract $request
   */
  public function getRequest()
  {
    $controller = $this->getActionController();
    if (null === $controller) {
      $controller = $this->getFrontController();
    }
    return $controller->getRequest();
  }
  /**
   * getResponse() -
   *
   * @return Zend_Controller_Response_Abstract $response
   */
  public function getResponse()
  {
    $controller = $this->getActionController();
    if (null === $controller) {
      $controller = $this->getFrontController();
    }
    return $controller->getResponse();
  }
  /**
   * getName()
   *
   * @return string
   */
  public function getName()
  {
    $fullClassName = get_class($this);
    if (strpos($fullClassName, &#39;_&#39;) !== false) {
      $helperName = strrchr($fullClassName, &#39;_&#39;);
      return ltrim($helperName, &#39;_&#39;);
    } elseif (strpos($fullClassName, &#39;\\&#39;) !== false) {
      $helperName = strrchr($fullClassName, &#39;\\&#39;);
      return ltrim($helperName, &#39;\\&#39;);
    } else {
      return $fullClassName;
    }
  }
}

助手基类提供的常用方法如下:

setActionController() 用来设置当前的动作控制器。
init(),该方法在实例化时由助手经纪人触发,可用来触发助手的初始化过程;
动作链中多个控制器使用相同的助手时,如要恢复状态时将十分有用。
preDispatch()分发动作之前触发。
postDispatch()分发过程结束时触发——即使preDispatch()插件已经跳过了该动作。清理时大量使用。
getRequest() 获取当前的请求对象。
getResponse() 获取当前的响应对象。
getName() 获取助手名。获取了下划线后面的类名部分,没有下划线则获取类的全名。

例如,如果类名为Zend_Controller_Action_Helper_Redirector,他将返回 Redirector,如果类名为FooMessage,将会返回全名。

举例说明自定义动作助手类

作用:解析传入的网址,返回各个部分。使用parse_url解析指定的网址。
用zendstudio新建一个zend framework项目helper_demo1。

新增文件:/helper_demo1/library/Application/Controller/Action/Helpers/UrlParser.php

<?php
require_once &#39;Zend/Controller/Action/Helper/Abstract.php&#39;;
class Application_Controller_Action_Helpers_UrlParser extends Zend_Controller_Action_Helper_Abstract
{
  public function __construct()
  {
  }
  /**
   * Parse url
   *
   * @param String $url
   * @return Array part of url
   */
  public function parse($url)
  {
    return parse_url($url);
  }
}

修改文件:/helper_demo1/application/Bootstrap.php

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
  protected function _initAutoload()
  {
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace(array(&#39;Application_&#39;));
  }
  protected function _initActionHelpers() {
    //用前缀形式
    //Zend_Controller_Action_HelperBroker::addPrefix(&#39;Application_Controller_Action_Helpers&#39;);
    //指定目录和前缀
    //Zend_Controller_Action_HelperBroker::addPath(&#39;/www/helper_demo1/library/Application/Controller/Action/Helpers&#39;,
    //                  &#39;Application_Controller_Action_Helpers&#39;);
    //new一个助手类传入
    Zend_Controller_Action_HelperBroker::addHelper(new Application_Controller_Action_Helpers_UrlParser);
  }
}

修改测试action:/helper_demo1/application/controllers/IndexController.php

<?php
class IndexController extends Zend_Controller_Action
{
  public function init()
  {
    /* Initialize action controller here */
  }
  public function indexAction()
  {
    $urlParser = $this->_helper->getHelper(&#39;UrlParser&#39;);
  var_dump($urlParser->parse(&#39;http://www.jb51.net/article/80479.htm&#39;));
  }
}

以上介绍了自定义动作助手类,以及简单的使用方法。

需要注意的就是什么是助手类的前缀,助手类的名称以及助手的路径。

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

更多Zend Framework创建自己的动作助手详解相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn