欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Zend Framework动作助手Url用法详解

程序员文章站 2024-04-01 19:56:46
本文实例讲述了zend framework动作助手url用法。分享给大家供大家参考,具体如下: url主要用于创建url; public function si...

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

url主要用于创建url;

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;
  }
}

www.localzend.com/helper_demo1/public/index

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"

实现源码如下:

/**
 * @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);
  }
}

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

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