laravel 在流明中创建存储链接

23c0lvtd  于 2023-04-22  发布在  其他
关注(0)|答案(2)|浏览(132)

我看到了一些关于…人们说要运行这个命令的问题
ln -s ../storage/app/public public/storage
它可以工作,它复制文件夹,但当另一个文件/图像添加到存储文件夹时,它不会更新,您必须再次运行命令才能获得它。
我如何使它等于Laravel,公共文件夹总是“复制”存储文件?
版本:“laravel/lumen-framework”:“^8.3.1”,“league/flysystem”:“一点一”

jtoj6r0c

jtoj6r0c1#

Lumen/Laravel创建符号链接的方法:

app()->make('files')
    ->link(storage_path('app/public'), rtrim(app()->basePath('public/storage/'), '/'));
  • 已更新。* 创建命令示例:
php artisan make:command FilesLinkCommand

然后将创建下一个文件:app/Console/Commands/FilesLinkCommand.php
打开它,改变一些东西:
1.更好的命令签名:protected $signature = 'files:link';
1.命令说明:protected $description = 'Create symlink link for files';
1.命令代码,update方法handle():

public function handle(): void
{
    // check if exists public/storage
    if ( file_exists(app()->basePath('public/storage/')) ) {
        $this->error("Symlink exists!");
        exit(0);
    }
    // check if exists storage/app/public
    if ( !file_exists(storage_path('app/public')) ) {
        $this->error("Storage folder [app/public] not exists!");
        exit(0);
    }
    // actually, we can create folders if not exists
    // using mkdir() php function, or File::makeDirectory()
    // but the second one still uses mkdir() behind the scene

    app()->make('files')
        ->link(storage_path('app/public'), rtrim(app()->basePath('public/storage/'), '/'));
    $this->info("Symlink created!");
}

这就是我们需要的!
使用:php artisan files:link
如果第一次运行命令时,您将看到消息Symlink created,下次将更改为Symlink exists!
php函数symlink()可以做同样的事情

shyt4zoc

shyt4zoc2#

首先,请确认storage/app/public文件夹存在。如果不存在,请创建一个新文件夹。
然后运行:

ln -sfv ~/path/to/storage/app/public ~/path/to/public/storage

然后,将文件移动到storage/app/public
现在,您应该能够访问如下资产:

http://localhost:8000/storage/logo.png

相关问题