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