4
0

Url.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * La classe Url permet de gérer les URL à travers MINZ
  4. */
  5. class Minz_Url {
  6. /**
  7. * Affiche une Url formatée
  8. * @param $url l'url à formater définie comme un tableau :
  9. * $url['c'] = controller
  10. * $url['a'] = action
  11. * $url['params'] = tableau des paramètres supplémentaires
  12. * ou comme une chaîne de caractère
  13. * @param $encodage pour indiquer comment encoder les & (& ou &amp; pour html)
  14. * @return l'url formatée
  15. */
  16. public static function display ($url = array (), $encodage = 'html', $absolute = false) {
  17. $isArray = is_array($url);
  18. if ($isArray) {
  19. $url = self::checkUrl($url);
  20. }
  21. $url_string = '';
  22. if ($absolute) {
  23. $url_string = Minz_Request::getBaseUrl();
  24. if (strlen($url_string) < strlen('http://a.bc')) {
  25. $url_string = Minz_Request::guessBaseUrl();
  26. if (PUBLIC_RELATIVE === '..') {
  27. //TODO: Implement proper resolver of relative parts such as /test/./../
  28. $url_string = dirname($url_string);
  29. }
  30. }
  31. if ($isArray) {
  32. $url_string .= PUBLIC_TO_INDEX_PATH;
  33. }
  34. if ($absolute === 'root') {
  35. $url_string = parse_url($url_string, PHP_URL_PATH);
  36. }
  37. } else {
  38. $url_string = $isArray ? '.' : PUBLIC_RELATIVE;
  39. }
  40. if ($isArray) {
  41. $url_string .= '/' . self::printUri($url, $encodage);
  42. } elseif ($encodage === 'html') {
  43. $url_string = Minz_Helper::htmlspecialchars_utf8($url_string . $url);
  44. } else {
  45. $url_string .= $url;
  46. }
  47. return $url_string;
  48. }
  49. /**
  50. * Construit l'URI d'une URL
  51. * @param l'url sous forme de tableau
  52. * @param $encodage pour indiquer comment encoder les & (& ou &amp; pour html)
  53. * @return l'uri sous la forme ?key=value&key2=value2
  54. */
  55. private static function printUri($url, $encodage) {
  56. $uri = '';
  57. $separator = '?';
  58. if ($encodage === 'html') {
  59. $and = '&amp;';
  60. } else {
  61. $and = '&';
  62. }
  63. if (isset($url['c'])
  64. && $url['c'] != Minz_Request::defaultControllerName()) {
  65. $uri .= $separator . 'c=' . $url['c'];
  66. $separator = $and;
  67. }
  68. if (isset($url['a'])
  69. && $url['a'] != Minz_Request::defaultActionName()) {
  70. $uri .= $separator . 'a=' . $url['a'];
  71. $separator = $and;
  72. }
  73. if (isset($url['params'])) {
  74. unset($url['params']['c']);
  75. unset($url['params']['a']);
  76. foreach ($url['params'] as $key => $param) {
  77. $uri .= $separator . urlencode($key) . '=' . urlencode($param);
  78. $separator = $and;
  79. }
  80. }
  81. return $uri;
  82. }
  83. /**
  84. * Vérifie que les éléments du tableau représentant une url soit ok
  85. * @param l'url sous forme de tableau (sinon renverra directement $url)
  86. * @return l'url vérifié
  87. */
  88. public static function checkUrl ($url) {
  89. $url_checked = $url;
  90. if (is_array ($url)) {
  91. if (!isset ($url['c'])) {
  92. $url_checked['c'] = Minz_Request::defaultControllerName ();
  93. }
  94. if (!isset ($url['a'])) {
  95. $url_checked['a'] = Minz_Request::defaultActionName ();
  96. }
  97. if (!isset ($url['params'])) {
  98. $url_checked['params'] = array ();
  99. }
  100. }
  101. return $url_checked;
  102. }
  103. }
  104. function _url ($controller, $action) {
  105. $nb_args = func_num_args ();
  106. if($nb_args < 2 || $nb_args % 2 != 0) {
  107. return false;
  108. }
  109. $args = func_get_args ();
  110. $params = array ();
  111. for($i = 2; $i < $nb_args; $i = $i + 2) {
  112. $params[$args[$i]] = $args[$i + 1];
  113. }
  114. return Minz_Url::display (array ('c' => $controller, 'a' => $action, 'params' => $params));
  115. }