Laravel mix产生相对路径

Jar*_*ier 2 laravel laravel-mix

在生产时,我加载我的资产,例如:

<link href="{{ mix('/css/app.css') }}" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

并希望看到编译时:

<link href="https://example.com/css/app.083fd04ba374238b03b23e742c997718.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

但是我只是看到相对路径:

<link href="/css/app.083fd04ba374238b03b23e742c997718.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

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 {
    //
}
Run Code Online (Sandbox Code Playgroud)

最新版本的Laravel(5.4.21).使用nginx,在Ubuntu 16.04上强制使用https.不知道为什么路径不满,而是相对的.

编辑:如果我尝试使用mixvs asset,我也在本地看到相同的行为https.协议在这里似乎并不重要.

Dev*_*von 15

处理此问题的最佳方法是使用asset()帮助程序来添加APP_URL.

<script src="{{ asset(mix('css/app.css')) }}"></script>
Run Code Online (Sandbox Code Playgroud)

  • 完美运行,无需自定义助手。这应该是公认的答案。 (2认同)

who*_*boy 3

如果你查看源代码,就会发现它就是这样设计的。您始终可以编写自己的助手。

您需要将其添加到帮助程序文件中。

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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

从刀片中调用像

<script src="{{ mixUrl('/css/app.css', '', 'http://example.com') }}"></script>
Run Code Online (Sandbox Code Playgroud)