CodeIgniter Forums
Route not work with regex - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Route not work with regex (/showthread.php?tid=81148)



Route not work with regex - mylastof - 01-27-2022

I have reoutes with custom placeholder like:

Code:
// route
$routes->addPlaceholder('unique', '([a-z0-9]{40}|all)$');
$routes->addPlaceholder('tab', '(?:^|done|in-progress)$');

$routes->get('(:unique)', 'Pages::browse/$1');
$routes->get('(:unique)/(:tab)', 'Pages::browse/$1/$2');

Code:
// controller
public function browse($o = 'one', $t= 'two')
{
    echo($o);
    echo('<br/>');
    echo($t);
}

When accessed like http://localhost:8080/all
show
Code:
all
two

but when accessed with http://localhost:8080/all/done just show 404.

by edit the route with
Code:
$routes->get('(:unique)/(:any)', 'Pages::browse/$1/$2');
show
Code:
all
done

whats wrong with the regex?


RE: Route not work with regex - kenjis - 01-27-2022

Check the route regex with `php spark routes`.


RE: Route not work with regex - iRedds - 01-27-2022

PHP Code:
$routes->addPlaceholder('unique''[a-z0-9]{40}|all');
$routes->addPlaceholder('tab''done|in-progress');

$routes->get('(:unique)''Pages::browse/$1');                    // #^([a-z0-9]{40}|all)$#u
$routes->get('(:unique)/(:tab)''Pages::browse/$1/$2');   // #^([a-z0-9]{40}|all)/(done|in-progress)$#u

in your implementation
// #^(([a-z0-9]{40}|all)$)$#u
// #^(([a-z0-9]{40}|all)$)/((?:^|done|in-progress)$)$#u 



RE: Route not work with regex - mylastof - 01-28-2022

(01-27-2022, 11:43 PM)iRedds Wrote:
PHP Code:
$routes->addPlaceholder('unique''[a-z0-9]{40}|all');
$routes->addPlaceholder('tab''done|in-progress');

$routes->get('(:unique)''Pages::browse/$1');                    // #^([a-z0-9]{40}|all)$#u
$routes->get('(:unique)/(:tab)''Pages::browse/$1/$2');  // #^([a-z0-9]{40}|all)/(done|in-progress)$#u

in your implementation
// #^(([a-z0-9]{40}|all)$)$#u
// #^(([a-z0-9]{40}|all)$)/((?:^|done|in-progress)$)$#u 

Thanks @iRedds