Url.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Controller_Action_Helper_Abstract
  24. */
  25. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  26. /**
  27. * Helper for creating URLs for redirects and other tasks
  28. *
  29. * @uses Zend_Controller_Action_Helper_Abstract
  30. * @category Zend
  31. * @package Zend_Controller
  32. * @subpackage Zend_Controller_Action_Helper
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Controller_Action_Helper_Url extends Zend_Controller_Action_Helper_Abstract
  37. {
  38. /**
  39. * Create URL based on default route
  40. *
  41. * @param string $action
  42. * @param string $controller
  43. * @param string $module
  44. * @param array $params
  45. * @return string
  46. */
  47. public function simple($action, $controller = null, $module = null, array $params = null)
  48. {
  49. $request = $this->getRequest();
  50. if (null === $controller) {
  51. $controller = $request->getControllerName();
  52. }
  53. if (null === $module) {
  54. $module = $request->getModuleName();
  55. }
  56. $url = $controller . '/' . $action;
  57. if ($module != $this->getFrontController()->getDispatcher()->getDefaultModule()) {
  58. $url = $module . '/' . $url;
  59. }
  60. if ('' !== ($baseUrl = $this->getFrontController()->getBaseUrl())) {
  61. $url = $baseUrl . '/' . $url;
  62. }
  63. if (null !== $params) {
  64. $paramPairs = array();
  65. foreach ($params as $key => $value) {
  66. $paramPairs[] = urlencode($key) . '/' . urlencode($value);
  67. }
  68. $paramString = implode('/', $paramPairs);
  69. $url .= '/' . $paramString;
  70. }
  71. $url = '/' . ltrim($url, '/');
  72. return $url;
  73. }
  74. /**
  75. * Assembles a URL based on a given route
  76. *
  77. * This method will typically be used for more complex operations, as it
  78. * ties into the route objects registered with the router.
  79. *
  80. * @param array $urlOptions Options passed to the assemble method of the Route object.
  81. * @param mixed $name The name of a Route to use. If null it will use the current Route
  82. * @param boolean $reset
  83. * @param boolean $encode
  84. * @return string Url for the link href attribute.
  85. */
  86. public function url($urlOptions = array(), $name = null, $reset = false, $encode = true)
  87. {
  88. $router = $this->getFrontController()->getRouter();
  89. return $router->assemble($urlOptions, $name, $reset, $encode);
  90. }
  91. /**
  92. * Perform helper when called as $this->_helper->url() from an action controller
  93. *
  94. * Proxies to {@link simple()}
  95. *
  96. * @param string $action
  97. * @param string $controller
  98. * @param string $module
  99. * @param array $params
  100. * @return string
  101. */
  102. public function direct($action, $controller = null, $module = null, array $params = null)
  103. {
  104. return $this->simple($action, $controller, $module, $params);
  105. }
  106. }