自定义错误页面与CodeIgniter中的模板 [英] Custom error pages with templates in CodeIgniter

查看:287
本文介绍了自定义错误页面与CodeIgniter中的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeIgniter的模板库, http:// williamsconcepts。 com / ci / codeigniter / libraries / template / reference.html ,现在我也要实现自定义错误页面。我发现一个方法涉及一个扩展默认路由器的MY_Router: http://maestric.com/doc/php/codeigniter_404 ,但只处理404错误。我想让所有错误显示一个简单的用户友好的页面,包括数据库错误等,我想要通过一个控制器,部分是我可以使用模板库,部分地,我也可以实现一个电子邮件功能发送自己有关发生错误的信息。

I'm using the template library for CodeIgniter, http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html, and now I want to implement custom error pages too. I found one method involving a MY_Router extending the default router: http://maestric.com/doc/php/codeigniter_404 but that only treats 404 errors. I want all errors to show a simple user-friendly page, including database errors etc, and I want it to go through a controller, partly so I can use the template library, and partly so I can also implement an email function to send myself information about the error that occurred.

有人询问是否扩展了上述MY_Router方法的其他错误功能,如error_db,但没有作者的回答,所以我在这里转过来,看看有没有人知道如何做到这一点,按照上述方法或任何其他简单的方式实现。请注意,我是一个新手,所以不要太多关于我对基本CodeIgniter功能的了解: - )

Someone asked about extending the functionality of the above MY_Router method for other errors, like error_db, but got no answer from the author, so I'm turning here to see if anyone knows how to do this, along the lines of the above method or any other simple way of achieving it. Please note that I'm a newbie, so do not assume too much about my knowledge of basic CodeIgniter functionality :-)

推荐答案

我为异常类创建了一个扩展名。
在这个扩展中,我已经替换了 $ this-> Exceptions-> show_error(); 方法,当我打电话给 show_error('User is not logged in',401,...)时,显示CI的功能

I've created an extension for the Exceptions class. In this extension I've replaced the $this->Exceptions->show_error(); method, witch is used by the show_error() function of CI.

show_error() ); 此自定义方法首先查找错误_ $ status_code 文件。在上面的例子中,它将寻找一个error_401.php文件。

when I call show_error('User is not logged in', 401); this custom method is looking for an error_$status_code file first. In the case of the example above, it will look for an error_401.php file.

当这个文件不存在时,它将加载error_general.php文件,像默认的 $ this-> Exceptions-> show_error();

When this file does not exists, it wil just load the error_general.php file, like the default $this->Exceptions->show_error(); does.

在你的情况下,您可以使用以下代码在库,控制器或任何应用程序中使用错误。

In your case, you can use the following code to use in your library, controller or whatever should throw an error.

<?php

if(!(isset($UserIsLoggedin))){
  $this->load->view('template/header');
  show_error('User is not logged in', 401);
  $this->load->view('template/footer');
}

?>

你的error_401.php文件应该不像这样:

Your error_401.php file should than look like this:

<div id="container">
	<h1><?php echo 'This is an 401 error'; ?></h1>
	<?php echo $message; ?>
</div>

/ application / core / MY_Exceptions.php:

/application/core/MY_Exceptions.php:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Exceptions extends CI_Exceptions
{
	
	function show_error($heading, $message, $template = 'error_general', $status_code = 500)
	{		
		if((!isset($template)) || ($template == 'error_general')){
			if(file_exists(APPPATH.'errors/error_'.$status_code.'.php')) {
				$template = 'error_'.$status_code;
			}
		}    	
		
		if (!isset($status_code)) $status_code = 500;
		
		set_status_header($status_code);

		$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';

		if (ob_get_level() > $this->ob_level + 1)
		{
			ob_end_flush();
		}
		ob_start();		
				
		include(APPPATH.'errors/'.$template.'.php');
		$buffer = ob_get_contents();
		ob_end_clean();
		return $buffer;
	}
	
}

?>

这篇关于自定义错误页面与CodeIgniter中的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆