1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: URI.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class URI
extends Router
{
15 * Returns a singleton instance of URI.
19 public static function instance()
23 if ($instance == NULL)
25 // Initialize the URI instance
33 * Retrieve a specific URI segment.
35 * @param integer|string segment number or label
36 * @param mixed default value returned if segment does not exist
39 public function segment($index = 1, $default = FALSE)
41 if (is_string($index))
43 if (($key = array_search($index, self
::$segments)) === FALSE)
49 $index = (int) $index - 1;
51 return isset(self
::$segments[$index]) ? self
::$segments[$index] : $default;
55 * Retrieve a specific routed URI segment.
57 * @param integer|string rsegment number or label
58 * @param mixed default value returned if segment does not exist
61 public function rsegment($index = 1, $default = FALSE)
63 if (is_string($index))
65 if (($key = array_search($index, self
::$rsegments)) === FALSE)
71 $index = (int) $index - 1;
73 return isset(self
::$rsegments[$index]) ? self
::$rsegments[$index] : $default;
77 * Retrieve a specific URI argument.
78 * This is the part of the segments that does not indicate controller or method
80 * @param integer|string argument number or label
81 * @param mixed default value returned if segment does not exist
84 public function argument($index = 1, $default = FALSE)
86 if (is_string($index))
88 if (($key = array_search($index, self
::$arguments)) === FALSE)
94 $index = (int) $index - 1;
96 return isset(self
::$arguments[$index]) ? self
::$arguments[$index] : $default;
100 * Returns an array containing all the URI segments.
102 * @param integer segment offset
103 * @param boolean return an associative array
106 public function segment_array($offset = 0, $associative = FALSE)
108 return $this->build_array(self
::$segments, $offset, $associative);
112 * Returns an array containing all the re-routed URI segments.
114 * @param integer rsegment offset
115 * @param boolean return an associative array
118 public function rsegment_array($offset = 0, $associative = FALSE)
120 return $this->build_array(self
::$rsegments, $offset, $associative);
124 * Returns an array containing all the URI arguments.
126 * @param integer segment offset
127 * @param boolean return an associative array
130 public function argument_array($offset = 0, $associative = FALSE)
132 return $this->build_array(self
::$arguments, $offset, $associative);
136 * Creates a simple or associative array from an array and an offset.
137 * Used as a helper for (r)segment_array and argument_array.
139 * @param array array to rebuild
140 * @param integer offset to start from
141 * @param boolean create an associative array
144 public function build_array($array, $offset = 0, $associative = FALSE)
146 // Prevent the keys from being improperly indexed
147 array_unshift($array, 0);
149 // Slice the array, preserving the keys
150 $array = array_slice($array, $offset +
1, count($array) - 1, TRUE);
152 if ($associative === FALSE)
155 $associative = array();
156 $pairs = array_chunk($array, 2);
158 foreach ($pairs as $pair)
160 // Add the key/value pair to the associative array
161 $associative[$pair[0]] = isset($pair[1]) ?
$pair[1] : '';
168 * Returns the complete URI as a string.
172 public function string()
174 return self
::$current_uri;
178 * Magic method for converting an object to a string.
182 public function __toString()
184 return self
::$current_uri;
188 * Returns the total number of URI segments.
192 public function total_segments()
194 return count(self
::$segments);
198 * Returns the total number of re-routed URI segments.
202 public function total_rsegments()
204 return count(self
::$rsegments);
208 * Returns the total number of URI arguments.
212 public function total_arguments()
214 return count(self
::$arguments);
218 * Returns the last URI segment.
220 * @param mixed default value returned if segment does not exist
223 public function last_segment($default = FALSE)
225 if (($end = $this->total_segments()) < 1)
228 return self
::$segments[$end - 1];
232 * Returns the last re-routed URI segment.
234 * @param mixed default value returned if segment does not exist
237 public function last_rsegment($default = FALSE)
239 if (($end = $this->total_segments()) < 1)
242 return self
::$rsegments[$end - 1];
246 * Returns the path to the current controller (not including the actual
247 * controller), as a web path.
249 * @param boolean return a full url, or only the path specifically
252 public function controller_path($full = TRUE)
254 return ($full) ? url
::site(self
::$controller_path) : self
::$controller_path;
258 * Returns the current controller, as a web path.
260 * @param boolean return a full url, or only the controller specifically
263 public function controller($full = TRUE)
265 return ($full) ? url
::site(self
::$controller_path.self
::$controller) : self
::$controller;
269 * Returns the current method, as a web path.
271 * @param boolean return a full url, or only the method specifically
274 public function method($full = TRUE)
276 return ($full) ? url
::site(self
::$controller_path.self
::$controller.'/'.self
::$method) : self
::$method;