Detailed explanation of Zend Framework action assistant Url usage, zendframework_PHP tutorial

WBOY
Release: 2016-07-12 08:57:36
Original
767 people have browsed it

Detailed explanation of the usage of Zend Framework action assistant Url, zendframework

The example in this article describes the usage of Zend Framework action assistant Url. Share it with everyone for your reference, the details are as follows:

Url is mainly used to create URLs;

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)

Copy after login
<&#63;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;
  }
}

Copy after login

www.localzend.com/helper_demo1/public/index

string(101) "/helper_demo1/public/modulename/controllername/actionname/param1/Chinese parameter"
string(101) "/helper_demo1/public/modulename/controllername/actionname/params/Chinese parameters"
string(101) "/helper_demo1/public/modulename/controllername/actionname/param1/Chinese parameter"

The implementation source code is as follows:

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

Copy after login

Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Analysis of Controller Usage of MVC Framework in Zend Framework Tutorial
  • Zend Framework Tutorial Road explained in detail by function Zend_Controller_Router
  • Zend Framework tutorial: Detailed explanation of Zend_Controller_Plugin plug-in usage
  • Zend Framework tutorial: Detailed explanation of the encapsulation of the response object Zend_Controller_Response instance
  • Zend Framework tutorial: Detailed explanation of the encapsulation of the request object Zend_Controller_Request instance
  • Zend Framework tutorial Detailed explanation of the action's base class Zend_Controller_Action
  • Zend Framework tutorial's detailed explanation of the usage of the distributor Zend_Controller_Dispatcher
  • Zend Framework tutorial's detailed explanation of the usage of the front-end controller Zend_Controller_Front
  • Zend Framework action assistant Redirector usage Detailed explanation of examples
  • Zend Framework action assistant Json usage example analysis
  • Detailed explanation of Zend Framework action assistant FlashMessenger usage
  • Resource Autoloading usage example of Zend Framework tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1106898.htmlTechArticleDetailed explanation of the usage of Zend Framework action assistant Url, zendframework This article describes the usage of Zend Framework action assistant Url. Share it with everyone for your reference, the details are as follows: Url is mainly used to create...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!