使用 CodeIgniter 中的模板自定义错误页面

发布于 2024-12-07 15:38:46 字数 633 浏览 6 评论 0原文

我正在使用 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 功能的了解:-)

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.

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 :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

霓裳挽歌倾城醉 2024-12-14 15:38:46

我已经为 Exceptions 类创建了一个扩展。
在此扩展中,我替换了 $this->Exceptions->show_error(); 方法,该方法由 CI 的 show_error() 函数使用。

当我调用 show_error('User is notlogin', 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;
	}
	
}

?>

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.

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.

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');
}

?>

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:

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

?>

微凉徒眸意 2024-12-14 15:38:46

我这样做:
我创建了自己的错误页面,每当我应该抛出 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 网站上的精彩教程。

I do it like this:
I create my own error page, and whenever I should throw a 404 error, I actually load my 404 page.

So say my default controller is site.php, my site.php looks like this:

<?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 */

So I serve the home/404.php whenever an error occurs. i.e., I don't allow CodeIgniter to call show_404(); therefore the 404 page looks like any other page.

p.s. I assume that you followed the nice tutorial in CodeIgniter's website.

同尘 2024-12-14 15:38:46

创建自定义错误页面的最简单方法是编辑 /application/views/errors/html/error_*.php 中的文件,例如 error_404.php(针对 404) 、 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 & 的视图文件页脚

另一件需要注意的事情是:

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

The simplest way to create custom error pages is to edit the files at /application/views/errors/html/error_*.php such as error_404.php (for 404s), error_db.php (for database errors) and error_general.php (for most other errors).

As these pages are within your application directory, you are free to customise them to your needs.

If your normal view template looks something like this:

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

You can adapt this in your /application/views/errors/html/error_*.php files like so:

<?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'; ?>

Notice that we're no longer using views, but instead including the view files for the header & footer.

Another thing to note:

In the header view, I'm passing a $data object which includes $data['page_title']. As the error pages don't use views, you have to add any variables that you'd normally pass into the view, hence the presence of $page_title.

尤怨 2024-12-14 15:38:46
config/routes.php 

在此处编辑

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

类型您的控制器,例如错误

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

config/routes.php 

edit

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

type here your controller for example Error

create a function index and load your view

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文