Repository navigation
Missing argument in route when using url_to or route_to with regex routes #9250
Description
Activity
- addedbugVerified issues on the current code behavior or pull requests that will fix themVerified issues on the current code behavior or pull requests that will fix them
on Nov 2, 2024 - changed the title
[-]Bug: Missing argument in route[/-][+]Bug: Missing argument in route when using `url_to` or `route_to` with regex routes[/+]on Nov 2, 2024 Using the
(:any)placeholder in the route does not mean that the parameter value is optional. You must always put a value.You can use:
url_to('urlto', ''), although in general I don't think it will behave as you expect - the route will not be found.In any case, this is not a bug.
- removedbugVerified issues on the current code behavior or pull requests that will fix themVerified issues on the current code behavior or pull requests that will fix them
on Nov 4, 2024 I've been trying to use regex in routes for a long time. It failed.
Try to create several routes to get rid of the "?".$routes->get('/test/(:any)', 'Test::direct/$1', ['as' => 'test_ext']); $routes->get('/test', 'Test::direct/$1', ['as' => 'test']);
To work, you need to specify a string for :any and not an array
url_to('urlto', 'param1/param2/param3')noturl_to('urlto', 'param1', 'param2', 'param3')Reacted by Bernhard EndersUsing the
(:any)placeholder in the route does not mean that the parameter value is optional. You must always put a value.You can use:
url_to('urlto', ''), although in general I don't think it will behave as you expect - the route will not be found.In any case, this is not a bug.
I couldn't disagree more... Because I'm using regular expressions (note the ? char) with (:any). And it also fails for '(/(.+))?' as mentioned in this carefully written bug report.
And, by running the examples, one can see that routing mechanism is working flawlessly with or without arguments. Problem arises when using url_to. For me this is, at least, a framework inconsistency or missing feature (if not a bug). 😥
I've been trying to use regex in routes for a long time. It failed. Try to create several routes to get rid of the "?".
$routes->get('/test/(:any)', 'Test::direct/$1', ['as' => 'test_ext']); $routes->get('/test', 'Test::direct/$1', ['as' => 'test']);To work, you need to specify a string for :any and not an array
url_to('urlto', 'param1/param2/param3')noturl_to('urlto', 'param1', 'param2', 'param3')Yeah! Unfortunately every time this subject comes up it is treated as a feature, read the docs etc....
So I kindly suggest to remove the partial regex support from routes (a great loss since routing works fine with regex, only additional framework related functions like
url_toorroute_todo not support it).We could also explicitly says in the docs not to use the special '?' char in routes because of missing support.
As I carefully showed in this bug report examples, regex with '?' works with or without arguments. Problem is framework support from related helper functions.
Well i have tested out your wanting functionality i manage to make it work with the following:
your routes are wrong in the first place and how you use the url_to() is wrong also.
Try it like this in routes file
$routes->get('/test/(:any)', 'Test::direct/$1', ['as' => 'test_ext']); $routes->get('/test', 'Test::direct', ['as' => 'test']);And this for the controller
use CodeIgniter\Controller; class Test extends Controller { public function direct(...$params): string { echo url_to('test_ext', ...$params); echo '<br />'; echo url_to('test'); echo '<br />'; print_r($params);die(); } }- changed the title
[-]Bug: Missing argument in route when using `url_to` or `route_to` with regex routes[/-][+]Missing argument in route when using `url_to` or `route_to` with regex routes[/+]on Feb 19, 2026
PHP Version
8.3
CodeIgniter4 Version
CodeIgniter 4.5.5
CodeIgniter4 Installation Method
Composer (using
codeigniter4/appstarter)Which operating systems have you tested for this bug?
Debian 12
Which server did you use?
fpm-fcgi
Database
N/A
What happened?
The following route definition works fine as long as you don't use
url_toorroute_toto refer to it:Linking as below works:
But using both
url_toorroute_tofails:Steps to Reproduce
Routes.php:Testcontroller:A) direct view
B) view using
url_toExpected Output
No
InvalidArgumentException,Missing argument for "(/(:any)" in route "test_urlto(/(:any))?"while using bothurl_toandroute_to.Anything else?
One can try it with
public bool $multipleSegmentsOneParam = true;orpublic bool $multipleSegmentsOneParam = false;inRouting.php. The error is the same in both situations (just the parameters are treated differently, as expected).One can also use a regex like
'(/(.+))?'instead of(/(:any))?. The error remains.