web/lib/Zend/Controller/Action/Helper/Url.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Controller
       
    17  * @subpackage Zend_Controller_Action_Helper
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Url.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Controller_Action_Helper_Abstract
       
    25  */
       
    26 require_once 'Zend/Controller/Action/Helper/Abstract.php';
       
    27 
       
    28 /**
       
    29  * Helper for creating URLs for redirects and other tasks
       
    30  *
       
    31  * @uses       Zend_Controller_Action_Helper_Abstract
       
    32  * @category   Zend
       
    33  * @package    Zend_Controller
       
    34  * @subpackage Zend_Controller_Action_Helper
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Controller_Action_Helper_Url extends Zend_Controller_Action_Helper_Abstract
       
    39 {
       
    40     /**
       
    41      * Create URL based on default route
       
    42      *
       
    43      * @param  string $action
       
    44      * @param  string $controller
       
    45      * @param  string $module
       
    46      * @param  array  $params
       
    47      * @return string
       
    48      */
       
    49     public function simple($action, $controller = null, $module = null, array $params = null)
       
    50     {
       
    51         $request = $this->getRequest();
       
    52 
       
    53         if (null === $controller) {
       
    54             $controller = $request->getControllerName();
       
    55         }
       
    56 
       
    57         if (null === $module) {
       
    58             $module = $request->getModuleName();
       
    59         }
       
    60 
       
    61         $url = $controller . '/' . $action;
       
    62         if ($module != $this->getFrontController()->getDispatcher()->getDefaultModule()) {
       
    63             $url = $module . '/' . $url;
       
    64         }
       
    65 
       
    66         if ('' !== ($baseUrl = $this->getFrontController()->getBaseUrl())) {
       
    67             $url = $baseUrl . '/' . $url;
       
    68         }
       
    69 
       
    70         if (null !== $params) {
       
    71             $paramPairs = array();
       
    72             foreach ($params as $key => $value) {
       
    73                 $paramPairs[] = urlencode($key) . '/' . urlencode($value);
       
    74             }
       
    75             $paramString = implode('/', $paramPairs);
       
    76             $url .= '/' . $paramString;
       
    77         }
       
    78 
       
    79         $url = '/' . ltrim($url, '/');
       
    80 
       
    81         return $url;
       
    82     }
       
    83 
       
    84     /**
       
    85      * Assembles a URL based on a given route
       
    86      *
       
    87      * This method will typically be used for more complex operations, as it
       
    88      * ties into the route objects registered with the router.
       
    89      *
       
    90      * @param  array   $urlOptions Options passed to the assemble method of the Route object.
       
    91      * @param  mixed   $name       The name of a Route to use. If null it will use the current Route
       
    92      * @param  boolean $reset
       
    93      * @param  boolean $encode
       
    94      * @return string Url for the link href attribute.
       
    95      */
       
    96     public function url($urlOptions = array(), $name = null, $reset = false, $encode = true)
       
    97     {
       
    98         $router = $this->getFrontController()->getRouter();
       
    99         return $router->assemble($urlOptions, $name, $reset, $encode);
       
   100     }
       
   101 
       
   102     /**
       
   103      * Perform helper when called as $this->_helper->url() from an action controller
       
   104      *
       
   105      * Proxies to {@link simple()}
       
   106      *
       
   107      * @param  string $action
       
   108      * @param  string $controller
       
   109      * @param  string $module
       
   110      * @param  array  $params
       
   111      * @return string
       
   112      */
       
   113     public function direct($action, $controller = null, $module = null, array $params = null)
       
   114     {
       
   115         return $this->simple($action, $controller, $module, $params);
       
   116     }
       
   117 }