Laravel Response :: download(),不暴露下载路径

时间:2015-10-05 23:06:08

标签: laravel-4 download laravel-routing

我对Laravel 4.2中的Response :: download()感到困惑

我想触发下载ZIP存档而不暴露文件的实际公共路径..

这是我在访问路线时遇到的错误

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

我的ROUTE写得像这样:

/*
/ Download Package (GET)
*/
Route::get('/download-packages/{$package}', array(
    'as'    => 'download-package',
    'uses'  => 'DownloadController@getPackageDownload'
));

在Controller中,getPackageDownload函数编写如下:

public function getPackageDownload( $package ){

    $file = public_path() . "/downloads/packages/" . $package . ".zip" ;
    $name = $package . ".zip";

    $headers = array(
        'Content-Type' => 'application/zip',
    );

    return Response::download($file, $name, $headers);
}

我的观看按钮代码:

<a href="{{ URL::route('download-package',[$package->package]) }}" target="_blank">Download</a></div>

视图中生成的下载链接是这样的

http://app.dev/laravel/public/download-packages/package-name

它与ZIP文件的实际公共位置不同,但这是理想的,我的理解是应该在getPackageDownload函数中触发响应。

1 个答案:

答案 0 :(得分:1)

所以,在离开代码并以精神焕发的眼睛回到它之后,事实证明我只是路由错误,路由参数$ package不应该有&#34; $&#34;在它面前..

/*
/ Download Package (GET)
*/
Route::get('/download-packages/{package}', array(
    'as'    => 'download-package',
    'uses'  => 'DownloadController@getPackageDownload'
));