Url.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 ($url_string == '') {
  25. $url_string = Minz_Request::guessBaseUrl();
  26. }
  27. if ($isArray) {
  28. $url_string .= PUBLIC_TO_INDEX_PATH;
  29. }
  30. if ($absolute === 'root') {
  31. $url_string = parse_url($url_string, PHP_URL_PATH);
  32. }
  33. } else {
  34. $url_string = $isArray ? '.' : PUBLIC_RELATIVE;
  35. }
  36. if ($isArray) {
  37. $url_string .= self::printUri($url, $encodage);
  38. } elseif ($encodage === 'html') {
  39. $url_string = Minz_Helper::htmlspecialchars_utf8($url_string . $url);
  40. } else {
  41. $url_string .= $url;
  42. }
  43. return $url_string;
  44. }
  45. /**
  46. * Construit l'URI d'une URL
  47. * @param l'url sous forme de tableau
  48. * @param $encodage pour indiquer comment encoder les & (& ou &amp; pour html)
  49. * @return l'uri sous la forme ?key=value&key2=value2
  50. */
  51. private static function printUri($url, $encodage) {
  52. $uri = '';
  53. $separator = '?';
  54. if ($encodage === 'html') {
  55. $and = '&amp;';
  56. } else {
  57. $and = '&';
  58. }
  59. if (isset($url['c'])
  60. && $url['c'] != Minz_Request::defaultControllerName()) {
  61. $uri .= $separator . 'c=' . $url['c'];
  62. $separator = $and;
  63. }
  64. if (isset($url['a'])
  65. && $url['a'] != Minz_Request::defaultActionName()) {
  66. $uri .= $separator . 'a=' . $url['a'];
  67. $separator = $and;
  68. }
  69. if (isset($url['params'])) {
  70. foreach ($url['params'] as $key => $param) {
  71. $uri .= $separator . urlencode($key) . '=' . urlencode($param);
  72. $separator = $and;
  73. }
  74. }
  75. return $uri;
  76. }
  77. /**
  78. * Vérifie que les éléments du tableau représentant une url soit ok
  79. * @param l'url sous forme de tableau (sinon renverra directement $url)
  80. * @return l'url vérifié
  81. */
  82. public static function checkUrl ($url) {
  83. $url_checked = $url;
  84. if (is_array ($url)) {
  85. if (!isset ($url['c'])) {
  86. $url_checked['c'] = Minz_Request::defaultControllerName ();
  87. }
  88. if (!isset ($url['a'])) {
  89. $url_checked['a'] = Minz_Request::defaultActionName ();
  90. }
  91. if (!isset ($url['params'])) {
  92. $url_checked['params'] = array ();
  93. }
  94. }
  95. return $url_checked;
  96. }
  97. }
  98. function _url ($controller, $action) {
  99. $nb_args = func_num_args ();
  100. if($nb_args < 2 || $nb_args % 2 != 0) {
  101. return false;
  102. }
  103. $args = func_get_args ();
  104. $params = array ();
  105. for($i = 2; $i < $nb_args; $i = $i + 2) {
  106. $params[$args[$i]] = $args[$i + 1];
  107. }
  108. return Minz_Url::display (array ('c' => $controller, 'a' => $action, 'params' => $params));
  109. }