Reputation: 2406
From a personal Symfony 5 project, I want to simulate a web file browser.(of directories located in server side)
My question is about routing, given possibles urls :
As you can see, the arborescence may contain a variable number of hierarchical directories. With the Symfony Router, How can I manage theses url with one controller ?
The content of the contoller should look like :
/**
* @Route("???")
*/
public function browse(): Response
{
// get all parameters (example : ['directory-B', 'directory-B-1', 'directory-B-1-1'])
// From serverside, check in the filesystem if the path 'directory-B/directory-B-1/directory-B-1-1
// If yes : return the list of files or directories contained in 'directory-B/directory-B-1/directory-B-1-1'
// If no : return an error
}
My question is here : @Route("???")
, how can I do what I want with Symfony routing ?
Question bonus : in directories, there will be only text files. With the routing system again, how can I create only one controller for all dynamic routes finishing by ".txt" ?
Examples of url :
Upvotes: 1
Views: 667
Reputation: 47495
Just have a route like this:
share:
path: /browse/{path}
controller: App\Controller\BrowserController::browse
requirements:
path: (.+)
Then in your controller simply split the incoming path to account for the directories, check their existence, etc:
public function browse(string $path): Response
{
$pathParts = explode('/', $path);
// do what you need here.
}
So you would end with things like:
| url path | partParts |
|----------- --------------|-----------------------------------------------|
| /browse/foo | ['foo'] |
| /browse/foo/bar | ['foo', 'bar'] |
| /browse/foo/bar/file.txt | ['foo', 'bar', 'file.txt'] |
With the path parts you would be able to check for path existence, if it can be read, implement your own permission system, etc.
Upvotes: 3