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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Route placeholders:
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
});
回答2:
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
});