blob: 1256afdefe4c9b7c316df91cc9376645083ccbde [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 Andreev3a5638b2014-01-03 14:57:03 +020012***************
13Class Reference
14***************
Derek Jones8ede1a22011-10-05 13:34:52 -050015
Andrey Andreev3a5638b2014-01-03 14:57:03 +020016.. class:: CI_URI
Derek Jones8ede1a22011-10-05 13:34:52 -050017
Andrey Andreev3a5638b2014-01-03 14:57:03 +020018 .. method:: segment($n[, $no_result = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050019
Andrey Andreev3a5638b2014-01-03 14:57:03 +020020 :param int $n: Segment index number
21 :param mixed $no_result: What to return if the searched segment is not found
22 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -050023
Andrey Andreev3a5638b2014-01-03 14:57:03 +020024 Permits you to retrieve a specific segment. Where n is the segment
25 number you wish to retrieve. Segments are numbered from left to right.
26 For example, if your full URL is this::
Derek Jones8ede1a22011-10-05 13:34:52 -050027
Andrey Andreev3a5638b2014-01-03 14:57:03 +020028 http://example.com/index.php/news/local/metro/crime_is_up
Derek Jones8ede1a22011-10-05 13:34:52 -050029
Andrey Andreev3a5638b2014-01-03 14:57:03 +020030 The segment numbers would be this:
Derek Jones8ede1a22011-10-05 13:34:52 -050031
Andrey Andreev3a5638b2014-01-03 14:57:03 +020032 #. news
33 #. local
34 #. metro
35 #. crime_is_up
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Andrey Andreev3a5638b2014-01-03 14:57:03 +020037 The optional second parameter defaults to NULL and allows you to set the return value
38 of this method when the requested URI segment is missing.
39 For example, this would tell the method to return the number zero in the event of failure::
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Andrey Andreev3a5638b2014-01-03 14:57:03 +020041 $product_id = $this->uri->segment(3, 0);
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Andrey Andreev3a5638b2014-01-03 14:57:03 +020043 It helps avoid having to write code like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050044
Andrey Andreev3a5638b2014-01-03 14:57:03 +020045 if ($this->uri->segment(3) === FALSE)
46 {
47 $product_id = 0;
48 }
49 else
50 {
51 $product_id = $this->uri->segment(3);
52 }
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Andrey Andreev3a5638b2014-01-03 14:57:03 +020054 .. method:: rsegment($n[, $no_result = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050055
Andrey Andreev3a5638b2014-01-03 14:57:03 +020056 :param int $n: Segment index number
57 :param mixed $no_result: What to return if the searched segment is not found
58 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -050059
Andrey Andreev3a5638b2014-01-03 14:57:03 +020060 This method is identical to ``segment()``, except that it lets you retrieve
61 a specific segment from your re-routed URI in the event you are
62 using CodeIgniter's :doc:`URI Routing <../general/routing>` feature.
Derek Jones8ede1a22011-10-05 13:34:52 -050063
Andrey Andreev3a5638b2014-01-03 14:57:03 +020064 .. method:: slash_segment($n[, $where = 'trailing'])
Derek Jones8ede1a22011-10-05 13:34:52 -050065
Andrey Andreev3a5638b2014-01-03 14:57:03 +020066 :param int $n: Segment index number
67 :param string $where: Where to add the slash ('trailing' or 'leading')
68 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -050069
Andrey Andreev3a5638b2014-01-03 14:57:03 +020070 This method is almost identical to ``segment()``, except it
71 adds a trailing and/or leading slash based on the second parameter.
72 If the parameter is not used, a trailing slash added. Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -050073
Andrey Andreev3a5638b2014-01-03 14:57:03 +020074 $this->uri->slash_segment(3);
75 $this->uri->slash_segment(3, 'leading');
76 $this->uri->slash_segment(3, 'both');
Derek Jones8ede1a22011-10-05 13:34:52 -050077
Andrey Andreev3a5638b2014-01-03 14:57:03 +020078 Returns:
Derek Jones8ede1a22011-10-05 13:34:52 -050079
Andrey Andreev3a5638b2014-01-03 14:57:03 +020080 #. segment/
81 #. /segment
82 #. /segment/
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Andrey Andreev3a5638b2014-01-03 14:57:03 +020084 .. method:: slash_rsegment($n[, $where = 'trailing'])
Derek Jones8ede1a22011-10-05 13:34:52 -050085
Andrey Andreev3a5638b2014-01-03 14:57:03 +020086 :param int $n: Segment index number
87 :param string $where: Where to add the slash ('trailing' or 'leading')
88 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -050089
Andrey Andreev3a5638b2014-01-03 14:57:03 +020090 This method is identical to ``slash_segment()``, except that it lets you
91 add slashes a specific segment from your re-routed URI in the event you
92 are using CodeIgniter's :doc:`URI Routing <../general/routing>`
93 feature.
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Andrey Andreev3a5638b2014-01-03 14:57:03 +020095 .. method:: uri_to_assoc([$n = 3[, $default = array()]])
Derek Jones87d152e2011-10-05 15:32:45 -050096
Andrey Andreev3a5638b2014-01-03 14:57:03 +020097 :param int $n: Segment index number
98 :param array $default: Default values
99 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200101 This method lets you turn URI segments into and associative array of
102 key/value pairs. Consider this URI::
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200104 index.php/user/search/name/joe/location/UK/gender/male
Derek Jones87d152e2011-10-05 15:32:45 -0500105
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200106 Using this method you can turn the URI into an associative array with
107 this prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200109 [array]
110 (
111 'name' => 'joe'
112 'location' => 'UK'
113 'gender' => 'male'
114 )
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200116 The first parameter lets you set an offset, which defaults to 3 since your
117 URI will normally contain a controller/method pair in the first and second segments.
118 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200120 $array = $this->uri->uri_to_assoc(3);
121 echo $array['name'];
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200123 The second parameter lets you set default key names, so that the array
124 returned will always contain expected indexes, even if missing from the URI.
125 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200127 $default = array('name', 'gender', 'location', 'type', 'sort');
128 $array = $this->uri->uri_to_assoc(3, $default);
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200130 If the URI does not contain a value in your default, an array index will
131 be set to that name, with a value of NULL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200133 Lastly, if a corresponding value is not found for a given key (if there
134 is an odd number of URI segments) the value will be set to NULL.
Derek Jones87d152e2011-10-05 15:32:45 -0500135
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200136 .. method:: ruri_to_assoc([$n = 3[, $default = array()]])
Derek Jones87d152e2011-10-05 15:32:45 -0500137
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200138 :param int $n: Segment index number
139 :param array $default: Default values
140 :returns: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200142 This method is identical to ``uri_to_assoc()``, except that it creates
143 an associative array using the re-routed URI in the event you are using
144 CodeIgniter's :doc:`URI Routing <../general/routing>` feature.
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200146 .. method:: assoc_to_uri($array)
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200148 :param array $array: Input array of key/value pairs
149 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200151 Takes an associative array as input and generates a URI string from it.
152 The array keys will be included in the string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200154 $array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
155 $str = $this->uri->assoc_to_uri($array);
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200157 // Produces: product/shoes/size/large/color/red
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200159 .. method:: uri_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200161 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200163 Returns a string with the complete URI. For example, if this is your full URL::
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200165 http://example.com/index.php/news/local/345
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200167 The method would return this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200169 news/local/345
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200171 .. method:: ruri_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200173 :returns: string
Derek Jones87d152e2011-10-05 15:32:45 -0500174
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200175 This method is identical to ``uri_string()``, except that it returns
176 the re-routed URI in the event you are using CodeIgniter's :doc:`URI
177 Routing <../general/routing>` feature.
Derek Jones8ede1a22011-10-05 13:34:52 -0500178
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200179 .. method:: total_segments()
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200181 :returns: int
182
183 Returns the total number of segments.
184
185 .. method:: total_rsegments()
186
187 :returns: int
188
189 This method is identical to ``total_segments()``, except that it returns
190 the total number of segments in your re-routed URI in the event you are
191 using CodeIgniter's :doc:`URI Routing <../general/routing>` feature.
192
193 .. method:: segment_array()
194
195 :returns: array
196
197 Returns an array containing the URI segments. For example::
198
199 $segs = $this->uri->segment_array();
200
201 foreach ($segs as $segment)
202 {
203 echo $segment;
204 echo '<br />';
205 }
206
207 .. method:: rsegment_array()
208
209 :returns: array
210
211 This method is identical to ``segment_array()``, except that it returns
212 the array of segments in your re-routed URI in the event you are using
213 CodeIgniter's :doc:`URI Routing <../general/routing>` feature.