2015-09-19 21 views
0

我正在使用樹枝給白頁而不是致命錯誤。我知道這取決於錯誤報告設置,但我必須明確地將其設置爲E_ALL才能正常工作。樹枝白色頁面而不是致命錯誤

我有一個error_handler()函數,我已經設置它來處理致命錯誤和標準錯誤(即未定義的變量/索引)。這工作很好。

我必須將error_reporting值設置爲E_PARSE,以便錯誤處理程序不會顯示錯誤,並將其輸出到瀏覽器。但是在使用Twig時,除非我明確地將錯誤報告設置爲E_ALL,否則它只是返回一個空白頁面,這是不好的。

我希望我的錯誤處理程序能夠處理這些錯誤。例如,如果在Twig之外發生異常,那麼錯誤處理程序將起作用。否則,它不會。

我的問題是,有沒有辦法強制Twig使用我想要的錯誤處理程序(例如值傳遞),同時保持當前設置?

下面是一個例子文件:

<?php 

require PANTHER_ROOT.'include/lib/Twig/Autoloader.php'; 

// Register Twig autoloader 
Twig_Autoloader::register(); 

// Make sure PHP reports no errors apart from parse errors (this is handled by the error handler) 
error_reporting(E_PARSE); 

// Sort out error handling stuff ... 
register_shutdown_function('error_handler'); 
set_error_handler('error_handler'); 

function error_handler($errno = 0, $errstr = 'Error', $errfile = 'unknown', $errline = 0) 
{ 
    // do something 
} 

function load_template($tpl_file) 
{ 
    global $tpl_manager, $style_root; 
    if (file_exists($style_root.$tpl_file)) 
     $tpl_file = $tpl_manager->loadTemplate('@style/'.$tpl_file); 
    else 
     $tpl_file = $tpl_manager->loadTemplate('@core/'.$tpl_file); 

    return $tpl_file; 
} 

$loader = new Twig_Loader_Filesystem('include/templates'); 

$style_rot = 'something'; 
$loader->addPath('include/templates/', 'core'); 
$loader->addPath($style_root, 'style'); 

$tpl_manager = new Twig_Environment($loader); 


// then, in a seperate file: 

$tpl_file = load_template('header.tpl'); 
$tpl_file->render(
    array(
     // stuff 
    ), 
); 

回答

1

嫩枝使用的例外當事情發生故障,而不是PHP錯誤。你不會在你的代碼中發現異常。這就是爲什麼你會得到一個空白頁面:未捕獲的異常被PHP報告爲致命錯誤,並且只有在將error_reporting設置爲E_PARSE時纔會顯示它們。

解決方案也是爲了捕獲異常,使用set_exception_handlertry/catch圍繞對Twig的調用。

相關問題