match http|https with fasteRoute in Slim3

110 Views Asked by At

here my sample FastRoute for slim3 :

$app->get('/api/search/[{domaine}[/{notused:.+}]]', function ($request, $response, $args) {
    return $this->renderer->render($response, 'index.phtml', $args);
});

with this FastRoute "regex" ([{domaine}[/{notused:.+}]]), I match :

/api/search/sample.com  
/api/search/sample.com/test  
/api/search/  

And $args['domaine'] return "sample.com".

but I want to match this too :

/api/search/http://sample.com  
/api/search/https://sample.com  

add new route like this work :

$app->get('/api/search/http://[{domaine}[/{notused:.+}]]' ...   
$app->get('/api/search/https://[{domaine}[/{notused:.+}]]' ...  

But it's better to have just one line.

Any ideas ?

1

There are 1 best solutions below

1
On

It would be better when you use an url encoded url as query parameter.

But when you really what to use this, you could regex the http(s) part as well

$app->get('/api/search/{urlSchema:https?}://[{domaine}[/{notused:.+}]]', ..);

that means it requires http and then have 0-1 s.