I need to compose URLs with parameters that can contain a slash /. For example, the classic /hello/{username}
route. By default, /hello/Fabien
will match this route but not /hello/Fabien/Kris
. I would to ask you how can I do it in Slim 3 framework.
Asked
Active
Viewed 1,752 times
5

User
- 314
- 2
- 3
- 15
-
By adding another placeholder `/hello/{username}/{name}`? – revo Aug 29 '16 at 10:20
-
@revo it is not possible, because there can be 2 or more slashes and I need to store everything after /hello/ to one variable, in this case, parameter – User Aug 29 '16 at 10:25
2 Answers
8
For “Unlimited” optional parameters, you can do this:
$app->get('/hello[/{params:.*}]', function ($request, $response, $args) {
$params = explode('/', $request->getAttribute('params'));
// $params is an array of all the optional segments
});

revo
- 47,783
- 14
- 74
- 117
1
You can just as well use $args
:
$app->get('/hello[/{route:.*}]', function ($request, $response, $args) {
$route = $args['route']; // Whole Route
$params = explode('/', $route); // Route split
});

Anuga
- 2,619
- 1
- 18
- 27