codeigniter4 / codeigniter4/CodeIgniter4

Missing argument in route when using `url_to` or `route_to` with regex routes

Open
#9,250 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
6k
Forks
2k
Avg merge
1d 11h
Merged PRs (30d)
73

Description

### 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_to` or `route_to` to refer to it:

```php
$routes->get('/test(/(:any))?', 'Test::direct/$1', ['as' => 'test']);
```
Linking as below works:
```html
No parameter
1 parameter
2 parameters
```

But using both `url_to` or `route_to` fails:

```php
No parameter
1 parameter
2 parameters
```

### Steps to Reproduce

1. Add the new routes in `Routes.php`:
```php
$routes->get('/test(/(:any))?', 'Test::direct/$1', ['as' => 'test']);
$routes->get('/test_urlto(/(:any))?', 'Test::urlto/$1', ['as' => 'urlto']);
$routes->get('/test_routeto(/(:any))?', 'Test::routeto/$1', ['as' => 'routeto']);
```
2. Create the `Test` controller:
```php
$params]);
}

public function urlto(...$params): string
{
return view('test_urlto', ['params' => $params]);
}

public function routeto(...$params): string
{
return view('test_routeto', ['params' => $params]);
}
}
```

3. Create the respective views:

A) direct view

```html


Test

Testing CI4 Routing with Parameters

Test using string directly



Passed Parameters





  • ```
    B) view using `url_to`

    ```html


    Test

    Testing CI4 Routing with Parameters


    Test using url_to


    Passed Parameters





    • ```

      4. Access those views at: /test and /test_urlto

      ### Expected Output

      No `InvalidArgumentException`, `Missing argument for "(/(:any)" in route "test_urlto(/(:any))?"` while using both `url_to` and `route_to` .

      ### Anything else?

      - One can try it with `public bool $multipleSegmentsOneParam = true;` or `public bool $multipleSegmentsOneParam = false;` in `Routing.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.

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure using the routes in Routes.php and compare url_to and route_to with both Routing.php multipleSegmentsOneParam settings. Start by tracing route parameter handling from the named regex routes; done means all shown links generate without InvalidArgumentException and preserve the expected parameters.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.