Skip to content

Commit 6b077b7

Browse files
authored
Merge pull request #6233 from kenjis/fix-docs-route-to
docs: improve route_to() and url_to()
2 parents 0d775ac + c068a79 commit 6b077b7

File tree

14 files changed

+134
-49
lines changed

14 files changed

+134
-49
lines changed

system/Common.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,8 @@ function remove_invisible_characters(string $str, bool $urlEncoded = true): stri
905905
* NOTE: This requires the controller/method to
906906
* have a route defined in the routes Config file.
907907
*
908-
* @param mixed ...$params
908+
* @param string $method Named route or Controller::method
909+
* @param int|string ...$params One or more parameters to be passed to the route
909910
*
910911
* @return false|string
911912
*/

system/Helpers/url_helper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ function mb_url_title(string $str, string $separator = '-', bool $lowercase = fa
525525
* NOTE: This requires the controller/method to
526526
* have a route defined in the routes Config file.
527527
*
528-
* @param mixed ...$args
528+
* @param string $controller Named route or Controller::method
529+
* @param int|string ...$args One or more parameters to be passed to the route
529530
*
530531
* @throws RouterException
531532
*/

system/Router/RouteCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,8 @@ public function environment(string $env, Closure $callback): RouteCollectionInte
989989
* // Equals 'path/$param1/$param2'
990990
* reverseRoute('Controller::method', $param1, $param2);
991991
*
992-
* @param mixed ...$params
992+
* @param string $search Named route or Controller::method
993+
* @param int|string ...$params One or more parameters to be passed to the route
993994
*
994995
* @return false|string
995996
*/

system/Router/RouteCollectionInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ public function getHTTPVerb();
169169
* // Equals 'path/$param1/$param2'
170170
* reverseRoute('Controller::method', $param1, $param2);
171171
*
172-
* @param array ...$params
172+
* @param string $search Named route or Controller::method
173+
* @param int|string ...$params
173174
*
174175
* @return false|string
175176
*/

user_guide_src/source/general/common_functions.rst

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Service Accessors
6565
values that are specific to the environment itself, like database
6666
settings, API keys, etc.
6767

68-
.. php:function:: esc($data[, $context = 'html' [, $encoding]])
68+
.. php:function:: esc($data[, $context = 'html'[, $encoding]])
6969
7070
:param string|array $data: The information to be escaped.
7171
:param string $context: The escaping context. Default is 'html'.
@@ -89,7 +89,7 @@ Service Accessors
8989

9090
For full details, see the :doc:`helpers` page.
9191

92-
.. php:function:: lang($line[, $args[, $locale ]])
92+
.. php:function:: lang($line[, $args[, $locale]])
9393
9494
:param string $line: The line of text to retrieve
9595
:param array $args: An array of data to substitute for placeholders.
@@ -99,15 +99,15 @@ Service Accessors
9999

100100
For more information, see the :doc:`Localization </outgoing/localization>` page.
101101

102-
.. php:function:: model($name [, $getShared = true [, &$conn = null ]])
102+
.. php:function:: model($name[, $getShared = true[, &$conn = null]])
103103
104104
:param string $name:
105105
:param boolean $getShared:
106106
:param ConnectionInterface|null $conn:
107107
:returns: More simple way of getting model instances
108108
:rtype: mixed
109109

110-
.. php:function:: old( $key[, $default = null, [, $escape = 'html' ]] )
110+
.. php:function:: old($key[, $default = null,[, $escape = 'html']])
111111
112112
:param string $key: The name of the old form data to check for.
113113
:param mixed $default: The default value to return if $key doesn't exist.
@@ -124,7 +124,7 @@ Service Accessors
124124
.. note:: If you are using the :doc:`form helper </helpers/form_helper>`, this feature is built-in. You only
125125
need to use this function when not using the form helper.
126126

127-
.. php:function:: session( [$key] )
127+
.. php:function:: session([$key])
128128
129129
:param string $key: The name of the session item to check for.
130130
:returns: An instance of the Session object if no $key, the value found in the session for $key, or null.
@@ -133,7 +133,7 @@ Service Accessors
133133
Provides a convenient way to access the session class and to retrieve a
134134
stored value. For more information, see the :doc:`Sessions </libraries/sessions>` page.
135135

136-
.. php:function:: timer( [$name] )
136+
.. php:function:: timer([$name])
137137
138138
:param string $name: The name of the benchmark point.
139139
:returns: The Timer instance
@@ -147,7 +147,7 @@ Service Accessors
147147

148148
.. literalinclude:: common_functions/003.php
149149

150-
.. php:function:: view ($name [, $data [, $options ]])
150+
.. php:function:: view($name[, $data[, $options]])
151151
152152
:param string $name: The name of the file to load
153153
:param array $data: An array of key/value pairs to make available within the view.
@@ -175,7 +175,7 @@ Service Accessors
175175

176176
For more details, see the :doc:`Views </outgoing/views>` page.
177177

178-
.. php:function:: view_cell ( $library [, $params = null [, $ttl = 0 [, $cacheName = null]]] )
178+
.. php:function:: view_cell($library[, $params = null[, $ttl = 0[, $cacheName = null]]])
179179
180180
:param string $library:
181181
:param null $params:
@@ -189,51 +189,51 @@ Service Accessors
189189
Miscellaneous Functions
190190
=======================
191191

192-
.. php:function:: app_timezone ()
192+
.. php:function:: app_timezone()
193193
194194
:returns: The timezone the application has been set to display dates in.
195195
:rtype: string
196196

197197
Returns the timezone the application has been set to display dates in.
198198

199-
.. php:function:: csp_script_nonce ()
199+
.. php:function:: csp_script_nonce()
200200
201201
:returns: The CSP nonce attribute for script tag.
202202
:rtype: string
203203

204204
Returns the nonce attribute for a script tag. For example: ``nonce="Eskdikejidojdk978Ad8jf"``.
205205
See :ref:`content-security-policy`.
206206

207-
.. php:function:: csp_style_nonce ()
207+
.. php:function:: csp_style_nonce()
208208
209209
:returns: The CSP nonce attribute for style tag.
210210
:rtype: string
211211

212212
Returns the nonce attribute for a style tag. For example: ``nonce="Eskdikejidojdk978Ad8jf"``.
213213
See :ref:`content-security-policy`.
214214

215-
.. php:function:: csrf_token ()
215+
.. php:function:: csrf_token()
216216
217217
:returns: The name of the current CSRF token.
218218
:rtype: string
219219

220220
Returns the name of the current CSRF token.
221221

222-
.. php:function:: csrf_header ()
222+
.. php:function:: csrf_header()
223223
224224
:returns: The name of the header for current CSRF token.
225225
:rtype: string
226226

227227
The name of the header for current CSRF token.
228228

229-
.. php:function:: csrf_hash ()
229+
.. php:function:: csrf_hash()
230230
231231
:returns: The current value of the CSRF hash.
232232
:rtype: string
233233

234234
Returns the current CSRF hash value.
235235

236-
.. php:function:: csrf_field ()
236+
.. php:function:: csrf_field()
237237
238238
:returns: A string with the HTML for hidden input with all required CSRF information.
239239
:rtype: string
@@ -242,7 +242,7 @@ Miscellaneous Functions
242242

243243
<input type="hidden" name="{csrf_token}" value="{csrf_hash}">
244244

245-
.. php:function:: csrf_meta ()
245+
.. php:function:: csrf_meta()
246246
247247
:returns: A string with the HTML for meta tag with all required CSRF information.
248248
:rtype: string
@@ -251,7 +251,7 @@ Miscellaneous Functions
251251

252252
<meta name="{csrf_header}" content="{csrf_hash}">
253253

254-
.. php:function:: force_https ( $duration = 31536000 [, $request = null [, $response = null]] )
254+
.. php:function:: force_https($duration = 31536000[, $request = null[, $response = null]])
255255
256256
:param int $duration: The number of seconds browsers should convert links to this resource to HTTPS.
257257
:param RequestInterface $request: An instance of the current Request object.
@@ -262,24 +262,24 @@ Miscellaneous Functions
262262
but through HTTPS. Will set the HTTP Strict Transport Security header, which instructs
263263
modern browsers to automatically modify any HTTP requests to HTTPS requests for the $duration.
264264

265-
.. php:function:: function_usable ( $function_name )
265+
.. php:function:: function_usable($function_name)
266266
267267
:param string $function_name: Function to check for
268268
:returns: true if the function exists and is safe to call, false otherwise.
269269
:rtype: bool
270270

271-
.. php:function:: is_cli ()
271+
.. php:function:: is_cli()
272272
273273
:returns: true if the script is being executed from the command line or false otherwise.
274274
:rtype: bool
275275

276-
.. php:function:: is_really_writable ( $file )
276+
.. php:function:: is_really_writable($file)
277277
278278
:param string $file: The filename being checked.
279279
:returns: true if you can write to the file, false otherwise.
280280
:rtype: bool
281281

282-
.. php:function:: log_message ($level, $message [, $context])
282+
.. php:function:: log_message($level, $message [, $context])
283283
284284
:param string $level: The level of severity
285285
:param string $message: The message that is to be logged.
@@ -295,7 +295,7 @@ Miscellaneous Functions
295295
Context can be used to substitute values in the message string. For full details, see the
296296
:doc:`Logging Information <logging>` page.
297297

298-
.. php:function:: redirect( string $route )
298+
.. php:function:: redirect(string $route)
299299
300300
:param string $route: The reverse-routed or named route to redirect the user to.
301301

@@ -326,19 +326,25 @@ Miscellaneous Functions
326326

327327
.. literalinclude:: common_functions/007.php
328328

329-
.. php:function:: route_to ( $method [, ...$params] )
329+
.. php:function:: route_to($method[, ...$params])
330330
331331
:param string $method: The named route alias, or name of the controller/method to match.
332-
:param mixed $params: One or more parameters to be passed to be matched in the route.
332+
:param int|string $params: One or more parameters to be passed to be matched in the route.
333333

334334
.. note:: This function requires the controller/method to have a route defined in **app/Config/routes.php**.
335335

336-
Generates a URI relative to the domain name (not **baseUrl**) for you based on either a named route alias,
336+
Generates a route for you based on either a named route alias,
337337
or a controller::method combination. Will take parameters into effect, if provided.
338338

339-
For full details, see the :doc:`/incoming/routing` page.
339+
.. literalinclude:: common_functions/009.php
340340

341-
.. php:function:: service ( $name [, ...$params] )
341+
.. literalinclude:: common_functions/010.php
342+
343+
.. note:: ``route_to()`` returns a route, not a full URI path for your site.
344+
If your **baseURL** contains sub folders, the return value is not the same
345+
as the URI to link. In that case, just use :php:func:`url_to` instead.
346+
347+
.. php:function:: service($name[, ...$params])
342348
343349
:param string $name: The name of the service to load
344350
:param mixed $params: One or more parameters to pass to the service method.
@@ -353,7 +359,7 @@ Miscellaneous Functions
353359

354360
.. literalinclude:: common_functions/008.php
355361

356-
.. php:function:: single_service ( $name [, ...$params] )
362+
.. php:function:: single_service($name [, ...$params])
357363
358364
:param string $name: The name of the service to load
359365
:param mixed $params: One or more parameters to pass to the service method.
@@ -372,7 +378,7 @@ Miscellaneous Functions
372378

373379
Fetch a config file item with slash appended (if not empty)
374380

375-
.. php:function:: stringify_attributes ( $attributes [, $js] )
381+
.. php:function:: stringify_attributes($attributes [, $js])
376382
377383
:param mixed $attributes: string, array of key value pairs, or object
378384
:param boolean $js: true if values do not need quotes (Javascript-style)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
// The route is defined as:
4+
$routes->get('users/(:num)/gallery(:any)', 'Galleries::showUserGallery/$1/$2');
5+
6+
?>
7+
8+
<?php
9+
10+
// Generate the route with user ID 15, gallery 12:
11+
route_to('Galleries::showUserGallery', 15, 12);
12+
// Result: '/users/15/gallery/12'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
// The route is defined as:
4+
$routes->get('users/(:num)/gallery(:any)', 'Galleries::showUserGallery/$1/$2', ['as' => 'user_gallery']);
5+
6+
?>
7+
8+
<?php
9+
10+
// Generate the route with user ID 15, gallery 12:
11+
route_to('user_gallery', 15, 12);
12+
// Result: '/users/15/gallery/12'

user_guide_src/source/general/urls.rst

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,47 @@ CodeIgniter URLs
99
By default, URLs in CodeIgniter are designed to be search-engine and human-friendly. Rather than using the standard
1010
"query-string" approach to URLs that is synonymous with dynamic systems, CodeIgniter uses a **segment-based** approach::
1111

12-
example.com/news/article/my_article
12+
https://example.com/news/article/my_article
1313

1414
Your URLs can be defined using the :doc:`URI Routing </incoming/routing>` feature with flexibility.
1515

1616
The :doc:`URI Library <../libraries/uri>` and the :doc:`URL Helper <../helpers/url_helper>` contain functions that make it easy to work with your URI data.
1717

18+
URL Structure
19+
=============
20+
21+
Base URL contains only the Hostname
22+
-----------------------------------
23+
24+
When you have the Base URL **https://www.example.com/** and imagine the following URL::
25+
26+
https://www.example.com/blog/news/2022/10?page=2
27+
28+
We use the following terms:
29+
30+
======== ============================
31+
Base URL **https://www.example.com/**
32+
URI path /blog/news/2022/10
33+
Route /blog/news/2022/10
34+
Query page=2
35+
======== ============================
36+
37+
Base URL contains Sub folders
38+
-----------------------------
39+
40+
When you have the Base URL **https://www.example.com/ci-blog/** and imagine the following URL::
41+
42+
https://www.example.com/ci-blog/blog/news/2022/10?page=2
43+
44+
We use the following terms:
45+
46+
======== ====================================
47+
Base URL **https://www.example.com/ci-blog/**
48+
URI path /ci-blog/blog/news/2022/10
49+
Route /blog/news/2022/10
50+
Query page=2
51+
======== ====================================
52+
1853
Removing the index.php file
1954
===========================
2055

user_guide_src/source/helpers/url_helper.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ The following functions are available:
346346

347347
.. php:function:: url_to($controller[, ...$args])
348348
349-
:param string $controller: The controller class and method
350-
:param mixed ...$args: Additional arguments to be injected into the route
349+
:param string $controller: Named route or Controller::method
350+
:param mixed ...$args: One or more parameters to be passed to the route
351351
:returns: Absolute URL
352352
:rtype: string
353353

@@ -362,12 +362,11 @@ The following functions are available:
362362

363363
.. literalinclude:: url_helper/022.php
364364

365-
The above example would return something like:
366-
*http://example.com/page/home*
367-
368365
This is useful because you can still change your routes after putting links
369366
into your views.
370367

368+
For full details, see the :ref:`reverse-routing` and :ref:`using-named-routes`.
369+
371370
.. php:function:: url_is($path)
372371
373372
:param string $path: The path to check the current URI path against.
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
<?php
22

3-
echo url_to('Home::index');
3+
// The route is defined as:
4+
$routes->get('/', 'Home::index');
5+
6+
?>
7+
8+
<a href="<?= url_to('Home::index') ?>">Home</a>
9+
<!-- Result: 'http://example.com/' -->
10+
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
<?php
22

3-
echo url_to('Page::index', 'home');
3+
// The route is defined as:
4+
$routes->get('pages/(:segment)', 'Page::index/$1');
5+
6+
?>
7+
8+
<a href="<?= url_to('Page::index', 'home') ?>">Home</a>
9+
<!-- Result: 'http://example.com/pages/home' -->

0 commit comments

Comments
 (0)