Skip to content

Commit d7cda93

Browse files
authored
Merge pull request #7652 from kenjis/fix-url-to-error-msg
fix: `url_to()` error message
2 parents e62b539 + 2ff1dc8 commit d7cda93

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,11 @@ protected function buildReverseRoute(string $from, array $params): string
12391239
// Build our resulting string, inserting the $params in
12401240
// the appropriate places.
12411241
foreach ($matches[0] as $index => $pattern) {
1242+
if (! isset($params[$index])) {
1243+
throw new InvalidArgumentException(
1244+
'Missing argument for "' . $pattern . '" in route "' . $from . '".'
1245+
);
1246+
}
12421247
if (! preg_match('#^' . $pattern . '$#u', $params[$index])) {
12431248
throw RouterException::forInvalidParameterType();
12441249
}

tests/system/Helpers/URLHelper/MiscUrlTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Router\Exceptions\RouterException;
1818
use CodeIgniter\Test\CIUnitTestCase;
1919
use Config\App;
20+
use InvalidArgumentException;
2021

2122
/**
2223
* @backupGlobals enabled
@@ -900,4 +901,20 @@ public function testUrlToWithSupportedLocaleInRoute()
900901
url_to('path-to', 'string', 13, 'en')
901902
);
902903
}
904+
905+
/**
906+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/7651
907+
*/
908+
public function testUrlToMissingArgument()
909+
{
910+
$this->expectException(InvalidArgumentException::class);
911+
$this->expectExceptionMessage('Missing argument for "([a-zA-Z]+)" in route "([a-zA-Z]+)/login".');
912+
913+
$routes = Services::routes();
914+
$routes->group('(:alpha)', static function ($routes) {
915+
$routes->match(['get'], 'login', 'Common\LoginController::loginView', ['as' => 'loginURL']);
916+
});
917+
918+
url_to('loginURL');
919+
}
903920
}

0 commit comments

Comments
 (0)