重定向至PHP codeigniter中的自定义设计500错误页面 [英] Redirect to custom design 500 error page in PHP codeigniter

查看:42
本文介绍了重定向至PHP codeigniter中的自定义设计500错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在电子商务网站上工作,在PHP中使用CodeIgniter框架。我有两个面板:1)正面和2)管理员。

在页面加载或URL重定向过程中,如果发生内部服务器错误,我希望为管理员显示我的自定义设计页面500_Error_admin.php,为前端用户界面显示500_Error_Front.php。

让我清除500_error_admin.php和500_error_Front.php这两个页面具有不同的设计。

我尝试此代码,但无法成功

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond $1 !^(index.php|resources|robots.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>

<IfModule !mod_rewrite.c>
    #Directly give the path of page
    ErrorDocument 500 http://localhost/myproject/errors/500_admin.php
    #Also try with create controller and call method
    #ErrorDocument 500 http://localhost/myproject/admin/Error/show_500    
</IfModule>

控制器方法

class Error extends CI_Controller {    
    function __construct() {
        parent::__construct();
    }

    function error_500() {        
        $this->load->view('errors/500_admin');      
    }
}

这在我要找的codeigniter中可能吗?

请建议我,我怎样才能用其他方法来做这件事?

推荐答案

这是一个老问题,但这是我的解决方案:

在应用程序/核心

<?php


class MY_Exceptions extends CI_Exceptions
{
    public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {
        $message = '<p>'.(is_array($message) ? implode('</p><p>', $message) : $message).'</p>';
        $_SESSION['500_error_heading'] = $heading;
        $_SESSION['500_error_message'] = $message;

        redirect('error/server');
    }
}

在配置/routes.php中

$route['error/server'] = 'errorController/error_server';

在错误控制器中

public function error_server()
    {
        $session = new SessionHelper();

        $data['heading'] = $session->get('500_error_heading');
        $data['message'] = $session->get('500_error_message');

        $session->unset('500_error_heading');
        $session->unset('500_error_message');

        $this->CI->load->view_template('errors/html/error_general', $data);
    }

这篇关于重定向至PHP codeigniter中的自定义设计500错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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