Skip to content

Commit ecef7f7

Browse files
authored
Merge pull request #7090 from kenjis/fix-docs-route-to-url-to
docs: improve route_to() and url_to()
2 parents c58889b + 2435311 commit ecef7f7

File tree

18 files changed

+69
-30
lines changed

18 files changed

+69
-30
lines changed

system/Common.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -941,18 +941,18 @@ function response(): ResponseInterface
941941

942942
if (! function_exists('route_to')) {
943943
/**
944-
* Given a controller/method string and any params,
944+
* Given a route name or controller/method string and any params,
945945
* will attempt to build the relative URL to the
946946
* matching route.
947947
*
948948
* NOTE: This requires the controller/method to
949949
* have a route defined in the routes Config file.
950950
*
951-
* @param string $method Named route or Controller::method
951+
* @param string $method Route name or Controller::method
952952
* @param int|string ...$params One or more parameters to be passed to the route.
953953
* The last parameter allows you to set the locale.
954954
*
955-
* @return false|string
955+
* @return false|string The route (URI path relative to baseURL) or false if not found.
956956
*/
957957
function route_to(string $method, ...$params)
958958
{

system/HTTP/IncomingRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class IncomingRequest extends Request
6262
*
6363
* Note: This WILL NOT match the actual URL in the browser since for
6464
* everything this cares about (and the router, etc) is the portion
65-
* AFTER the script name. So, if hosted in a sub-folder this will
66-
* appear different than actual URL. If you need that use getPath().
65+
* AFTER the baseURL. So, if hosted in a sub-folder this will
66+
* appear different than actual URI path. If you need that use getPath().
6767
*
6868
* @deprecated Will be protected. Use getUri() instead.
6969
*
@@ -72,7 +72,7 @@ class IncomingRequest extends Request
7272
public $uri;
7373

7474
/**
75-
* The detected path (relative to SCRIPT_NAME).
75+
* The detected URI path (relative to the baseURL).
7676
*
7777
* Note: current_url() uses this to build its URI,
7878
* so this becomes the source for the "current URL"
@@ -511,7 +511,7 @@ private function determineHost(App $config, string $baseURL): string
511511
}
512512

513513
/**
514-
* Returns the path relative to SCRIPT_NAME,
514+
* Returns the URI path relative to baseURL,
515515
* running detection as necessary.
516516
*/
517517
public function getPath(): string

system/HTTP/URI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class URI
9090
*
9191
* Note: The constructor of the IncomingRequest class changes the path of
9292
* the URI object held by the IncomingRequest class to a path relative
93-
* to the SCRIPT_NAME. If the baseURL contains subfolders, this value
93+
* to the baseURL. If the baseURL contains subfolders, this value
9494
* will be different from the current URI path.
9595
*
9696
* @var string

system/Helpers/url_helper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,15 @@ function mb_url_title(string $str, string $separator = '-', bool $lowercase = fa
542542

543543
if (! function_exists('url_to')) {
544544
/**
545-
* Get the full, absolute URL to a controller method
545+
* Get the full, absolute URL to a route name or controller method
546546
* (with additional arguments)
547547
*
548548
* NOTE: This requires the controller/method to
549549
* have a route defined in the routes Config file.
550550
*
551-
* @param string $controller Named route or Controller::method
552-
* @param int|string ...$args One or more parameters to be passed to the route
551+
* @param string $controller Route name or Controller::method
552+
* @param int|string ...$args One or more parameters to be passed to the route.
553+
* The last parameter allows you to set the locale.
553554
*
554555
* @throws RouterException
555556
*/

system/Router/RouteCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ public function environment(string $env, Closure $callback): RouteCollectionInte
10471047
* @param int|string ...$params One or more parameters to be passed to the route.
10481048
* The last parameter allows you to set the locale.
10491049
*
1050-
* @return false|string
1050+
* @return false|string The route (URI path relative to baseURL) or false if not found.
10511051
*/
10521052
public function reverseRoute(string $search, ...$params)
10531053
{

system/Router/RouteCollectionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function getHTTPVerb();
172172
* @param string $search Named route or Controller::method
173173
* @param int|string ...$params
174174
*
175-
* @return false|string
175+
* @return false|string The route (URI path relative to baseURL) or false if not found.
176176
*/
177177
public function reverseRoute(string $search, ...$params);
178178

tests/system/Helpers/URLHelper/MiscUrlTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,4 +884,20 @@ public function urlToMissingRoutesProvider()
884884
],
885885
];
886886
}
887+
888+
public function testUrlToWithSupportedLocaleInRoute()
889+
{
890+
Services::createRequest(new App());
891+
$routes = service('routes');
892+
$routes->add(
893+
'{locale}/path/(:segment)/to/(:num)',
894+
'myController::goto/$1/$2',
895+
['as' => 'path-to']
896+
);
897+
898+
$this->assertSame(
899+
'http://example.com/index.php/en/path/string/to/13',
900+
url_to('path-to', 'string', 13, 'en')
901+
);
902+
}
887903
}

user_guide_src/source/general/common_functions.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,27 +367,30 @@ Miscellaneous Functions
367367

368368
.. php:function:: route_to($method[, ...$params])
369369
370-
:param string $method: The named route alias, or name of the controller/method to match.
371-
:param int|string $params: One or more parameters to be passed to be matched in the route. The last parameter allows you to set the locale.
370+
:param string $method: Route name or Controller::method
371+
:param int|string ...$params: One or more parameters to be passed to the route. The last parameter allows you to set the locale.
372+
:returns: a route (URI path)
373+
:rtype: string
372374

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

377+
.. important:: ``route_to()`` returns a *route*, not a full URI path for your site.
378+
If your **baseURL** contains sub folders, the return value is not the same
379+
as the URI to link. In that case, just use :php:func:`url_to()` instead.
380+
See also :ref:`urls-url-structure`.
381+
375382
Generates a route for you based on a controller::method combination. Will take parameters into effect, if provided.
376383

377384
.. literalinclude:: common_functions/009.php
378385

379-
Generates a route for you based on a named route alias.
386+
Generates a route for you based on a route name.
380387

381388
.. literalinclude:: common_functions/010.php
382389

383390
Since v4.3.0, when you use ``{locale}`` in your route, you can optionally specify the locale value as the last parameter.
384391

385392
.. literalinclude:: common_functions/011.php
386393

387-
.. note:: ``route_to()`` returns a route, not a full URI path for your site.
388-
If your **baseURL** contains sub folders, the return value is not the same
389-
as the URI to link. In that case, just use :php:func:`url_to()` instead.
390-
391394
.. php:function:: service($name[, ...$params])
392395
393396
:param string $name: The name of the service to load

user_guide_src/source/general/common_functions/009.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
// The route is defined as:
4-
$routes->get('users/(:num)/gallery(:any)', 'Galleries::showUserGallery/$1/$2');
4+
$routes->get('users/(:num)/gallery/(:num)', 'Galleries::showUserGallery/$1/$2');
55

66
?>
77

user_guide_src/source/general/common_functions/010.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
// The route is defined as:
4-
$routes->get('users/(:num)/gallery(:any)', 'Galleries::showUserGallery/$1/$2', ['as' => 'user_gallery']);
4+
$routes->get('users/(:num)/gallery/(:num)', 'Galleries::showUserGallery/$1/$2', ['as' => 'user_gallery']);
55

66
?>
77

0 commit comments

Comments
 (0)