【问题标题】:How to restrict Response:download base on route filter?如何限制响应:基于路由过滤器的下载?
【发布时间】:2015-03-08 01:39:34
【问题描述】:

详情

  • 我使用的是 Laravel 4.0
  • 我不确定为什么我的所有用户徽标都无法加载
  • 我 100% 确信这些徽标位于正确的路径、正确的位置。
  • 它们在我的应用程序中显示良好,除了 URL /api/url/decode
  • 如果他们为我提供了我设置的正确 api_key,我想允许 /files/logo_path/{id} url。
  • 再次请注意,URL = /files/logo_path/{id} 并非所有人都可以访问。

  • 谁能告诉我这里做错了什么?

这是我尝试过的

routes.php

// API
Route::group(array('prefix' => 'api'), function(){
    Route::get('url', array('before' => 'api', 'uses' => 'UrlController@index'));
    Route::get('url/decode', 'UrlController@decode');

    Route::get('files/logo_path/{id}', function($id)
        {
            $logo_path = app_path() . '/files/logo_path/' . User::find($id)->logo_path;
            return Response::download($logo_path);
        });
});

filters.php

// API
Route::filter('api', function() {

    if (Input::get('key') != '*********')
    {
        return Response::view('errors.404', array(), 404);
    }
});

结果

更新

<img id="Company Logo" src="/files/logo_path/{{{ $distributor['user']['id'] or '' }}}" alt="logo" height="60px" width="200px">

【问题讨论】:

  • 能否添加链接到图片的代码?
  • 当然可以。给我一百万。
  • @lukasgeiter:我更新了我的帖子。
  • 如果你去 /files/logo_path/{an id that exists} 你会得到什么?你有图像吗?您是否尝试过使用干预并返回图像响应?
  • 如果我去它,它不会显示,但它会下载该 id 的图像。

标签: php laravel laravel-4 routes


【解决方案1】:

您的问题与过滤器无关。问题在于您如何尝试显示数据。

Response::download() 是要求浏览器提示用户保存或打开文件。

但是您将链接代码放在img<> html 中 - 用于在页面上显示内嵌图像。

它们不能混合在一起。

如果您想允许用户只下载他们被允许访问的图像 - 您将保持当前路线不变 - 但将您的查看代码更改为

<a href="{{{ /files/logo_path/{{{ $distributor['user']['id'] or '' }}}""> Click here to download your file</a>

如果您希望允许用户查看页面上的图像(仅当他们有权访问) - 那么您将保持查看代码不变:

<img id="Company Logo" src="/files/logo_path/{{{ $distributor['user']['id'] or '' }}}" alt="logo" height="60px" width="200px">

但是将您的路线代码更改为:

Route::get('files/logo_path/{id}', function($id)
        {
            $logo_path = app_path() . '/files/logo_path/' . User::find($id)->logo_path;
            return file_get_contents($logo_path);
        });

【讨论】:

    猜你喜欢
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2017-08-10
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多