Twig 白页而不是致命错误
Twig White page instead of fatal error
我正在使用 Twig,它给出的是白页而不是致命错误。我知道这取决于错误报告设置,但我必须明确地将其设置为 E_ALL 才能正常工作。
我有一个 error_handler() 函数,我已将其设置为处理致命错误和标准错误(即未定义的 variable/index)错误。这很好用。
我必须将 error_reporting 值设置为 E_PARSE,这样错误处理程序就不会显示错误,并将错误输出到浏览器。但是在使用 Twig 时,除非我明确设置错误报告为 E_ALL,否则它只是 returns 一个空白页,这是不好的。
我希望我的错误处理程序能够处理这些错误。例如,如果异常发生在 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
),
);
Twig 在出现故障时使用异常,而不是 PHP 错误。而且您不会在代码中捕获异常。这就是您得到空白页的原因:PHP 将未捕获的异常报告为致命错误,并且仅在将 error_reporting 设置为 E_PARSE 时不会显示它们。
解决方案也是捕获异常,在对 Twig 的调用周围使用 set_exception_handler
或 try/catch
。
我正在使用 Twig,它给出的是白页而不是致命错误。我知道这取决于错误报告设置,但我必须明确地将其设置为 E_ALL 才能正常工作。
我有一个 error_handler() 函数,我已将其设置为处理致命错误和标准错误(即未定义的 variable/index)错误。这很好用。
我必须将 error_reporting 值设置为 E_PARSE,这样错误处理程序就不会显示错误,并将错误输出到浏览器。但是在使用 Twig 时,除非我明确设置错误报告为 E_ALL,否则它只是 returns 一个空白页,这是不好的。
我希望我的错误处理程序能够处理这些错误。例如,如果异常发生在 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
),
);
Twig 在出现故障时使用异常,而不是 PHP 错误。而且您不会在代码中捕获异常。这就是您得到空白页的原因:PHP 将未捕获的异常报告为致命错误,并且仅在将 error_reporting 设置为 E_PARSE 时不会显示它们。
解决方案也是捕获异常,在对 Twig 的调用周围使用 set_exception_handler
或 try/catch
。