2017-05-05 89 views
0

生产,加载我的资产我使用例如:Laravel混合产生的相对路径

<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。不知道为什么路径不满,但相对。

编辑:我也看到本地相同的行为,如果我尝试使用mix VS asset,没有https。议定书似乎并不重要。

回答

1

如果您查看source,那就是它的设计原理。你可以随时写你自己的帮手。

您需要将其添加到helpers文件中。

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> 
+0

谢谢你,这正是我最终要做的事情。似乎有点奇怪,这是功能,但任何方式,我得到它的工作。 –

+0

很高兴帮助!在与Laravel合作一段时间之后,一旦没有意义,我就会找到源头。 – whoacowboy

5

处理这种情况最好的办法就是使用资产()辅助前面加上你的APP_URL。

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