Skip to content

Commit c068a79

Browse files
committed
docs: update explanation about route_to() and url_to()
1 parent 6414095 commit c068a79

File tree

7 files changed

+42
-20
lines changed

7 files changed

+42
-20
lines changed

user_guide_src/source/general/common_functions.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,16 @@ Miscellaneous Functions
333333

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

336-
Generates a URI path (route) 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 :ref:`reverse-routing` and :ref:`using-named-routes`.
339+
.. literalinclude:: common_functions/009.php
340+
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.
340346

341347
.. php:function:: service($name[, ...$params])
342348
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/helpers/url_helper.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ The following functions are available:
365365
This is useful because you can still change your routes after putting links
366366
into your views.
367367

368+
For full details, see the :ref:`reverse-routing` and :ref:`using-named-routes`.
369+
368370
.. php:function:: url_is($path)
369371
370372
:param string $path: The path to check the current URI path against.

user_guide_src/source/incoming/routing.rst

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,13 @@ Reverse routing allows you to define the controller and method, as well as any p
278278
to, and have the router lookup the current route to it. This allows route definitions to change without you having
279279
to update your application code. This is typically used within views to create links.
280280

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

286286
.. literalinclude:: routing/029.php
287287

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-
293288
.. _using-named-routes:
294289

295290
Using Named Routes
@@ -304,11 +299,6 @@ with the name of the route:
304299

305300
This has the added benefit of making the views more readable, too.
306301

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-
312302
Routes with any HTTP verbs
313303
==========================
314304

user_guide_src/source/incoming/routing/029.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
?>
77

8-
<!-- Generate the URI path (route) to link to user ID 15, gallery 12: -->
9-
<a href="<?= route_to('Galleries::showUserGallery', 15, 12) ?>">View Gallery</a>
10-
<!-- Result: '/users/15/gallery/12' -->
8+
<!-- Generate the URI to link to user ID 15, gallery 12: -->
9+
<a href="<?= url_to('Galleries::showUserGallery', 15, 12) ?>">View Gallery</a>
10+
<!-- Result: 'http://example.com/users/15/gallery/12' -->

user_guide_src/source/incoming/routing/030.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
?>
77

8-
<!-- Generate the URI path (route) to link to user ID 15, gallery 12: -->
9-
<a href="<?= route_to('user_gallery', 15, 12) ?>">View Gallery</a>
10-
<!-- Result: '/users/15/gallery/12' -->
8+
<!-- Generate the URI to link to user ID 15, gallery 12: -->
9+
<a href="<?= url_to('user_gallery', 15, 12) ?>">View Gallery</a>
10+
<!-- Result: 'http://example.com//users/15/gallery/12' -->

0 commit comments

Comments
 (0)