Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ######### |
| 2 | URI Class |
| 3 | ######### |
| 4 | |
| 5 | The URI Class provides functions that help you retrieve information from |
| 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 | |
| 12 | $this->uri->segment(n) |
| 13 | ====================== |
| 14 | |
| 15 | Permits you to retrieve a specific segment. Where n is the segment |
| 16 | number you wish to retrieve. Segments are numbered from left to right. |
| 17 | For example, if your full URL is this:: |
| 18 | |
| 19 | http://example.com/index.php/news/local/metro/crime_is_up |
| 20 | |
| 21 | The segment numbers would be this: |
| 22 | |
| 23 | #. news |
| 24 | #. local |
| 25 | #. metro |
| 26 | #. crime_is_up |
| 27 | |
Phil Sturgeon | 55a6ddb | 2012-05-23 18:37:24 +0100 | [diff] [blame] | 28 | By default the function returns NULL if the segment does not |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 29 | exist. There is an optional second parameter that permits you to set |
| 30 | your own default value if the segment is missing. For example, this |
| 31 | would tell the function to return the number zero in the event of |
| 32 | failure:: |
| 33 | |
| 34 | $product_id = $this->uri->segment(3, 0); |
| 35 | |
| 36 | It helps avoid having to write code like this:: |
| 37 | |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 38 | if ($this->uri->segment(3) === FALSE) |
| 39 | { |
| 40 | $product_id = 0; |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | $product_id = $this->uri->segment(3); |
| 45 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
| 47 | $this->uri->rsegment(n) |
| 48 | ======================= |
| 49 | |
| 50 | This function is identical to the previous one, except that it lets you |
| 51 | retrieve a specific segment from your re-routed URI in the event you are |
| 52 | using CodeIgniter's :doc:`URI Routing <../general/routing>` feature. |
| 53 | |
| 54 | $this->uri->slash_segment(n) |
| 55 | ============================= |
| 56 | |
| 57 | This function is almost identical to $this->uri->segment(), except it |
| 58 | adds a trailing and/or leading slash based on the second parameter. If |
| 59 | the parameter is not used, a trailing slash added. Examples:: |
| 60 | |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 61 | $this->uri->slash_segment(3); |
| 62 | $this->uri->slash_segment(3, 'leading'); |
| 63 | $this->uri->slash_segment(3, 'both'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
| 65 | Returns: |
| 66 | |
| 67 | #. segment/ |
| 68 | #. /segment |
| 69 | #. /segment/ |
| 70 | |
| 71 | $this->uri->slash_rsegment(n) |
| 72 | ============================== |
| 73 | |
| 74 | This function is identical to the previous one, except that it lets you |
| 75 | add slashes a specific segment from your re-routed URI in the event you |
| 76 | are using CodeIgniter's :doc:`URI Routing <../general/routing>` |
| 77 | feature. |
| 78 | |
| 79 | $this->uri->uri_to_assoc(n) |
| 80 | ============================= |
| 81 | |
| 82 | This function lets you turn URI segments into and associative array of |
| 83 | key/value pairs. Consider this URI:: |
| 84 | |
| 85 | index.php/user/search/name/joe/location/UK/gender/male |
| 86 | |
| 87 | Using this function you can turn the URI into an associative array with |
| 88 | this prototype:: |
| 89 | |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 90 | [array] |
| 91 | ( |
| 92 | 'name' => 'joe' |
| 93 | 'location' => 'UK' |
| 94 | 'gender' => 'male' |
| 95 | ) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 96 | |
| 97 | The first parameter of the function lets you set an offset. By default |
| 98 | it is set to 3 since your URI will normally contain a |
| 99 | controller/function in the first and second segments. Example:: |
| 100 | |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 101 | $array = $this->uri->uri_to_assoc(3); |
| 102 | |
| 103 | echo $array['name']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 104 | |
| 105 | The second parameter lets you set default key names, so that the array |
| 106 | returned by the function will always contain expected indexes, even if |
| 107 | missing from the URI. Example:: |
| 108 | |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 109 | $default = array('name', 'gender', 'location', 'type', 'sort'); |
| 110 | |
| 111 | $array = $this->uri->uri_to_assoc(3, $default); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
| 113 | If the URI does not contain a value in your default, an array index will |
| 114 | be set to that name, with a value of FALSE. |
| 115 | |
| 116 | Lastly, if a corresponding value is not found for a given key (if there |
| 117 | is an odd number of URI segments) the value will be set to FALSE |
| 118 | (boolean). |
| 119 | |
| 120 | $this->uri->ruri_to_assoc(n) |
| 121 | ============================== |
| 122 | |
| 123 | This function is identical to the previous one, except that it creates |
| 124 | an associative array using the re-routed URI in the event you are using |
| 125 | CodeIgniter's :doc:`URI Routing <../general/routing>` feature. |
| 126 | |
| 127 | $this->uri->assoc_to_uri() |
| 128 | ============================ |
| 129 | |
| 130 | Takes an associative array as input and generates a URI string from it. |
| 131 | The array keys will be included in the string. Example:: |
| 132 | |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 133 | $array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red'); |
| 134 | |
| 135 | $str = $this->uri->assoc_to_uri($array); |
| 136 | |
| 137 | // Produces: product/shoes/size/large/color/red |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | |
| 139 | $this->uri->uri_string() |
| 140 | ========================= |
| 141 | |
| 142 | Returns a string with the complete URI. For example, if this is your |
| 143 | full URL:: |
| 144 | |
| 145 | http://example.com/index.php/news/local/345 |
| 146 | |
| 147 | The function would return this:: |
| 148 | |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 149 | news/local/345 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | |
| 151 | $this->uri->ruri_string() |
| 152 | ========================== |
| 153 | |
| 154 | This function is identical to the previous one, except that it returns |
| 155 | the re-routed URI in the event you are using CodeIgniter's :doc:`URI |
| 156 | Routing <../general/routing>` feature. |
| 157 | |
| 158 | $this->uri->total_segments() |
| 159 | ============================= |
| 160 | |
| 161 | Returns the total number of segments. |
| 162 | |
| 163 | $this->uri->total_rsegments() |
| 164 | ============================== |
| 165 | |
| 166 | This function is identical to the previous one, except that it returns |
| 167 | the total number of segments in your re-routed URI in the event you are |
| 168 | using CodeIgniter's :doc:`URI Routing <../general/routing>` feature. |
| 169 | |
| 170 | $this->uri->segment_array() |
| 171 | ============================ |
| 172 | |
| 173 | Returns an array containing the URI segments. For example:: |
| 174 | |
Derek Jones | 87d152e | 2011-10-05 15:32:45 -0500 | [diff] [blame] | 175 | $segs = $this->uri->segment_array(); |
| 176 | |
| 177 | foreach ($segs as $segment) |
| 178 | { |
| 179 | echo $segment; |
| 180 | echo '<br />'; |
| 181 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 182 | |
| 183 | $this->uri->rsegment_array() |
| 184 | ============================= |
| 185 | |
| 186 | This function is identical to the previous one, except that it returns |
| 187 | the array of segments in your re-routed URI in the event you are using |
| 188 | CodeIgniter's :doc:`URI Routing <../general/routing>` feature. |