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 | 3a5638b | 2014-01-03 14:57:03 +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: mixed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 31 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 35 | http://example.com/index.php/news/local/metro/crime_is_up |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 36 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 37 | The segment numbers would be this: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 39 | #. news |
| 40 | #. local |
| 41 | #. metro |
| 42 | #. crime_is_up |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 44 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 48 | $product_id = $this->uri->segment(3, 0); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 50 | It helps avoid having to write code like this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 52 | if ($this->uri->segment(3) === FALSE) |
| 53 | { |
| 54 | $product_id = 0; |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | $product_id = $this->uri->segment(3); |
| 59 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 61 | .. method:: rsegment($n[, $no_result = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 63 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 67 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 70 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 71 | .. method:: slash_segment($n[, $where = 'trailing']) |
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 | :param int $n: Segment index number |
| 74 | :param string $where: Where to add the slash ('trailing' or 'leading') |
| 75 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 77 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 81 | $this->uri->slash_segment(3); |
| 82 | $this->uri->slash_segment(3, 'leading'); |
| 83 | $this->uri->slash_segment(3, 'both'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 85 | Returns: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 87 | #. segment/ |
| 88 | #. /segment |
| 89 | #. /segment/ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 90 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 91 | .. method:: slash_rsegment($n[, $where = 'trailing']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 93 | :param int $n: Segment index number |
| 94 | :param string $where: Where to add the slash ('trailing' or 'leading') |
| 95 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 96 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 97 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 101 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 102 | .. method:: uri_to_assoc([$n = 3[, $default = array()]]) |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 103 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 104 | :param int $n: Segment index number |
| 105 | :param array $default: Default values |
| 106 | :returns: array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 108 | This method lets you turn URI segments into and associative array of |
| 109 | key/value pairs. Consider this URI:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 111 | index.php/user/search/name/joe/location/UK/gender/male |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 112 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 113 | Using this method you can turn the URI into an associative array with |
| 114 | this prototype:: |
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 | [array] |
| 117 | ( |
| 118 | 'name' => 'joe' |
| 119 | 'location' => 'UK' |
| 120 | 'gender' => 'male' |
| 121 | ) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 123 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 127 | $array = $this->uri->uri_to_assoc(3); |
| 128 | echo $array['name']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 130 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 134 | $default = array('name', 'gender', 'location', 'type', 'sort'); |
| 135 | $array = $this->uri->uri_to_assoc(3, $default); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 137 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 140 | 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 Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 142 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 143 | .. method:: ruri_to_assoc([$n = 3[, $default = array()]]) |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 144 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 145 | :param int $n: Segment index number |
| 146 | :param array $default: Default values |
| 147 | :returns: array |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 148 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 149 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 153 | .. method:: assoc_to_uri($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 | :param array $array: Input array of key/value pairs |
| 156 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 157 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 158 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 160 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 161 | $array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red'); |
| 162 | $str = $this->uri->assoc_to_uri($array); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 164 | // Produces: product/shoes/size/large/color/red |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 165 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 166 | .. method:: uri_string() |
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 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 169 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 170 | 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] | 171 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 172 | http://example.com/index.php/news/local/345 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 174 | The method would return this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 175 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 176 | news/local/345 |
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 | .. method:: ruri_string() |
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 | :returns: string |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 181 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 182 | 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 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:: total_segments() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 187 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame] | 188 | :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. |