Joe Scotto Joe Scotto - 1 year ago 75
reST (reStructuredText) Question

Slim framework url as file path

I'm new to using Slim and am trying to create a simple file hosting site. I'm trying to set my current directory via the url using

$app->get()
. Is there a way where I can have a url such as:
site.com/panel/documents/text/word/etc
where everything after
panel
is interpreted as the current path? I currently have this code:

$app->get('/panel/{path}', function ($request, $response) {
$path = $request->getAttribute('path');

return $path;
});


The issue is that I'm only able to return the
path
when only one path is set, I.E.
/panel/documents
returns
documents
. If I do something such as
/panel/documents/text
it will return a not found error. Any help would be great. Thanks!

Answer Source

Assuming you are using the latest Slim v3, you can use placeholders to achieve your goal as per the documentation: http://www.slimframework.com/docs/objects/router.html#route-placeholders

Look for unlimited optional params

$app->get('/news[/{params:.*}]', function ($request, $response, $args) {
    $params = explode('/', $request->getAttribute('params'));

    // $params is an array of all the optional segments
});

In your case it would be:

$app->get('/panel/{path:.*}', function ($request, $response) {
    $path = $request->getAttribute('path');

    return $path;
});