/**
 * @see Zend_Controller_Action_Helper_Abstract
 */
require_once 'Zend/Controller/Action/Helper/Abstract.php';
/**
 * Helper for creating URLs for redirects and other tasks
 *
 * @uses    Zend_Controller_Action_Helper_Abstract
 * @category  Zend
 * @package  Zend_Controller
 * @subpackage Zend_Controller_Action_Helper
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license  http://framework.zend.com/license/new-bsd   New BSD License
 */
class Zend_Controller_Action_Helper_Url extends Zend_Controller_Action_Helper_Abstract
{
  /**
   * Create URL based on default route
   *
   * @param string $action
   * @param string $controller
   * @param string $module
   * @param array $params
   * @return string
   */
  public function simple($action, $controller = null, $module = null, array $params = null)
  {
    $request = $this->getRequest();
    if (null === $controller) {
      $controller = $request->getControllerName();
    }
    if (null === $module) {
      $module = $request->getModuleName();
    }
    $url = $controller . '/' . $action;
    if ($module != $this->getFrontController()->getDispatcher()->getDefaultModule()) {
      $url = $module . '/' . $url;
    }
    if ('' !== ($baseUrl = $this->getFrontController()->getBaseUrl())) {
      $url = $baseUrl . '/' . $url;
    }
    if (null !== $params) {
      $paramPairs = array();
      foreach ($params as $key => $value) {
        $paramPairs[] = urlencode($key) . '/' . urlencode($value);
      }
      $paramString = implode('/', $paramPairs);
      $url .= '/' . $paramString;
    }
    $url = '/' . ltrim($url, '/');
    return $url;
  }
  /**
   * Assembles a URL based on a given route
   *
   * This method will typically be used for more complex operations, as it
   * ties into the route objects registered with the router.
   *
   * @param array  $urlOptions Options passed to the assemble method of the Route object.
   * @param mixed  $name    The name of a Route to use. If null it will use the current Route
   * @param boolean $reset
   * @param boolean $encode
   * @return string Url for the link href attribute.
   */
  public function url($urlOptions = array(), $name = null, $reset = false, $encode = true)
  {
    $router = $this->getFrontController()->getRouter();
    return $router->assemble($urlOptions, $name, $reset, $encode);
  }
  /**
   * Perform helper when called as $this->_helper->url() from an action controller
   *
   * Proxies to {@link simple()}
   *
   * @param string $action
   * @param string $controller
   * @param string $module
   * @param array $params
   * @return string
   */
  public function direct($action, $controller = null, $module = null, array $params = null)
  {
    return $this->simple($action, $controller, $module, $params);
  }
}

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

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

Url主要用于创建url;

您可能感兴趣的文章:

  • Zend
    Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
  • Zend
    Framework教程之动作的基类Zend_Controller_Action详解
  • Zend
    Framework动作助手Redirector用法实例详解
  • Zend
    Framework动作助手Url用法详解
  • Zend
    Framework动作助手Json用法实例分析
  • Zend
    Framework动作助手FlashMessenger用法详解
  • Zend
    Framework动作助手(Zend_Controller_Action_Helper)用法详解
  • Zend
    Framework教程之前端控制器Zend_Controller_Front用法详解
  • php入门教程之Zend
    Studio设置与开发实例
  • Zend
    Framework开发入门经典教程
  • Zend
    Framework动作控制器用法示例

FlashMessenger简单用法

www.localzend.com/helper_demo1/public/index

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

<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category  Zend
 * @package  Zend_Controller
 * @subpackage Zend_Controller_Action_Helper
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license  http://framework.zend.com/license/new-bsd   New BSD License
 */
/**
 * @see Zend_Session
 */
require_once 'Zend/Session.php';
/**
 * @see Zend_Controller_Action_Helper_Abstract
 */
require_once 'Zend/Controller/Action/Helper/Abstract.php';
/**
 * Flash Messenger - implement session-based messages
 *
 * @uses    Zend_Controller_Action_Helper_Abstract
 * @category  Zend
 * @package  Zend_Controller
 * @subpackage Zend_Controller_Action_Helper
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license  http://framework.zend.com/license/new-bsd   New BSD License
 * @version  $Id: FlashMessenger.php 23775 2011-03-01 17:25:24Z ralph $
 */
class Zend_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_Abstract implements IteratorAggregate, Countable
{
  /**
   * $_messages - Messages from previous request
   *
   * @var array
   */
  static protected $_messages = array();
  /**
   * $_session - Zend_Session storage object
   *
   * @var Zend_Session
   */
  static protected $_session = null;
  /**
   * $_messageAdded - Wether a message has been previously added
   *
   * @var boolean
   */
  static protected $_messageAdded = false;
  /**
   * $_namespace - Instance namespace, default is 'default'
   *
   * @var string
   */
  protected $_namespace = 'default';
  /**
   * __construct() - Instance constructor, needed to get iterators, etc
   *
   * @param string $namespace
   * @return void
   */
  public function __construct()
  {
    if (!self::$_session instanceof Zend_Session_Namespace) {
      self::$_session = new Zend_Session_Namespace($this->getName());
      foreach (self::$_session as $namespace => $messages) {
        self::$_messages[$namespace] = $messages;
        unset(self::$_session->{$namespace});
      }
    }
  }
  /**
   * postDispatch() - runs after action is dispatched, in this
   * case, it is resetting the namespace in case we have forwarded to a different
   * action, Flashmessage will be 'clean' (default namespace)
   *
   * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
   */
  public function postDispatch()
  {
    $this->resetNamespace();
    return $this;
  }
  /**
   * setNamespace() - change the namespace messages are added to, useful for
   * per action controller messaging between requests
   *
   * @param string $namespace
   * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
   */
  public function setNamespace($namespace = 'default')
  {
    $this->_namespace = $namespace;
    return $this;
  }
  /**
   * resetNamespace() - reset the namespace to the default
   *
   * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
   */
  public function resetNamespace()
  {
    $this->setNamespace();
    return $this;
  }
  /**
   * addMessage() - Add a message to flash message
   *
   * @param string $message
   * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
   */
  public function addMessage($message)
  {
    if (self::$_messageAdded === false) {
      self::$_session->setExpirationHops(1, null, true);
    }
    if (!is_array(self::$_session->{$this->_namespace})) {
      self::$_session->{$this->_namespace} = array();
    }
    self::$_session->{$this->_namespace}[] = $message;
    return $this;
  }
  /**
   * hasMessages() - Wether a specific namespace has messages
   *
   * @return boolean
   */
  public function hasMessages()
  {
    return isset(self::$_messages[$this->_namespace]);
  }
  /**
   * getMessages() - Get messages from a specific namespace
   *
   * @return array
   */
  public function getMessages()
  {
    if ($this->hasMessages()) {
      return self::$_messages[$this->_namespace];
    }
    return array();
  }
  /**
   * Clear all messages from the previous request & current namespace
   *
   * @return boolean True if messages were cleared, false if none existed
   */
  public function clearMessages()
  {
    if ($this->hasMessages()) {
      unset(self::$_messages[$this->_namespace]);
      return true;
    }
    return false;
  }
  /**
   * hasCurrentMessages() - check to see if messages have been added to current
   * namespace within this request
   *
   * @return boolean
   */
  public function hasCurrentMessages()
  {
    return isset(self::$_session->{$this->_namespace});
  }
  /**
   * getCurrentMessages() - get messages that have been added to the current
   * namespace within this request
   *
   * @return array
   */
  public function getCurrentMessages()
  {
    if ($this->hasCurrentMessages()) {
      return self::$_session->{$this->_namespace};
    }
    return array();
  }
  /**
   * clear messages from the current request & current namespace
   *
   * @return boolean
   */
  public function clearCurrentMessages()
  {
    if ($this->hasCurrentMessages()) {
      unset(self::$_session->{$this->_namespace});
      return true;
    }
    return false;
  }
  /**
   * getIterator() - complete the IteratorAggregate interface, for iterating
   *
   * @return ArrayObject
   */
  public function getIterator()
  {
    if ($this->hasMessages()) {
      return new ArrayObject($this->getMessages());
    }
    return new ArrayObject();
  }
  /**
   * count() - Complete the countable interface
   *
   * @return int
   */
  public function count()
  {
    if ($this->hasMessages()) {
      return count($this->getMessages());
    }
    return 0;
  }
  /**
   * Strategy pattern: proxy to addMessage()
   *
   * @param string $message
   * @return void
   */
  public function direct($message)
  {
    return $this->addMessage($message);
  }
}

实现源码如下:

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

有这么一种场景,在用户注册成功后,需要在提示页面上显示用户的名称,如果不通过get传递请求,当然你也可以通过session传递

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

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

访问
跳转到

本文实例讲述了Zend
Framework动作助手Url用法。分享给大家供大家参考,具体如下:

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

要显示的用户名称。但是seesion的操作难免复杂,可以使用Flash
Messenger快速的实现这个需求。

您可能感兴趣的文章:

  • Zend
    Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
  • Zend
    Framework教程之动作的基类Zend_Controller_Action详解
  • Zend
    Framework动作助手Redirector用法实例详解
  • Zend
    Framework动作助手Json用法实例分析
  • Zend
    Framework动作助手FlashMessenger用法详解
  • Zend
    Framework创建自己的动作助手详解
  • Zend
    Framework动作助手(Zend_Controller_Action_Helper)用法详解
  • Zend
    Framework教程之前端控制器Zend_Controller_Front用法详解
  • php入门教程之Zend
    Studio设置与开发实例
  • Zend
    Framework开发入门经典教程
  • Zend
    Framework动作控制器用法示例

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

<?php
class UserController extends Zend_Controller_Action
{
  protected $_flashMessenger = null;
  public function init()
  {
    $this->_flashMessenger =
    $this->_helper->getHelper('FlashMessenger');
    $this->initView();
  }
  public function registerAction()
  {
    $this->_flashMessenger->addMessage('xxxxx,Welcome!');
    $this->_helper->redirector('regtips');
  }
  public function regtipsAction()
  {
    $this->view->messages = $this->_flashMessenger->getMessages();
  }
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend
FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

FlashMessager实现源码如下

public function simple($action, $controller = null, $module = null, array $params = null)
public function url($urlOptions = array(), $name = null, $reset = false, $encode = true)
public function direct($action, $controller = null, $module = null, array $params = null)



<?php
class IndexController extends Zend_Controller_Action
{
  public function init()
  {
    /* Initialize action controller here */
  }
  public function indexAction()
  {
    //$urlParser = $this->_helper->getHelper('UrlParser');
    //var_dump($urlParser->parse('//www.jb51.net/article/80479.htm'));
    $url = $this->_helper->getHelper('Url');
    $action = 'actionname';
    $controller = 'controllername';
    $module = 'modulename';
    $params = array('param1'=>'中文参数');
    var_dump($url->simple($action, $controller, $module, $params));
    $urlOptions = array(
        'action'=>$action,
        'controller'=>$controller,
        'module'=>$module,
        'params'=>$params);
    var_dump($url->url($urlOptions));
    var_dump($url->direct($action, $controller, $module, $params));
    exit;
  }
}

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

新增/helper_demo1/application/controllers/UserController.php

string(101)
“/helper_demo1/public/modulename/controllername/actionname/param1/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0”
string(101)
“/helper_demo1/public/modulename/controllername/actionname/params/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0”
string(101)
“/helper_demo1/public/modulename/controllername/actionname/param1/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0”

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

FlashMessenger也是使用Zend_Session_Namespace来存储消息以备将来或下个请求来读取,但是相对简单一些。

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend
FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程yzc579亚洲城官网,》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<?php
var_dump($this->messages);
?>
</body>
</html>

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

FlashMessenger助手允许你传递用户可能需要在下个请求看到的消息。

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

本文实例讲述了Zend
Framework动作助手FlashMessenger用法。分享给大家供大家参考,具体如下:

<?php
/**
 * @see Zend_Controller_Action
 */
require_once 'Zend/Controller/Action.php';
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, '_') !== false) {
      $helperName = strrchr($fullClassName, '_');
      return ltrim($helperName, '_');
    } elseif (strpos($fullClassName, '\\') !== false) {
      $helperName = strrchr($fullClassName, '\\');
      return ltrim($helperName, '\\');
    } else {
      return $fullClassName;
    }
  }
}

在helper_demo1项目的基础上

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

FlashMessenger 用来处理Flash
Messenger会话;FlashMessenger是一个神奇的助手。

<?php
require_once 'Zend/Controller/Action/Helper/Abstract.php';
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);
  }
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend
FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

您可能感兴趣的文章:

  • Zend
    Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
  • Zend
    Framework教程之动作的基类Zend_Controller_Action详解
  • Zend
    Framework动作助手Redirector用法实例详解
  • Zend
    Framework动作助手Url用法详解
  • Zend
    Framework动作助手Json用法实例分析
  • Zend
    Framework创建自己的动作助手详解
  • Zend
    Framework动作助手(Zend_Controller_Action_Helper)用法详解
  • Zend
    Framework教程之前端控制器Zend_Controller_Front用法详解
  • php入门教程之Zend
    Studio设置与开发实例
  • Zend
    Framework开发入门经典教程
  • Zend
    Framework动作控制器用法示例

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

新增/helper_demo1/application/views/scripts/user/regtips.phtml

类的源代码如下:

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

Author

发表评论

电子邮件地址不会被公开。 必填项已用*标注