6

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

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

4

4 回答 4

2

我为 Exceptions 类创建了一个扩展。在这个扩展中,我替换了$this->Exceptions->show_error();方法,show_error()CI 的功能使用了女巫。

当我调用show_error('User is not logged in', 401);此自定义方法时,首先要查找 error_$status_code文件。在上面的示例中,它将查找 error_401.php 文件。

当这个文件不存在时,它只会加载 error_general.php 文件,就像默认的$this->Exceptions->show_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 文件应如下所示:

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

/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;
	}
	
}

?>

于 2015-05-06T17:10:51.540 回答
1

我这样做:我创建自己的错误页面,并且每当我应该抛出 404 错误时,我实际上加载了我的 404 页面。

所以说我的默认控制器是 site.php,我的 site.php 看起来像这样:

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

class Site extends CI_Controller {

    public function index()
    {
        $this->load->view('welcome_message');
    }

    public function view($page = "home" , $function = "index")
    {
        do_something();
        if($status == "404")
        {
            $function = "404";
        }
        $this->load->view('templates/header', $data);
        $this->load->view($page.'/'.$function, $data);      
        $this->load->view('templates/footer', $data);
    }




}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

因此,每当发生错误时,我都会为 home/404.php 提供服务。即,我不允许 CodeIgniter 调用,show_404();因此 404 页面看起来像任何其他页面。

ps我假设您遵循了 CodeIgniter 网站上的精彩教程。

于 2013-09-23T18:22:17.263 回答
1

创建自定义错误页面的最简单方法是编辑文件,/application/views/errors/html/error_*.php例如error_404.php(对于 404s)、error_db.php(对于数据库错误)和error_general.php(对于大多数其他错误)。

由于这些页面位于您的application目录中,您可以根据自己的需要自由定制它们。

如果您的普通视图模板如下所示:

<?php $this->load->view('includes/header'); ?>
...
...
<?php $this->load->view('includes/footer'); ?>

您可以在您的/application/views/errors/html/error_*.php文件中调整它,如下所示:

<?php
  $page_title = $heading;
  include VIEWPATH.'includes'.DIRECTORY_SEPARATOR.'header.php';
?>
<div class="well">
  <h1><?php echo $heading; ?></h1>
  <?php echo $message; ?>
</div>
<?php include VIEWPATH.'includes'.DIRECTORY_SEPARATOR.'footer.php'; ?>

请注意,我们不再使用视图,而是包含header&的视图文件footer

还有一点需要注意:

header视图中,我传递了一个$data包含$data['page_title']. 由于错误页面不使用视图,因此您必须添加通常会传递到视图中的任何变量,因此存在$page_title.

于 2016-08-24T13:21:23.663 回答
0
config/routes.php 

编辑

$route['404_override'] = '';

在此处输入您的控制器,例如错误

创建一个函数 索引加载您的视图

于 2017-09-19T19:20:17.427 回答