代码之家  ›  专栏  ›  技术社区  ›  Abhishek

从Laravel图像url中删除“存储”

  •  0
  • Abhishek  · 技术社区  · 7 年前

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

    https://example.com/a/xyz.png

    https://example.com/storage/a/xyz.png -可接近

    但在本地,可以访问不带存储的URL。

    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应该在没有存储字的情况下工作。它不起作用。在我的本地机器上,由代客泊车管理的同一代码在没有存储关键字的情况下运行良好

    1 回复  |  直到 7 年前
        1
  •  2
  •   Tpojka    7 年前

    问题是执行命令 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存储:链接