给 Lumen 添加 storage:link 命令

添加并引用全局可用的辅助函数:

bootstrap/app.php:

1
require_once __DIR__ . '/helpers/functions.php';

bootstrap/helpers/functions.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 设置 public_path 方法
*/
if (!function_exists('public_path')) {
/**
* Return the path to public dir
* @param null $path
* @return string
*/
function public_path($path = null)
{
return rtrim(app()->basePath('public/' . $path), '/');
}
}

添加 StorageLinkCommand 类:

app/Console/Commands/StorageLinkCommand.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class StorageLinkCommand extends Command
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'storage:link';

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

/**
* Execute the console command.
*/
public function handle()
{
// 创建 storage 文件夹
if (!file_exists(storage_path('app/public'))) {
mkdir(storage_path('app/public'));
}

// 如果软链接已存在则不再创建
if (file_exists(public_path('storage'))) {
return $this->error('The "public/storage" directory already exists.');
}

// 创建软链接
$this->laravel->make('files')->link(
storage_path('app/public'), public_path('storage')
);

$this->info('The [public/storage] directory has been linked.');
}
}

当然,除了 StorageLinkCommand 这个类,你也可以在 Laravel 框架的 vendor/composer/autoload_classmap.php 中找到其他命令所对应的文件路径,把他们复制到 Lumen 项目中就可以正常使用了。