程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> Zend Framework動作助手Url用法詳解,zendframework

Zend Framework動作助手Url用法詳解,zendframework

編輯:關於PHP編程

Zend Framework動作助手Url用法詳解,zendframework


本文實例講述了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('http://www.bkjia.com/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程序設計有所幫助。

您可能感興趣的文章:

  • Zend Framework教程之MVC框架的Controller用法分析
  • Zend Framework教程之路由功能Zend_Controller_Router詳解
  • Zend Framework教程之Zend_Controller_Plugin插件用法詳解
  • Zend Framework教程之響應對象的封裝Zend_Controller_Response實例詳解
  • Zend Framework教程之請求對象的封裝Zend_Controller_Request實例詳解
  • Zend Framework教程之動作的基類Zend_Controller_Action詳解
  • Zend Framework教程之分發器Zend_Controller_Dispatcher用法詳解
  • Zend Framework教程之前端控制器Zend_Controller_Front用法詳解
  • Zend Framework動作助手Redirector用法實例詳解
  • Zend Framework動作助手Json用法實例分析
  • Zend Framework動作助手FlashMessenger用法詳解
  • Zend Framework教程之Resource Autoloading用法實例

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved