blob: f0fa04005246b3c5e143ccbf22909cde92e9a152 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#########
2URI Class
3#########
4
Andrey Andreev3a5638b2014-01-03 14:57:03 +02005The URI Class provides methods that help you retrieve information from
Derek Jones8ede1a22011-10-05 13:34:52 -05006your URI strings. If you use URI routing, you can also retrieve
7information about the re-routed segments.
8
9.. note:: This class is initialized automatically by the system so there
10 is no need to do it manually.
11
Andrey Andreevcc042092014-01-03 17:08:27 +020012.. contents::
13 :local:
14
15.. raw:: html
16
17 <div class="custom-index container"></div>
18
Andrey Andreev3a5638b2014-01-03 14:57:03 +020019***************
20Class Reference
21***************
Derek Jones8ede1a22011-10-05 13:34:52 -050022
Andrey Andreev3a5638b2014-01-03 14:57:03 +020023.. class:: CI_URI
Derek Jones8ede1a22011-10-05 13:34:52 -050024
Andrey Andreev3a5638b2014-01-03 14:57:03 +020025 .. method:: segment($n[, $no_result = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050026
Andrey Andreev3a5638b2014-01-03 14:57:03 +020027 :param int $n: Segment index number
28 :param mixed $no_result: What to return if the searched segment is not found
29 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -050030
Andrey Andreev3a5638b2014-01-03 14:57:03 +020031 Permits you to retrieve a specific segment. Where n is the segment
32 number you wish to retrieve. Segments are numbered from left to right.
33 For example, if your full URL is this::
Derek Jones8ede1a22011-10-05 13:34:52 -050034
Andrey Andreev3a5638b2014-01-03 14:57:03 +020035 http://example.com/index.php/news/local/metro/crime_is_up
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Andrey Andreev3a5638b2014-01-03 14:57:03 +020037 The segment numbers would be this:
Derek Jones8ede1a22011-10-05 13:34:52 -050038
Andrey Andreev3a5638b2014-01-03 14:57:03 +020039 #. news
40 #. local
41 #. metro
42 #. crime_is_up
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Andrey Andreev3a5638b2014-01-03 14:57:03 +020044 The optional second parameter defaults to NULL and allows you to set the return value
45 of this method when the requested URI segment is missing.
46 For example, this would tell the method to return the number zero in the event of failure::
Derek Jones8ede1a22011-10-05 13:34:52 -050047
Andrey Andreev3a5638b2014-01-03 14:57:03 +020048 $product_id = $this->uri->segment(3, 0);
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Andrey Andreev3a5638b2014-01-03 14:57:03 +020050 It helps avoid having to write code like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Andrey Andreev3a5638b2014-01-03 14:57:03 +020052 if ($this->uri->segment(3) === FALSE)
53 {
54 $product_id = 0;
55 }
56 else
57 {
58 $product_id = $this->uri->segment(3);
59 }
Derek Jones8ede1a22011-10-05 13:34:52 -050060
Andrey Andreev3a5638b2014-01-03 14:57:03 +020061 .. method:: rsegment($n[, $no_result = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050062
Andrey Andreev3a5638b2014-01-03 14:57:03 +020063 :param int $n: Segment index number
64 :param mixed $no_result: What to return if the searched segment is not found
65 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -050066
Andrey Andreev3a5638b2014-01-03 14:57:03 +020067 This method is identical to ``segment()``, except that it lets you retrieve
68 a specific segment from your re-routed URI in the event you are
69 using CodeIgniter's :doc:`URI Routing <../general/routing>` feature.
Derek Jones8ede1a22011-10-05 13:34:52 -050070
Andrey Andreev3a5638b2014-01-03 14:57:03 +020071 .. method:: slash_segment($n[, $where = 'trailing'])
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Andrey Andreev3a5638b2014-01-03 14:57:03 +020073 :param int $n: Segment index number
74 :param string $where: Where to add the slash ('trailing' or 'leading')
75 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Andrey Andreev3a5638b2014-01-03 14:57:03 +020077 This method is almost identical to ``segment()``, except it
78 adds a trailing and/or leading slash based on the second parameter.
79 If the parameter is not used, a trailing slash added. Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -050080
Andrey Andreev3a5638b2014-01-03 14:57:03 +020081 $this->uri->slash_segment(3);
82 $this->uri->slash_segment(3, 'leading');
83 $this->uri->slash_segment(3, 'both');
Derek Jones8ede1a22011-10-05 13:34:52 -050084
Andrey Andreev3a5638b2014-01-03 14:57:03 +020085 Returns:
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Andrey Andreev3a5638b2014-01-03 14:57:03 +020087 #. segment/
88 #. /segment
89 #. /segment/
Derek Jones8ede1a22011-10-05 13:34:52 -050090
Andrey Andreev3a5638b2014-01-03 14:57:03 +020091 .. method:: slash_rsegment($n[, $where = 'trailing'])
Derek Jones8ede1a22011-10-05 13:34:52 -050092
Andrey Andreev3a5638b2014-01-03 14:57:03 +020093 :param int $n: Segment index number
94 :param string $where: Where to add the slash ('trailing' or 'leading')
95 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Andrey Andreev3a5638b2014-01-03 14:57:03 +020097 This method is identical to ``slash_segment()``, except that it lets you
98 add slashes a specific segment from your re-routed URI in the event you
99 are using CodeIgniter's :doc:`URI Routing <../general/routing>`
100 feature.
Derek Jones8ede1a22011-10-05 13:34:52 -0500101
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200102 .. method:: uri_to_assoc([$n = 3[, $default = array()]])
Derek Jones87d152e2011-10-05 15:32:45 -0500103
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200104 :param int $n: Segment index number
105 :param array $default: Default values
106 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200108 This method lets you turn URI segments into and associative array of
109 key/value pairs. Consider this URI::
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200111 index.php/user/search/name/joe/location/UK/gender/male
Derek Jones87d152e2011-10-05 15:32:45 -0500112
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200113 Using this method you can turn the URI into an associative array with
114 this prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200116 [array]
117 (
118 'name' => 'joe'
119 'location' => 'UK'
120 'gender' => 'male'
121 )
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200123 The first parameter lets you set an offset, which defaults to 3 since your
124 URI will normally contain a controller/method pair in the first and second segments.
125 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200127 $array = $this->uri->uri_to_assoc(3);
128 echo $array['name'];
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200130 The second parameter lets you set default key names, so that the array
131 returned will always contain expected indexes, even if missing from the URI.
132 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200134 $default = array('name', 'gender', 'location', 'type', 'sort');
135 $array = $this->uri->uri_to_assoc(3, $default);
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200137 If the URI does not contain a value in your default, an array index will
138 be set to that name, with a value of NULL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200140 Lastly, if a corresponding value is not found for a given key (if there
141 is an odd number of URI segments) the value will be set to NULL.
Derek Jones87d152e2011-10-05 15:32:45 -0500142
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200143 .. method:: ruri_to_assoc([$n = 3[, $default = array()]])
Derek Jones87d152e2011-10-05 15:32:45 -0500144
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200145 :param int $n: Segment index number
146 :param array $default: Default values
147 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200149 This method is identical to ``uri_to_assoc()``, except that it creates
150 an associative array using the re-routed URI in the event you are using
151 CodeIgniter's :doc:`URI Routing <../general/routing>` feature.
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200153 .. method:: assoc_to_uri($array)
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200155 :param array $array: Input array of key/value pairs
156 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500157
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200158 Takes an associative array as input and generates a URI string from it.
159 The array keys will be included in the string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200161 $array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
162 $str = $this->uri->assoc_to_uri($array);
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200164 // Produces: product/shoes/size/large/color/red
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200166 .. method:: uri_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200168 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200170 Returns a string with the complete URI. For example, if this is your full URL::
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200172 http://example.com/index.php/news/local/345
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200174 The method would return this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200176 news/local/345
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200178 .. method:: ruri_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200180 :returns: string
Derek Jones87d152e2011-10-05 15:32:45 -0500181
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200182 This method is identical to ``uri_string()``, except that it returns
183 the re-routed URI in the event you are using CodeIgniter's :doc:`URI
184 Routing <../general/routing>` feature.
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200186 .. method:: total_segments()
Derek Jones8ede1a22011-10-05 13:34:52 -0500187
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200188 :returns: int
189
190 Returns the total number of segments.
191
192 .. method:: total_rsegments()
193
194 :returns: int
195
196 This method is identical to ``total_segments()``, except that it returns
197 the total number of segments in your re-routed URI in the event you are
198 using CodeIgniter's :doc:`URI Routing <../general/routing>` feature.
199
200 .. method:: segment_array()
201
202 :returns: array
203
204 Returns an array containing the URI segments. For example::
205
206 $segs = $this->uri->segment_array();
207
208 foreach ($segs as $segment)
209 {
210 echo $segment;
211 echo '<br />';
212 }
213
214 .. method:: rsegment_array()
215
216 :returns: array
217
218 This method is identical to ``segment_array()``, except that it returns
219 the array of segments in your re-routed URI in the event you are using
220 CodeIgniter's :doc:`URI Routing <../general/routing>` feature.