>  기사  >  백엔드 개발  >  Zend Framework 액션 어시스턴트 Url의 사용법에 대해

Zend Framework 액션 어시스턴트 Url의 사용법에 대해

不言
不言원래의
2018-06-15 17:10:281508검색

이 글은 주로 Zend Framework Action Assistant Url의 사용법을 소개하고, Action Assistant Url의 기능, 정의, 관련 사용법을 예시 형태로 분석합니다. 도움이 필요한 친구들이 참고할 수 있습니다

이 글은 Zend의 사용법을 설명합니다. 예제가 포함된 프레임워크 작업 도우미 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/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0"
string(101) "/helper_demo1/public/ 모듈 이름/ 컨트롤러 이름/작업 이름/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 &#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 사용자 정의 도우미 클래스 관련 지식

Zend Framework 작업 컨트롤러 사용 정보

Zend Framework의 Zend_Db_Table_Rowset 사용 정보

위 내용은 Zend Framework 액션 어시스턴트 Url의 사용법에 대해의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.