BaseUrl.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** @see Zend_View_Helper_Abstract */
  23. require_once 'Zend/View/Helper/Abstract.php';
  24. /**
  25. * Helper for retrieving the BaseUrl
  26. *
  27. * @package Zend_View
  28. * @subpackage Helper
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract
  33. {
  34. /**
  35. * BaseUrl
  36. *
  37. * @var string
  38. */
  39. protected $_baseUrl;
  40. /**
  41. * Returns site's base url, or file with base url prepended
  42. *
  43. * $file is appended to the base url for simplicity
  44. *
  45. * @param string|null $file
  46. * @return string
  47. */
  48. public function baseUrl($file = null)
  49. {
  50. // Get baseUrl
  51. $baseUrl = $this->getBaseUrl();
  52. // Remove trailing slashes
  53. if (null !== $file) {
  54. $file = '/' . ltrim($file, '/\\');
  55. }
  56. return $baseUrl . $file;
  57. }
  58. /**
  59. * Set BaseUrl
  60. *
  61. * @param string $base
  62. * @return Zend_View_Helper_BaseUrl
  63. */
  64. public function setBaseUrl($base)
  65. {
  66. $this->_baseUrl = rtrim($base, '/\\');
  67. return $this;
  68. }
  69. /**
  70. * Get BaseUrl
  71. *
  72. * @return string
  73. */
  74. public function getBaseUrl()
  75. {
  76. if ($this->_baseUrl === null) {
  77. /** @see Zend_Controller_Front */
  78. require_once 'Zend/Controller/Front.php';
  79. $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
  80. // Remove scriptname, eg. index.php from baseUrl
  81. $baseUrl = $this->_removeScriptName($baseUrl);
  82. $this->setBaseUrl($baseUrl);
  83. }
  84. return $this->_baseUrl;
  85. }
  86. /**
  87. * Remove Script filename from baseurl
  88. *
  89. * @param string $url
  90. * @return string
  91. */
  92. protected function _removeScriptName($url)
  93. {
  94. if (!isset($_SERVER['SCRIPT_NAME'])) {
  95. // We can't do much now can we? (Well, we could parse out by ".")
  96. return $url;
  97. }
  98. if (($pos = strripos($url, basename($_SERVER['SCRIPT_NAME']))) !== false) {
  99. $url = substr($url, 0, $pos);
  100. }
  101. return $url;
  102. }
  103. }