【问题标题】:Remove 'storage' from Laravel image url从 Laravel 图像 url 中删除“存储”
【发布时间】:2019-02-08 09:06:41
【问题描述】:

我已经上传了一张图片到服务器,除了我需要在 URL 中添加storage 来访问来自 Laravel 的公共目录的图片之外,一切都设置正确。

https://example.com/a/xyz.png - 不可访问

https://example.com/storage/a/xyz.png - 可访问

但是在本地可以访问没有存储的 URL。

NGINX

root /var/www/example.in/live/public/;
index index.html index.php index.htm index.nginx-debian.html;
server_name example.in www.example.in;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

location ~ /\.ht {
        deny all;
}

这不是我需要从 URL 中隐藏存储字的问题。我的问题是图像的 URL 默认情况下应该在没有存储字的情况下工作。它不工作。我的本地机器上由代客管理的相同代码在没有存储关键字的情况下工作正常

【问题讨论】:

标签: php laravel nginx


【解决方案1】:

问题是执行命令php artisan storage:link 它应该在那个位置(即public_path('storage'))。这是默认行为。您可以手动链接到想要的位置:

ln -s /absolute/path/to/project_root/storage/app/public /absolute/path/to/project_root/public/wanted-name-of-directory

或使用自定义命令扩展本机命令。对于后一种解决方案,请遵循 this answer 中的逻辑。应该是这样的:

  1. php artisan make:command CustomStorageLinkCommand

  2. 然后从新创建的文件中删除所有并使用此代码:

<?php

namespace App\Console\Commands;

use Illuminate\Foundation\Console\StorageLinkCommand;

class CustomStorageLinkCommand extends StorageLinkCommand
{
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a symbolic link from "public/a" to "storage/app/public"';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        if (file_exists(public_path('a'))) {
            return $this->error('The "public/a" directory already exists.');
        }
        $this->laravel->make('files')->link(
            storage_path('app/public'), public_path('a')
        );
        $this->info('The [public/a] directory has been linked.');
    }
}
  1. 执行php artisan storage:link 命令

【讨论】:

  • 感谢您的回答,但这不是正确的解决方案。在我的位置,我有相同的目录结构public/storage/a/,但它正在工作localhost/a/xyz.img。此外,目录a 只是例如会有n 目录,如a 具有不同的名称。理想的位置是在 public 的存储文件夹内。我不明白它是如何在本地机器上工作的,而不是在服务器上。
  • 所以,最后的结论是默认存储在 URL 中。我的本地系统代客以某种方式操纵了没有存储字的 URL。
猜你喜欢
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 2018-05-02
  • 2017-08-13
  • 2018-03-15
  • 2021-04-25
相关资源
最近更新 更多