首頁 > 後端開發 > php教程 > 關於Zend Framework動作助手Url的用法

關於Zend Framework動作助手Url的用法

不言
發布: 2023-04-01 11:56:01
原創
1576 人瀏覽過

這篇文章主要介紹了Zend Framework動作助手Url用法,結合實例形式分析了動作助手Url的功能,定義與相關使用技巧,需要的朋友可以參考下

本文實例講述了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(&#39;UrlParser&#39;);
    //var_dump($urlParser->parse(&#39;//www.jb51.net/article/80479.htm&#39;));
    $url = $this->_helper->getHelper(&#39;Url&#39;);
    $action = &#39;actionname&#39;;
    $controller = &#39;controllername&#39;;
    $module = &#39;modulename&#39;;
    $params = array(&#39;param1&#39;=>&#39;中文参数&#39;);
    var_dump($url->simple($action, $controller, $module, $params));
    $urlOptions = array(
        &#39;action&#39;=>$action,
        &#39;controller&#39;=>$controller,
        &#39;module&#39;=>$module,
        &#39;params&#39;=>$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/中文參數"

string(101) "/helper_demo1/public/modulename/controllername/actionname/params/中文參數"
string(101) "/helper_demo1/public/modulename/controllername/actionname/param1/中文參數"

#實作原始碼如下:

/**
 * @see Zend_Controller_Action_Helper_Abstract
 */
require_once &#39;Zend/Controller/Action/Helper/Abstract.php&#39;;
/**
 * 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 . &#39;/&#39; . $action;
    if ($module != $this->getFrontController()->getDispatcher()->getDefaultModule()) {
      $url = $module . &#39;/&#39; . $url;
    }
    if (&#39;&#39; !== ($baseUrl = $this->getFrontController()->getBaseUrl())) {
      $url = $baseUrl . &#39;/&#39; . $url;
    }
    if (null !== $params) {
      $paramPairs = array();
      foreach ($params as $key => $value) {
        $paramPairs[] = urlencode($key) . &#39;/&#39; . urlencode($value);
      }
      $paramString = implode(&#39;/&#39;, $paramPairs);
      $url .= &#39;/&#39; . $paramString;
    }
    $url = &#39;/&#39; . ltrim($url, &#39;/&#39;);
    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中文網!

相關推薦:

關於Zend Framework自訂Helper類別的相關知識

關於Zend Framework動作控制器的用法

關於Zend Framework中Zend_Db_Table_Rowset的用法

以上是關於Zend Framework動作助手Url的用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板