模块化开发静态资源映射
在使用 nwidart/laravel-modules 进行模块化开发时,每个模块下都会有单独的静态资源(CSS、JS等)文件夹,发现包本身并没有提供比较便利的资源引用方法。以下是用 Artisan 命令行生成软链的解决办法
生成 Command:
php artisan make:command ModuleAssetLink
app/Consoles/Commands/ModuleAssetsLink.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ModuleAssetsLink extends Command
{
protected $signature = 'module:assets-link
{module : Module name}
{link : Link name}';
protected $description = '将模块静态资源映射到项目 `public` 目录';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$module = $this->argument('module');
$link = $this->argument('link');
if (file_exists(public_path($link))) {
return $this->error("The 'public/{$link}' directory already exists.");
}
$this->laravel->make('files')->link(
module_path($module) . '/Resources/assets', public_path($link)
);
$this->info("The [public/{$link}] directory has been linked.");
}
}
使用:
$ php artisan module:assets-link Admin admin-assets
The [public/admin-assets] directory has been linked.
本作品采用《CC 协议》,转载必须注明作者和本文链接
映射完了,在模块中怎么应用?我直接输入url+public中映射的资源地址,显示404错误,把映射目录当成路由处理了