Search code examples
laravellaravel-4routesparameter-passinglaravel-routing

Passing a param containing a path to a controller


I'm trying to pass a variable who contains a path from a form to a controller function in Laravel 4, with the purpose of download an image. I tried a lot of things but nothing worked for me. If I don't pass the parameter in the route, I get a missing parameter error, and if I pass the parameter in the route, I get a NotFoundHttpException.

Here's my code:

View Form:

{{ Form::open(array('route' => array('download', $myPath))) }}
    {{ Form::submit('Download', array('class' => 'generator')); }}
{{ Form::close() }}

Route:

Route::post('/download/{myPath}', array('uses' => 'ImagesController@download', 'as' => 'download'));

ImagesController function:

public function download($myPath){
    return Response::download($myPath);
}

When I click the submit I'm obtaining a URL like this with NotFoundHttpException:

http://localhost/resizer/public/download/images/myimage.jpg

I don't understand what I'm missing.


Solution

  • You are actually posting two variables. Try this instead

    Route::post('/download/{myFolder}/{myFile}', array('uses' => 'ImagesController@download', 'as' => 'download'));
    

    and in your controller

     public function download($myFolder, $myFile){
            return Response::download($myFolder.'/'.$myFile);
    }