codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: Reverse routing fails for routes with regex containing parenthesis #7237

Closed csg-luke closed 1 year ago

csg-luke commented 1 year ago

PHP Version

8.0

CodeIgniter4 Version

4.3.1

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

apache

Database

MariaDB 10.6

What happened?

Reverse routing is failing for routes with regex containing parenthesis

Error: preg_match(): Compilation failed: missing closing parenthesis at offset 19, vendor/codeigniter4/framework/system/Router/RouteCollection.php line: 1238

Looking at the RouteCollection.php line 1211 preg_match_all('/\(([^)]+)\)/', $from, $matches); It appears the regex is only matching to the first parenthesis and losing the rest.

Steps to Reproduce

$routes->addPlaceholder('imgFileExt', '^.*\.(?:jpg|png)$');
$routes->get('images/(:imgFileExt)', 'Images::getFile/$1');
$file = 'test.jpg';
url_to('Images::getFile', $file)

Expected Output

/images/test.jpg

Anything else?

No response

totoprayogo1916 commented 1 year ago

working with:

$routes->addPlaceholder('imgFileExt', '.*(?:jpg|png)');
kenjis commented 1 year ago

I don't want to permit Route like images/(^.*\.(?:jpg|png)$). I think it would be fine to say that regular expressions with double parentheses such as this are not supported.

+--------+----------------------------+------+-------------------------------------+----------------+---------------+
| Method | Route                      | Name | Handler                             | Before Filters | After Filters |
+--------+----------------------------+------+-------------------------------------+----------------+---------------+
| GET    | /                          | »    | \App\Controllers\Home::index        |                | toolbar       |
| GET    | images/(^.*\.(?:jpg|png)$) | »    | \App\Controllers\Images::getFile/$1 | <unknown>      | <unknown>     |
+--------+----------------------------+------+-------------------------------------+----------------+---------------+
kenjis commented 1 year ago

Workaround:

$routes->addPlaceholder('img', '.*\.jpg|.*\.png');
$routes->get('images/(:img)', 'Home::index/$1');
kenjis commented 1 year ago

Try #7880