Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ######### |
| 2 | URI Class |
| 3 | ######### |
| 4 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 5 | The URI Class provides methods that help you retrieve information from |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 6 | your URI strings. If you use URI routing, you can also retrieve |
| 7 | information 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 Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 12 | .. contents:: |
| 13 | :local: |
| 14 | |
| 15 | .. raw:: html |
| 16 | |
| 17 | <div class="custom-index container"></div> |
| 18 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 19 | *************** |
| 20 | Class Reference |
| 21 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 23 | .. class:: CI_URI |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 25 | .. method:: segment($n[, $no_result = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 27 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 31 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 32 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 36 | http://example.com/index.php/news/local/metro/crime_is_up |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 37 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 38 | The segment numbers would be this: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 39 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 40 | #. news |
| 41 | #. local |
| 42 | #. metro |
| 43 | #. crime_is_up |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 45 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 49 | $product_id = $this->uri->segment(3, 0); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 50 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 51 | It helps avoid having to write code like this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 53 | if ($this->uri->segment(3) === FALSE) |
| 54 | { |
| 55 | $product_id = 0; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | $product_id = $this->uri->segment(3); |
| 60 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 61 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 62 | .. method:: rsegment($n[, $no_result = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 63 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 64 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 69 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 73 | .. method:: slash_segment($n[, $where = 'trailing']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 75 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 80 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 84 | $this->uri->slash_segment(3); |
| 85 | $this->uri->slash_segment(3, 'leading'); |
| 86 | $this->uri->slash_segment(3, 'both'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 87 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 88 | Returns: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 89 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 90 | #. segment/ |
| 91 | #. /segment |
| 92 | #. /segment/ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 93 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 94 | .. method:: slash_rsegment($n[, $where = 'trailing']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 95 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 96 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 101 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 106 | .. method:: uri_to_assoc([$n = 3[, $default = array()]]) |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 107 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 108 | :param int $n: Segment index number |
| 109 | :param array $default: Default values |
| 110 | :returns: Associative URI segments array |
| 111 | :rtype: array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 113 | This method lets you turn URI segments into and associative array of |
| 114 | key/value pairs. Consider this URI:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 115 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 116 | index.php/user/search/name/joe/location/UK/gender/male |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 117 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 118 | Using this method you can turn the URI into an associative array with |
| 119 | this prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 121 | [array] |
| 122 | ( |
| 123 | 'name' => 'joe' |
| 124 | 'location' => 'UK' |
| 125 | 'gender' => 'male' |
| 126 | ) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 127 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 128 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 132 | $array = $this->uri->uri_to_assoc(3); |
| 133 | echo $array['name']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 134 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 135 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 139 | $default = array('name', 'gender', 'location', 'type', 'sort'); |
| 140 | $array = $this->uri->uri_to_assoc(3, $default); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 142 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 145 | 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 Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 147 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 148 | .. method:: ruri_to_assoc([$n = 3[, $default = array()]]) |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 149 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 150 | :param int $n: Segment index number |
| 151 | :param array $default: Default values |
| 152 | :returns: Associative routed URI segments array |
| 153 | :rtype: array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 154 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 155 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 158 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 159 | .. method:: assoc_to_uri($array) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 160 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 161 | :param array $array: Input array of key/value pairs |
| 162 | :returns: URI string |
| 163 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 164 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 165 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 167 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 168 | $array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red'); |
| 169 | $str = $this->uri->assoc_to_uri($array); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 171 | // Produces: product/shoes/size/large/color/red |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 173 | .. method:: uri_string() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 175 | :returns: URI string |
| 176 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 177 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 178 | Returns a string with the complete URI. For example, if this is your full URL:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 179 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 180 | http://example.com/index.php/news/local/345 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 181 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 182 | The method would return this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 183 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 184 | news/local/345 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 185 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 186 | .. method:: ruri_string() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 187 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 188 | :returns: Routed URI string |
| 189 | :rtype: string |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 190 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 191 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 194 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 195 | .. method:: total_segments() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 196 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 197 | :returns: Count of URI segments |
| 198 | :rtype: int |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 199 | |
| 200 | Returns the total number of segments. |
| 201 | |
| 202 | .. method:: total_rsegments() |
| 203 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 204 | :returns: Count of routed URI segments |
| 205 | :rtype: int |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 206 | |
| 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 | |
| 211 | .. method:: segment_array() |
| 212 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 213 | :returns: URI segments array |
| 214 | :rtype: array |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 215 | |
| 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 | |
| 226 | .. method:: rsegment_array() |
| 227 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame^] | 228 | :returns: Routed URI segments array |
| 229 | :rtype: array |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 230 | |
| 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 |
| 233 | CodeIgniter's :doc:`URI Routing <../general/routing>` feature. |