7

在生产中,加载我使用的资产,例如:

<link href="{{ mix('/css/app.css') }}" rel="stylesheet">

并期望在编译时看到:

<link href="https://example.com/css/app.083fd04ba374238b03b23e742c997718.css" rel="stylesheet">

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

<link href="/css/app.083fd04ba374238b03b23e742c997718.css" rel="stylesheet">

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 {
    //
}

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

编辑:如果我尝试使用mixvs asset,我也会在本地看到相同的行为,而没有https. 协议实际上在这里似乎无关紧要。

4

4 回答 4

33

处理这个问题的最好方法是使用asset()助手来添加你的APP_URL。

<script src="{{ asset(mix('css/app.css')) }}"></script>
于 2017-05-19T20:29:00.303 回答
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]);
    }
}

从刀片调用

<script src="{{ mixUrl('/css/app.css', '', 'http://example.com') }}"></script>
于 2017-05-06T10:23:04.560 回答
1

最好的方法是使用asset()助手。但是还有另一种方法可以通过使用url()or secure_url()helper

<script src="{{ url(mix('css/app.css')) }}"></script>

或者

<script src="{{ secure_url(mix('css/app.css')) }}"></script>
于 2018-11-20T09:57:01.473 回答
-1

这是我从 TwigExtension 调用的函数,在 laravel 5.* 你可以做一个辅助函数。

// use Illuminate\Foundation\Mix;

function mix_url($path, $manifestDirectory = ''){

    return asset(app(Mix::class)(...func_get_args()));
}

您还需要在 .env 上定义 ASSET_URL

于 2020-05-20T09:50:59.190 回答