Welcome Guest, Not a member yet? Register   Sign In
Route not work with regex
#1

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?
Reply
#2

Check the route regex with `php spark routes`.
Reply
#3

(This post was last modified: 01-27-2022, 11:46 PM by iRedds.)

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 
Reply
#4

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB