skipperbent / simple-php-router

Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

About matching with regular expressions

riku22 opened this issue · comments

commented

Hello.

I created a routing like the one below, but it doesn't work.
What should I do now?

SimpleRouter::get('/api/test/v{api_version}/{name}', function($api_version, $name){
var_dump($api_version);
var_dump($name);
})
->where([
'api_version' => '[0-9]+',
'name' => '[a-zA-Z]+',
]);

I'm expecting it to work with a URL like /api/test/v1/abc, but I get a 404 error.
I would appreciate any advice.

Did u try to remove the v in the api_version?

commented

Hello.

In the end, I decided to remove the leading "v" in the function and use it.

SimpleRouter::get('/api/test/{api_version}/{name}', function($api_version, $name){
$api_version = preg_replace('/^v/i', '', $api_version);
var_dump($api_version);
var_dump($name);
})
->where([
'api_version' => 'v[0-9]+',
'name' => '[a-zA-Z]+',
]);

Thank you.