从Laravel 5视图中访问供应商文件的简单而干净的方法是什么?



我已经在我的Laravel 5项目中安装了bootstrap-calendar(使用wer)。Bootstrap-calendar被安装到我的laravel/vendor/Bootstrap-calendar/文件夹。

要使用引导日历,我需要在我的视图中引用CSS/JS文件,从:

laravel/vendor/bootstrap-calendar/css
laravel/vendor/bootstrap-calendar/js

是否有一个简单而干净的方法来使用这些文件在我的项目,而不必将它们复制到公共文件夹?

你可以使用Laravel Mix(在Laravel <= 5.3中被称为Laravel Elixir)在你的应用程序的资源上执行这个和其他功能。

Laravel>= 5.4:

mix.copy('vendor/bootstrap-calendar/css', 'public/bootstrap-calendar/css');
mix.copy('vendor/bootstrap-calendar/js', 'public/bootstrap-calendar/js');

Laravel <= 5.3:

elixir(function(mix) {
    mix.copy('vendor/bootstrap-calendar/css', 'public/bootstrap-calendar/css');
    mix.copy('vendor/bootstrap-calendar/js', 'public/bootstrap-calendar/js');
});

public目录之外的文件不能通过HTTP访问(因为public是服务器的文档根目录)。这里有两个选项:

  1. bootstrap-calendar文件复制到您的public目录(不是最好的解决方案,因为在任何更新包时您都需要再次复制它们)

  2. 使用以下命令创建从供应商目录到publicbootstrap-calendar目录的符号链接(确保事先在public中创建了bootstrap-calendar目录):

ln -s /path/to/laravel/vendor/bootstrap-calendar /path/to/laravel/public/bootstrap-calendar

然后你可以把它们包含在你的视图中,像这样:

<!-- Stylesheets -->
<link rel="stylesheet" href="{{ asset('bootstrap-calendar/css/calendar.css') }}">
<!-- Scripts -->
<script src="{{ asset('bootstrap-calendar/js/calendar.js') }}">

最新更新