1 <?php defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: URI.php 4072 2009-03-13 17:20:38Z jheathco $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class URI_Core 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, URI::$segments)) === FALSE)
49 $index = (int) $index - 1;
51 return isset(URI::$segments[$index]) ? URI::$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, URI::$rsegments)) === FALSE)
71 $index = (int) $index - 1;
73 return isset(URI::$rsegments[$index]) ? URI::$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, URI::$arguments)) === FALSE)
94 $index = (int) $index - 1;
96 return isset(URI::$arguments[$index]) ? URI::$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(URI::$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(URI::$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(URI::$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 URI::$current_uri;
178 * Magic method for converting an object to a string.
182 public function __toString()
184 return URI::$current_uri;
188 * Returns the total number of URI segments.
192 public function total_segments()
194 return count(URI::$segments);
198 * Returns the total number of re-routed URI segments.
202 public function total_rsegments()
204 return count(URI::$rsegments);
208 * Returns the total number of URI arguments.
212 public function total_arguments()
214 return count(URI::$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 URI::$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 URI::$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(URI::$controller_path) : URI::$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(URI::$controller_path.URI::$controller) : URI::$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(URI::$controller_path.URI::$controller.'/'.URI::$method) : URI::$method;