blob: 4d38c1d22822e7f4523b65e84016b553efb9a5a8 [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 Andreevcd3d9db2015-02-02 13:41:01 +020023.. php:class:: CI_URI
Derek Jones8ede1a22011-10-05 13:34:52 -050024
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020025 .. php:method:: segment($n[, $no_result = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050026
Andrey Andreev28c2c972014-02-08 04:27:48 +020027 :param int $n: Segment index number
28 :param mixed $no_result: What to return if the searched segment is not found
29 :returns: Segment value or $no_result value if not found
30 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -050031
Andrey Andreev3a5638b2014-01-03 14:57:03 +020032 Permits you to retrieve a specific segment. Where n is the segment
33 number you wish to retrieve. Segments are numbered from left to right.
34 For example, if your full URL is this::
Derek Jones8ede1a22011-10-05 13:34:52 -050035
Andrey Andreev3a5638b2014-01-03 14:57:03 +020036 http://example.com/index.php/news/local/metro/crime_is_up
Derek Jones8ede1a22011-10-05 13:34:52 -050037
Andrey Andreev3a5638b2014-01-03 14:57:03 +020038 The segment numbers would be this:
Derek Jones8ede1a22011-10-05 13:34:52 -050039
Andrey Andreev3a5638b2014-01-03 14:57:03 +020040 #. news
41 #. local
42 #. metro
43 #. crime_is_up
Derek Jones8ede1a22011-10-05 13:34:52 -050044
Andrey Andreev3a5638b2014-01-03 14:57:03 +020045 The optional second parameter defaults to NULL and allows you to set the return value
46 of this method when the requested URI segment is missing.
47 For example, this would tell the method to return the number zero in the event of failure::
Derek Jones8ede1a22011-10-05 13:34:52 -050048
Andrey Andreev3a5638b2014-01-03 14:57:03 +020049 $product_id = $this->uri->segment(3, 0);
Derek Jones8ede1a22011-10-05 13:34:52 -050050
Andrey Andreev3a5638b2014-01-03 14:57:03 +020051 It helps avoid having to write code like this::
Derek Jones8ede1a22011-10-05 13:34:52 -050052
Andrey Andreev3a5638b2014-01-03 14:57:03 +020053 if ($this->uri->segment(3) === FALSE)
54 {
55 $product_id = 0;
56 }
57 else
58 {
59 $product_id = $this->uri->segment(3);
60 }
Derek Jones8ede1a22011-10-05 13:34:52 -050061
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020062 .. php:method:: rsegment($n[, $no_result = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -050063
Andrey Andreev28c2c972014-02-08 04:27:48 +020064 :param int $n: Segment index number
65 :param mixed $no_result: What to return if the searched segment is not found
66 :returns: Routed segment value or $no_result value if not found
67 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -050068
Andrey Andreev3a5638b2014-01-03 14:57:03 +020069 This method is identical to ``segment()``, except that it lets you retrieve
70 a specific segment from your re-routed URI in the event you are
71 using CodeIgniter's :doc:`URI Routing <../general/routing>` feature.
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020073 .. php:method:: slash_segment($n[, $where = 'trailing'])
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Andrey Andreev28c2c972014-02-08 04:27:48 +020075 :param int $n: Segment index number
76 :param string $where: Where to add the slash ('trailing' or 'leading')
77 :returns: Segment value, prepended/suffixed with a forward slash, or a slash if not found
78 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -050079
Andrey Andreev3a5638b2014-01-03 14:57:03 +020080 This method is almost identical to ``segment()``, except it
81 adds a trailing and/or leading slash based on the second parameter.
82 If the parameter is not used, a trailing slash added. Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Andrey Andreev3a5638b2014-01-03 14:57:03 +020084 $this->uri->slash_segment(3);
85 $this->uri->slash_segment(3, 'leading');
86 $this->uri->slash_segment(3, 'both');
Derek Jones8ede1a22011-10-05 13:34:52 -050087
Andrey Andreev3a5638b2014-01-03 14:57:03 +020088 Returns:
Derek Jones8ede1a22011-10-05 13:34:52 -050089
Andrey Andreev3a5638b2014-01-03 14:57:03 +020090 #. segment/
91 #. /segment
92 #. /segment/
Derek Jones8ede1a22011-10-05 13:34:52 -050093
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020094 .. php:method:: slash_rsegment($n[, $where = 'trailing'])
Derek Jones8ede1a22011-10-05 13:34:52 -050095
Andrey Andreev28c2c972014-02-08 04:27:48 +020096 :param int $n: Segment index number
97 :param string $where: Where to add the slash ('trailing' or 'leading')
98 :returns: Routed segment value, prepended/suffixed with a forward slash, or a slash if not found
99 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200101 This method is identical to ``slash_segment()``, except that it lets you
102 add slashes a specific segment from your re-routed URI in the event you
103 are using CodeIgniter's :doc:`URI Routing <../general/routing>`
104 feature.
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200106 .. php:method:: uri_to_assoc([$n = 3[, $default = array()]])
Derek Jones87d152e2011-10-05 15:32:45 -0500107
Andrey Andreev28c2c972014-02-08 04:27:48 +0200108 :param int $n: Segment index number
109 :param array $default: Default values
110 :returns: Associative URI segments array
111 :rtype: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
Rafael Schwemmer8158bc32015-03-18 15:41:32 +0100113 This method lets you turn URI segments into an associative array of
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200114 key/value pairs. Consider this URI::
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200116 index.php/user/search/name/joe/location/UK/gender/male
Derek Jones87d152e2011-10-05 15:32:45 -0500117
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200118 Using this method you can turn the URI into an associative array with
119 this prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200121 [array]
122 (
123 'name' => 'joe'
124 'location' => 'UK'
125 'gender' => 'male'
126 )
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200128 The first parameter lets you set an offset, which defaults to 3 since your
129 URI will normally contain a controller/method pair in the first and second segments.
130 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200132 $array = $this->uri->uri_to_assoc(3);
133 echo $array['name'];
Derek Jones8ede1a22011-10-05 13:34:52 -0500134
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200135 The second parameter lets you set default key names, so that the array
136 returned will always contain expected indexes, even if missing from the URI.
137 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200139 $default = array('name', 'gender', 'location', 'type', 'sort');
140 $array = $this->uri->uri_to_assoc(3, $default);
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200142 If the URI does not contain a value in your default, an array index will
143 be set to that name, with a value of NULL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200145 Lastly, if a corresponding value is not found for a given key (if there
146 is an odd number of URI segments) the value will be set to NULL.
Derek Jones87d152e2011-10-05 15:32:45 -0500147
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200148 .. php:method:: ruri_to_assoc([$n = 3[, $default = array()]])
Derek Jones87d152e2011-10-05 15:32:45 -0500149
Andrey Andreev28c2c972014-02-08 04:27:48 +0200150 :param int $n: Segment index number
151 :param array $default: Default values
152 :returns: Associative routed URI segments array
153 :rtype: array
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200155 This method is identical to ``uri_to_assoc()``, except that it creates
156 an associative array using the re-routed URI in the event you are using
157 CodeIgniter's :doc:`URI Routing <../general/routing>` feature.
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200159 .. php:method:: assoc_to_uri($array)
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
Andrey Andreev28c2c972014-02-08 04:27:48 +0200161 :param array $array: Input array of key/value pairs
162 :returns: URI string
163 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200165 Takes an associative array as input and generates a URI string from it.
166 The array keys will be included in the string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200168 $array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
169 $str = $this->uri->assoc_to_uri($array);
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200171 // Produces: product/shoes/size/large/color/red
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200173 .. php:method:: uri_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Andrey Andreev28c2c972014-02-08 04:27:48 +0200175 :returns: URI string
176 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200178 Returns a string with the complete URI. For example, if this is your full URL::
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200180 http://example.com/index.php/news/local/345
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200182 The method would return this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200184 news/local/345
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200186 .. php:method:: ruri_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500187
Andrey Andreev28c2c972014-02-08 04:27:48 +0200188 :returns: Routed URI string
189 :rtype: string
Derek Jones87d152e2011-10-05 15:32:45 -0500190
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200191 This method is identical to ``uri_string()``, except that it returns
192 the re-routed URI in the event you are using CodeIgniter's :doc:`URI
193 Routing <../general/routing>` feature.
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200195 .. php:method:: total_segments()
Derek Jones8ede1a22011-10-05 13:34:52 -0500196
Andrey Andreev28c2c972014-02-08 04:27:48 +0200197 :returns: Count of URI segments
198 :rtype: int
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200199
200 Returns the total number of segments.
201
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200202 .. php:method:: total_rsegments()
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200203
Andrey Andreev28c2c972014-02-08 04:27:48 +0200204 :returns: Count of routed URI segments
205 :rtype: int
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200206
207 This method is identical to ``total_segments()``, except that it returns
208 the total number of segments in your re-routed URI in the event you are
209 using CodeIgniter's :doc:`URI Routing <../general/routing>` feature.
210
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200211 .. php:method:: segment_array()
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200212
Andrey Andreev28c2c972014-02-08 04:27:48 +0200213 :returns: URI segments array
214 :rtype: array
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200215
216 Returns an array containing the URI segments. For example::
217
218 $segs = $this->uri->segment_array();
219
220 foreach ($segs as $segment)
221 {
222 echo $segment;
223 echo '<br />';
224 }
225
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200226 .. php:method:: rsegment_array()
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200227
Andrey Andreev28c2c972014-02-08 04:27:48 +0200228 :returns: Routed URI segments array
229 :rtype: array
Andrey Andreev3a5638b2014-01-03 14:57:03 +0200230
231 This method is identical to ``segment_array()``, except that it returns
232 the array of segments in your re-routed URI in the event you are using
Rafael Schwemmer8158bc32015-03-18 15:41:32 +0100233 CodeIgniter's :doc:`URI Routing <../general/routing>` feature.