Skip to content

Commit b252439

Browse files
committed
docs: improve route_to() and url_to()
1 parent 6b12ab8 commit b252439

7 files changed

Lines changed: 37 additions & 13 deletions

File tree

user_guide_src/source/general/common_functions.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,14 @@ Miscellaneous Functions
329329
.. 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 URI path (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+
For full details, see the :ref:`reverse-routing` and :ref:`using-named-routes`.
340340

341341
.. php:function:: service($name[, ...$params])
342342

user_guide_src/source/helpers/url_helper.rst

Lines changed: 2 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,9 +362,6 @@ 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

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' -->

user_guide_src/source/incoming/routing.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ routes defined within this closure are only accessible from the given environmen
269269

270270
.. literalinclude:: routing/028.php
271271

272+
.. _reverse-routing:
273+
272274
Reverse Routing
273275
===============
274276

@@ -277,12 +279,19 @@ to, and have the router lookup the current route to it. This allows route defini
277279
to update your application code. This is typically used within views to create links.
278280

279281
For example, if you have a route to a photo gallery that you want to link to, you can use the ``route_to()`` helper
280-
function to get the current route that should be used. The first parameter is the fully qualified Controller and method,
282+
function to get the URI path (route) that should be used. The first parameter is the fully qualified Controller and method,
281283
separated by a double colon (``::``), much like you would use when writing the initial route itself. Any parameters that
282284
should be passed to the route are passed in next:
283285

284286
.. literalinclude:: routing/029.php
285287

288+
.. note:: ``route_to()`` returns a URI path for the route, not a full URI path
289+
for your site. If your **baseURL** contains sub folders, the return value is
290+
not the same as the URI to link. In that case, you need to use :php:func:`site_url`
291+
like ``site_url(route_to(...))``, or just use :php:func:`url_to` instead.
292+
293+
.. _using-named-routes:
294+
286295
Using Named Routes
287296
==================
288297

@@ -295,6 +304,11 @@ with the name of the route:
295304

296305
This has the added benefit of making the views more readable, too.
297306

307+
.. note:: ``route_to()`` returns a URI path for the route, not a full URI path
308+
for your site. If your **baseURL** contains sub folders, the return value is
309+
not the same as the URI to link. In that case, you need to use :php:func:`site_url`
310+
like ``site_url(route_to(...))``, or just use :php:func:`url_to` instead.
311+
298312
Routes with any HTTP verbs
299313
==========================
300314

user_guide_src/source/incoming/routing/029.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
?>
77

8-
<!-- Generate the relative URL to link to user ID 15, gallery 12: -->
8+
<!-- Generate the URI path (route) to link to user ID 15, gallery 12: -->
99
<a href="<?= route_to('Galleries::showUserGallery', 15, 12) ?>">View Gallery</a>
1010
<!-- Result: '/users/15/gallery/12' -->

user_guide_src/source/incoming/routing/030.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
?>
77

8-
<!-- Generate the relative URL to link to user ID 15, gallery 12: -->
8+
<!-- Generate the URI path (route) to link to user ID 15, gallery 12: -->
99
<a href="<?= route_to('user_gallery', 15, 12) ?>">View Gallery</a>
1010
<!-- Result: '/users/15/gallery/12' -->

0 commit comments

Comments
 (0)