【问题标题】:How do I remove /storage from the URL for public images in my Laravel project?如何从 Laravel 项目中公共图像的 URL 中删除 /storage?
【发布时间】:2019-06-26 23:17:30
【问题描述】:

我想要实现的是从 URL 中删除 /storage,这样最终它是 www.example.com/images/x.jpg 而不是默认的 www.example.com/storage/x.jpg

我已经尝试从url 中的config/filesystems.php 中删除 /storage,如下所示:

// Original

'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL') . '/storage',
        'visibility' => 'public',
 ],

// Modified

'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL'), // <-- changed
        'visibility' => 'public',
 ],

但它不起作用。我认为问题在于,如果没有前缀,它将被视为公用文件夹中的文件。

是否有可能实现我想要实现的目标?

【问题讨论】:

    标签: php filesystems laravel-5.7 laravel-storage


    【解决方案1】:

    完成此操作的最直接方法是添加新磁盘。通过这种方式,您可以将新模式应用于您的图像,而不会影响现有文件和 url,以及许多其他好处。

    步骤 1

    将您的磁盘添加到 config/filesystems.php

    'images' => [
            'driver' => 'local',
            'root' => storage_path('app/public/images'),
            'url' => env('APP_URL') . '/images',
            'visibility' => 'public',
     ],
    

    以下是如何从控制器save file uploads 到新磁盘的示例:

    // storeAs: path, filename, disk
    $request->file('image')->storeAs('/', 'x.jpg', 'images')
    

    这就是您如何生成指向看起来像http://example.com/images/x.jpg 的图像的链接:

    Storage::disk('images')->url('x.jpg')
    

    第二步

    以下是从新路径提供文件的三个不同选项(您只需选择一个):

    选项 1

    在您的公共目录中创建符号链接。

    ln -s /var/www/example.com/storage/app/public/images /var/www/example.com/public/images
    

    这与 Laravel 用于默认公共磁盘(/storage URL)的方法相同。

    从 Laravel 7 开始,您可以修改 config/filesystems.php 来管理额外的符号链接:

        /*
        |--------------------------------------------------------------------------
        | Symbolic Links
        |--------------------------------------------------------------------------
        |
        | Here you may configure the symbolic links that will be created when the
        | `storage:link` Artisan command is executed. The array keys should be
        | the locations of the links and the values should be their targets.
        |
        */
        'links' => [
            public_path('storage') => storage_path('app/public'),
            public_path('images')  => storage_path('app/public/images'),
        ],
    

    选项 2

    在你的 Laravel 应用程序中创建一个路由来提供图片:

    Route::get('images/{file}', function ($file) {
        return Storage::disk('images')->response($file);
        // or to trigger downloads:
        // return Storage::disk('images')->download($file);
    });
    

    此选项的缺点是它使用 PHP 进程为每个图像提供服务,而不是像选项 1 和 3 那样由网络服务器处理。

    选项 3

    在您的网络服务器中创建重写规则。

    在 nginx 中,它可能看起来像这样:

    location /images/ {
        root /var/www/example.com/storage/app/public/;
    }
    

    在 Apache 中,您可以使用 alias:

    Alias "/images" "/var/www/example.com/storage/app/public/images"
    

    【讨论】:

      猜你喜欢
      • 2021-08-08
      • 2015-06-09
      • 1970-01-01
      • 2017-02-27
      • 2016-06-09
      • 2019-03-30
      • 2013-03-13
      • 2015-04-06
      相关资源
      最近更新 更多