2014-11-25 40 views
0

當我嘗試在配置文件中調用對象方法(或靜態方法)時,遇到了問題(laravel崩潰)。我用它在一個地方緩存所有格式化的產品價格。請建議我如何解決這個問題?請看以下代碼:Laravel在配置文件中調用對象方法時崩潰

應用程序\ routes.php文件:

Route::get('test', function() { 
    dd(Config::get('settings.productPrices')); 
}); 

應用程序\ CONFIG \ settings.php中:

$setting_data['productPrices'] = Cache::remember('settings.productPrices', 60, function() 
{ 
    $products = Product::all(); 
    $prices = []; 
    foreach ($products as $product) { 
     $prices [$product->id] = $product->getPriceFormatted(); 
    } 
    return $prices; 
}); 
return $setting_data; 

getPriceFormatted法正常工作。如果我使用$ product-> price而不是$ product-> getPriceFormatted(),則一切正常。

應用程序\型號\ Product.php:

public function getPriceFormatted() { 

    if (Config::has('settings.exchangeRateUsdToUah')) { 
     return Config::get('settings.exchangeRateUsdToUah') * $this->price. ' UAH'; 
    } 
    return $this->price . ' USD'; 
} 

附: Config :: get('settings.exchangeRateUsdToUah')工作正常。

+0

您將需要告訴我們它是如何「崩潰」,比如,你看到了什麼錯誤/異常?沒有這些,沒有人能夠確定發生了什麼。 – ollieread 2014-11-25 11:33:37

+0

頁面無限加載。和其他網站頁面也不反應。即使pc重新啓動任何頁面不起作用。 – 2014-11-25 12:58:38

+0

「不起作用」是描述性最差的事情 – lagbox 2014-11-25 14:54:36

回答

0

配置在Laravel完全安裝之前加載,因此此時我無法使用其大部分部件。

相關問題