Laravel mix 生成相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43815086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel mix generates relative paths
提问by Jared Eitnier
On production, to load my assets I use for example:
在生产中,加载我使用的资产,例如:
<link href="{{ mix('/css/app.css') }}" rel="stylesheet">
and expect to see when compiled:
并期望在编译时看到:
<link href="https://example.com/css/app.083fd04ba374238b03b23e742c997718.css" rel="stylesheet">
However I am just seeing the relative path:
但是我只是看到了相对路径:
<link href="/css/app.083fd04ba374238b03b23e742c997718.css" rel="stylesheet">
webpack.mix.js:
webpack.mix.js:
mix
.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css')
.sass('resources/assets/sass/print.scss', 'public/css')
.copy([
'node_modules/bootstrap-sass/assets/fonts/bootstrap',
'node_modules/font-awesome/fonts',
], 'public/fonts')
.sourceMaps();
if (mix.config.inProduction) {
mix
.version()
.disableNotifications();
} else {
//
}
On latest version of Laravel (5.4.21). Using nginx, forcing https on Ubuntu 16.04. No idea why the paths are not full, but relative.
在最新版本的 Laravel (5.4.21) 上。使用 nginx,在 Ubuntu 16.04 上强制使用 https。不知道为什么路径不完整,而是相对的。
EDIT:I am also seeing the same behavior locally if I try to use mix
vs asset
, without https
. Protocol seems not matter here actually.
编辑:如果我尝试使用mix
vs asset
,而没有https
. 协议在这里似乎并不重要。
采纳答案by whoacowboy
If you check out the source, that is how it is designed to work. You could always write your own helper.
如果您查看源代码,这就是它的设计方式。你总是可以编写自己的助手。
You need to add this to a helpersfile.
您需要将其添加到帮助文件中。
use Illuminate\Support\Str;
use Illuminate\Support\HtmlString;
if (! function_exists('mixUrl')) {
/**
* Get the path to a versioned Mix file.
*
* @param string $path
* @param string $manifestDirectory
* @param string $baseUrl
* @return \Illuminate\Support\HtmlString
*
* @throws \Exception
*/
function mixUrl($path, $manifestDirectory = '', $baseUrl = null)
{
static $manifest;
if (! starts_with($path, '/')) {
$path = "/{$path}";
}
if ($manifestDirectory && ! starts_with($manifestDirectory, '/')) {
$manifestDirectory = "/{$manifestDirectory}";
}
if (file_exists(public_path($manifestDirectory.'/hot'))) {
return new HtmlString("//localhost:8080{$path}");
}
if (! $manifest) {
if (! file_exists($manifestPath = public_path($manifestDirectory.'/mix-manifest.json'))) {
throw new Exception('The Mix manifest does not exist.');
}
$manifest = json_decode(file_get_contents($manifestPath), true);
}
if (!is_null($baseUrl)){
if (strlen($baseUrl) > 1 && Str::endsWith($baseUrl, '/')) {
$baseUrl = substr($baseUrl, 0, -1);
}
return new HtmlString($baseUrl . $manifestDirectory . $manifest[$path]);
}
if (! array_key_exists($path, $manifest)) {
throw new Exception(
"Unable to locate Mix file: {$path}. Please check your ".
'webpack.mix.js output paths and try again.'
);
}
return new HtmlString($manifestDirectory.$manifest[$path]);
}
}
called from blade like
从刀片中调用
<script src="{{ mixUrl('/css/app.css', '', 'http://example.com') }}"></script>
回答by Devon
The best way to handle this is to use the asset() helper to prepend your APP_URL.
处理此问题的最佳方法是使用 asset() 助手来添加您的 APP_URL。
<script src="{{ asset(mix('css/app.css')) }}"></script>
回答by shahabphp
The best way is to use asset()
helper. But there is another way to handle this by using url()
or secure_url()
helper
最好的方法是使用asset()
助手。但是还有另一种方法可以通过使用url()
或secure_url()
助手来处理这个问题
<script src="{{ url(mix('css/app.css')) }}"></script>
or
或者
<script src="{{ secure_url(mix('css/app.css')) }}"></script>
回答by backtheweb
This is my function called from a TwigExtension, on laravel 5.* you can do a helper function.
这是我从 TwigExtension 调用的函数,在 laravel 5.* 上你可以做一个辅助函数。
// use Illuminate\Foundation\Mix;
function mix_url($path, $manifestDirectory = ''){
return asset(app(Mix::class)(...func_get_args()));
}
Also you need to define the ASSET_URL on your .env
您还需要在 .env 上定义 ASSET_URL