2017-05-05 90 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>