Slim 3 - Slash as a part of route parameter

2019-06-25 06:09发布

问题:

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
});