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 | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 12 | *************** |
| 13 | Class Reference |
| 14 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 15 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 16 | .. class:: CI_URI |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 17 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 18 | .. method:: segment($n[, $no_result = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 19 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 20 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 23 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 24 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 28 | http://example.com/index.php/news/local/metro/crime_is_up |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 29 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 30 | The segment numbers would be this: |
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 | #. news |
| 33 | #. local |
| 34 | #. metro |
| 35 | #. 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 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 41 | $product_id = $this->uri->segment(3, 0); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 42 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 43 | It helps avoid having to write code like this:: |
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 | if ($this->uri->segment(3) === FALSE) |
| 46 | { |
| 47 | $product_id = 0; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | $product_id = $this->uri->segment(3); |
| 52 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 54 | .. method:: rsegment($n[, $no_result = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 56 | :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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 60 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 63 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 64 | .. method:: slash_segment($n[, $where = 'trailing']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 66 | :param int $n: Segment index number |
| 67 | :param string $where: Where to add the slash ('trailing' or 'leading') |
| 68 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 70 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 74 | $this->uri->slash_segment(3); |
| 75 | $this->uri->slash_segment(3, 'leading'); |
| 76 | $this->uri->slash_segment(3, 'both'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 78 | Returns: |
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 | #. segment/ |
| 81 | #. /segment |
| 82 | #. /segment/ |
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 | .. method:: slash_rsegment($n[, $where = 'trailing']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 86 | :param int $n: Segment index number |
| 87 | :param string $where: Where to add the slash ('trailing' or 'leading') |
| 88 | :returns: string |
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 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 95 | .. method:: uri_to_assoc([$n = 3[, $default = array()]]) |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 96 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 97 | :param int $n: Segment index number |
| 98 | :param array $default: Default values |
| 99 | :returns: array |
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 lets you turn URI segments into and associative array of |
| 102 | key/value pairs. Consider this URI:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 104 | index.php/user/search/name/joe/location/UK/gender/male |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 105 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 106 | Using this method you can turn the URI into an associative array with |
| 107 | this prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 109 | [array] |
| 110 | ( |
| 111 | 'name' => 'joe' |
| 112 | 'location' => 'UK' |
| 113 | 'gender' => 'male' |
| 114 | ) |
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 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 120 | $array = $this->uri->uri_to_assoc(3); |
| 121 | echo $array['name']; |
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 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 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 | $default = array('name', 'gender', 'location', 'type', 'sort'); |
| 128 | $array = $this->uri->uri_to_assoc(3, $default); |
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 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 133 | 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 Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 135 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 136 | .. method:: ruri_to_assoc([$n = 3[, $default = array()]]) |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 137 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 138 | :param int $n: Segment index number |
| 139 | :param array $default: Default values |
| 140 | :returns: array |
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 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 145 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 146 | .. method:: assoc_to_uri($array) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 148 | :param array $array: Input array of key/value pairs |
| 149 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 151 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 153 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 154 | $array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red'); |
| 155 | $str = $this->uri->assoc_to_uri($array); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 156 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 157 | // Produces: product/shoes/size/large/color/red |
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:: uri_string() |
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 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 163 | 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] | 164 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 165 | http://example.com/index.php/news/local/345 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 166 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 167 | The method would return this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 169 | news/local/345 |
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 | .. method:: ruri_string() |
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 | :returns: string |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 174 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 175 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 178 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 179 | .. method:: total_segments() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 180 | |
Andrey Andreev | 3a5638b | 2014-01-03 14:57:03 +0200 | [diff] [blame^] | 181 | :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. |