2014-07-18 108 views
1

取本(非)刀片腳本list.blade.phpLaravel刀片模板呈現2倍

<?php 
error_log(print_r("START" , true)); 

global $i; 

for ($a = 0 ; $a < 3 ; $a++) { 
    $j = @(int)$i; 

    error_log(print_r($j , true)); 
    echo $j; 

    $i = $j + 1 ; 
} 

error_log(print_r("STOP" , true)); 

預期的結果應該是012但輸出345

如果您檢查您的服務器錯誤日誌,你可以看到這一點:

START 
0 
1 
2 
STOP 
START 
3 
4 
5 
STOP 

因此模板跑了第一次沒有任何輸出,它是跑第二次,然後被髮送的輸出。

我使用更新的Laravel 4.2版本。這不是一個真正的問題,但是當每個解析的行請求沉重的計算任務時,加載時間只是X2。

您認爲這是一個錯誤還是正常行爲?

有沒有辦法在第一次啓動(空運行)時避免模板中的某些執行?

+0

你在哪裏包含這個模板?而全球的使用只是讓我感到困惑。如果您想傳遞額外數據,請使用裝飾器 – Pinoniq

+0

此模板只是一個PoC。 Global僅用於保持多個負載之間的狀態以向您顯示問題。 – Potsky

回答

0

這不是一個答案。我剛剛測試了您的代碼並得到了結果012。我使用4.2.6版測試了它。我不知道你是如何得到它的。

這是我使用的代碼。

應用程序/ route.php

Route::get("test",function(){ 
    return View::make("aaa"); 
}); 

應用程序/視圖/ aaa.blade.php

<?php 
error_log(print_r("START" , true)); 

global $i; 

for ($a = 0 ; $a < 3 ; $a++) { 
    $j = @(int)$i; 

    error_log(print_r($j , true)); 
    echo $j; 

    $i = $j + 1 ; 
} 

error_log(print_r("STOP" , true)); 
+0

你是對的!我用我的Laravel樣板,但它是罪魁禍首。我使用後過濾器來動態縮減HTML代碼。我會回答我的問題。 – Potsky

1

發現問題,而不是解決方案!我用我的Laravel樣板,但它是罪魁禍首。我使用全局過濾器後,在飛行中(對於這個問題,添加錯誤日誌)壓縮HTML代碼:

App::after(function ($request, $response) { 

    // Minify only texts 
    if (strpos($response->headers->get('content-type') , 'text/') !== false) { 
     if ($response instanceof Illuminate\Http\Response) { 

error_log(print_r('coucou1' , true)); 
      $output = $response->getOriginalContent(); 
error_log(print_r('coucou2' , true)); 

      $re = '%# Collapse whitespace everywhere but in blacklisted elements. 
      (?>    # Match all whitespans other than single space. 
      [^\S ]\s*  # Either one [\t\r\n\f\v] and zero or more ws, 
      | \s{2,}  # or two or more consecutive-any-whitespace. 
      ) # Note: The remaining regex consumes no text at all... 
      (?=    # Ensure we are not in a blacklist tag. 
      [^<]*+  # Either zero or more non-"<" {normal*} 
      (?:   # Begin {(special normal*)*} construct 
      <   # or a < starting a non-blacklist tag. 
      (?!/?(?:textarea|pre|script)\b) 
      [^<]*+  # more non-"<" {normal*} 
      )*+   # Finish "unrolling-the-loop" 
      (?:   # Begin alternation group. 
      <   # Either a blacklist start tag. 
      (?>textarea|pre|script)\b 
      | \z   # or end of file. 
      )    # End alternation group. 
      ) # If we made it here, we are not in a blacklist tag. 
      %Six'; 

error_log(print_r('coucou3' , true)); 

      $output = preg_replace($re , " " , $output); 

error_log(print_r('coucou4' , true)); 
      if ($output !== null) { 
       $response->setContent($output); 
      } 
error_log(print_r('coucou5' , true)); 
     } 
    } 

}); 

和錯誤日誌結果真的令人吃驚:

START 
0 
1 
2 
STOP 
coucou1 
coucou2 
coucou3 
START 
3 
4 
5 
STOP 
coucou4 
coucou5 

刀片模板進行評估以下行第二次:

$output = preg_replace($re , " " , $output); 

這是不是在PHP可能......我想我錯過Laravel架構設計......如果有人不懂...