Mister Verleg
Mister Verleg

Reputation: 4293

Laravel multiple route parameters

I am trying to create a route that lets me download a file when given its path:

example.com/download/dogs/beagles/stickypaw.jpg
example.com/download/dogs/germanshepards/woofer.jpg
example.com/download/dogs/alldogs.jpg

Normally I would use named parameters, however in this case that would mean having 3 different routes:

Route::get('/download/package}/{folder}/{filename}',function ($package, $folder,$filePath) {
  $filePath = "$package/$folder/$filePath";
  return Storage::download($filePath);    
});

Route::get('/download/{package?}/{filename}', function ($package, $filePath) {
  $filePath = "$package/$filePath";
  return Storage::download($filePath);
});

Route::get('/download/{filename}', function ($filename) {     
  return Storage::download($filename);
});

Is there a way to get the path after download into a variable?

example:

Route::get('/download/{path}', function ($path) {
   // loop over the path array
});

Upvotes: 0

Views: 50

Answers (1)

Dan
Dan

Reputation: 5358

It's not tested, but should work:

Route::get('/download/{path}', function ($path) {     
    $folders = explode('/', $path);

    // ...
})
    ->where('path', '(.+)');

Upvotes: 1

Related Questions