当前位置:首页 > CMS教程 > 其它CMS > 列表

Zend Framework动作助手Url用法详解

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-13 14:45:31 浏览: 评论:0 

这篇文章主要介绍了Zend Framework动作助手Url用法,结合实例形式分析了动作助手Url的功能,定义与相关使用技巧,需要的朋友可以参考下。

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

Url主要用于创建url;

  1. public function simple($action, $controller = null, $module = null, array $params = null) 
  2. public function url($urlOptions = array(), $name = null, $reset = false, $encode = true) 
  3. public function direct($action, $controller = null, $module = null, array $params = null) 
  4.  
  5. <?php 
  6. class IndexController extends Zend_Controller_Action 
  7. { 
  8.   public function init() 
  9.   { 
  10.     /* Initialize action controller here */ 
  11.   } 
  12.   public function indexAction() 
  13.   { 
  14.     //$urlParser = $this->_helper->getHelper('UrlParser'); 
  15.     //var_dump($urlParser->parse('https://www.jb51.net/article/80479.htm')); 
  16.     $url = $this->_helper->getHelper('Url'); 
  17.     $action = 'actionname'; 
  18.     $controller = 'controllername'; 
  19.     $module = 'modulename'; 
  20.     $params = array('param1'=>'中文参数'); 
  21.     var_dump($url->simple($action, $controller, $module, $params)); 
  22.     $urlOptions = array( 
  23.         'action'=>$action, 
  24.         'controller'=>$controller, 
  25.         'module'=>$module, 
  26.         'params'=>$params); 
  27.     var_dump($url->url($urlOptions)); 
  28.     var_dump($url->direct($action, $controller, $module, $params)); 
  29.     exit; 
  30.   } 
  31. } 
  32. www.phpfensi.com/helper_demo1/public/index 
  33.  
  34. string(101) "/helper_demo1/public/modulename/controllername/actionname/param1/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0" 
  35. string(101) "/helper_demo1/public/modulename/controllername/actionname/params/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0" 
  36. string(101) "/helper_demo1/public/modulename/controllername/actionname/param1/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0" 

实现源码如下:

  1. /** 
  2.  * @see Zend_Controller_Action_Helper_Abstract 
  3.  */ 
  4. require_once 'Zend/Controller/Action/Helper/Abstract.php'; 
  5. /** 
  6.  * Helper for creating URLs for redirects and other tasks 
  7.  * 
  8.  * @uses    Zend_Controller_Action_Helper_Abstract 
  9.  * @category  Zend 
  10.  * @package  Zend_Controller 
  11.  * @subpackage Zend_Controller_Action_Helper 
  12.  * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) 
  13.  * @license  http://framework.zend.com/license/new-bsd   New BSD License 
  14.  */ 
  15. class Zend_Controller_Action_Helper_Url extends Zend_Controller_Action_Helper_Abstract 
  16. { 
  17.   /** 
  18.    * Create URL based on default route 
  19.    * 
  20.    * @param string $action 
  21.    * @param string $controller 
  22.    * @param string $module 
  23.    * @param array $params 
  24.    * @return string 
  25.    */ 
  26.   public function simple($action, $controller = null, $module = null, array $params = null) 
  27.   { 
  28.     $request = $this->getRequest(); 
  29.     if (null === $controller) { 
  30.       $controller = $request->getControllerName(); 
  31.     } 
  32.     if (null === $module) { 
  33.       $module = $request->getModuleName(); 
  34.     } 
  35.     $url = $controller . '/' . $action; 
  36.     if ($module != $this->getFrontController()->getDispatcher()->getDefaultModule()) { 
  37.       $url = $module . '/' . $url; 
  38.     } 
  39.     if ('' !== ($baseUrl = $this->getFrontController()->getBaseUrl())) { 
  40.       $url = $baseUrl . '/' . $url; 
  41.     } 
  42.     if (null !== $params) { 
  43.       $paramPairs = array(); 
  44.       foreach ($params as $key => $value) { 
  45.         $paramPairs[] = urlencode($key) . '/' . urlencode($value); 
  46.       } 
  47.       $paramString = implode('/', $paramPairs); 
  48.       $url .= '/' . $paramString; 
  49.     } 
  50.     $url = '/' . ltrim($url, '/'); 
  51.     return $url; 
  52.   } 
  53.   /** 
  54.    * Assembles a URL based on a given route 
  55.    * 
  56.    * This method will typically be used for more complex operations, as it 
  57.    * ties into the route objects registered with the router. 
  58.    * 
  59.    * @param array  $urlOptions Options passed to the assemble method of the Route object. 
  60.    * @param mixed  $name    The name of a Route to use. If null it will use the current Route 
  61.    * @param boolean $reset 
  62.    * @param boolean $encode 
  63.    * @return string Url for the link href attribute. 
  64.    */ 
  65.   public function url($urlOptions = array(), $name = null, $reset = false, $encode = true) 
  66.   { 
  67.     $router = $this->getFrontController()->getRouter(); 
  68.     return $router->assemble($urlOptions, $name, $reset, $encode); 
  69.   } 
  70.   /** 
  71.    * Perform helper when called as $this->_helper->url() from an action controller 
  72.    * 
  73.    * Proxies to {@link simple()} 
  74.    * 
  75.    * @param string $action 
  76.    * @param string $controller 
  77.    * @param string $module 
  78.    * @param array $params 
  79.    * @return string 
  80.    */ 
  81.   public function direct($action, $controller = null, $module = null, array $params = null) 
  82.   { 
  83.     return $this->simple($action, $controller, $module, $params); 
  84.   } 
  85. }

Tags: Framework动作助手 Url

分享到: