如何使用Codeigniter在自定义404页面中重定向404错误?

时间:2022-05-04 07:41:07

Kind sirs, I'm using Codeigniter to build a blog. I might need a way to redirect a 404 error into a custom 404 page. Just like what Abduzeedo.com's 404 page. Is it possible to control this by using routes? Or should i use controllers to direct it to another view? Thanks very much!

亲切的先生,我正在使用Codeigniter建立一个博客。我可能需要一种方法将404错误重定向到自定义404页面。就像Abduzeedo.com的404页面一样。是否可以通过使用路线来控制这个?或者我应该使用控制器将其指向另一个视图?非常感谢!

9 个解决方案

#1


17  

there is another way which I use: by overriding Exception core class of Codeigniter. Firstly make sure your config file(system/application/config/config.php) subclass prefix is as following $config['subclass_prefix'] = 'MY_';

我还有另一种方法:通过覆盖Codeigniter的Exception核心类。首先确保您的配置文件(system / application / config / config.php)子类前缀如下所示$ config ['subclass_prefix'] ='MY_';

Then make a file named MY_Exceptions.php in system/application/libraries. Then override the function show_404() function here as follows.

然后在system / application / libraries中创建一个名为MY_Exceptions.php的文件。然后在此处覆盖函数show_404()函数,如下所示。

class MY_Exceptions extends CI_Exceptions{
    function MY_Exceptions(){
        parent::CI_Exceptions();
    }

    function show_404($page=''){    

        $this->config =& get_config();
        $base_url = $this->config['base_url'];

        $_SESSION['error_message'] = 'Error message';
        header("location: ".$base_url.'error.html');
        exit;
    }
}

Now Error controller will be your error page, where the page will be redirected for 404 error.

现在,错误控制器将成为您的错误页面,其中页面将被重定向为404错误。

#2


7  

Custom 404 page is create by using the following 3 steps,

使用以下3个步骤创建自定义404页面,

Step 1: First open routes.php in application/config and set a custom controller name,

步骤1:首先在application / config中打开routes.php并设置自定义控制器名称,

$route['404_override'] = 'my404'; //my404 is class name. 

Step 2: Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here,

步骤2:创建一个新控制器并在其中编写以下代码。代码很容易理解。所以我不打算在这里解释一下,

    <?php 
class my404 extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct(); 
    } 

    public function index() 
    { 
        $this->output->set_status_header('404'); 
        $data['content'] = 'error_404'; // View name 
        $this->load->view('index',$data);//loading in my template 
    } 
    } 
    ?>

Step 3: Create a view file with name 'error_404.php' with your custom message.

第3步:使用您的自定义消息创建名为“error_404.php”的视图文件。

#3


2  

Simple Solution.

简单解决方案

Create your custom error or Page not found page within view.

在视图中创建自定义错误或“未找到页面”页面。

Create a controller for error page. Within index function load the view of error page that you already created in previous step.

为错误页面创建控制器。在索引函数内加载您在上一步中已创建的错误页面的视图。

Now, Go to your application/config/routes.php

现在,转到您的application / config / routes.php

you should find $route['404_override'] = '';

你应该找到$ route ['404_override'] ='';

Its the '' mean its calling the default error page.

它的意思是它调用默认的错误页面。

Now place your controller name for error page .

现在将控制器名称放在错误页面上。

It will work.

它会工作。

Suppose my Controller for error page is 'error'. and view page is 'notfound' .

假设我的Controller错误页面是'错误'。并且查看页面是'未发现'。

So, code for Error.php controller will be:

因此,Error.php控制器的代码将是:

<?php
class Error extends CI_Controller
{
    function index()
        {
           $this->load->view('notfound'); 
        }

}

Now, I replaced $route['404_override'] = ''; to $route['404_override'] = 'error'; in application/config/routes.php.

现在,我更换了$ route ['404_override'] =''; to $ route ['404_override'] ='错误';在application / config / routes.php中。

#4


2  

You don't need to create any controller or modify the code, rather than modify one view file. Just placed your 404 page HTML into the application/views/errors/html/error_404.php file.

您不需要创建任何控制器或修改代码,而不是修改一个视图文件。只需将404页面HTML放入application / views / errors / html / error_404.php文件即可。

If you want to use base_url() or any other CodeIgniter functions, initialize CI_Controller class like the below code.

如果要使用base_url()或任何其他CodeIgniter函数,请像下面的代码一样初始化CI_Controller类。

<?php
  $ci = new CI_Controller();
  $ci =& get_instance();
  $ci->load->helper('url');
?>

#5


1  

Well you want to show your custom 404page when page not found in your website you just replace your custom file on core file which is located in application/error/error_404.php just replace your file here and you are good to go else where you can follow other method by using routes and custom controller for you custom 404 page. But the shortest and easiest way is replacing the core file with custom 404error page file

那么你想在你的网站上找不到页面时显示你的自定义404页面你只需要在application / error / error_404.php中的核心文件上替换你的自定义文件,只需在这里替换你的文件,你就可以去其他地方了。通过使用路由和自定义控制器为您自定义404页面按照其他方法。但最简单和最简单的方法是使用自定义404error页面文件替换核心文件

#6


1  

Very very very very more simple solution...
Just edit /application/views/error_404.php with your custom 404 page :)

非常非常非常简单的解决方案...只需使用您的自定义404页面编辑/application/views/error_404.php :)

#7


0  

For a simple solution, go to your "error_404.php" file in "application/errors/" directory and put this code at the beginning of the file:

要获得一个简单的解决方案,请转到“application / errors /”目录中的“error_404.php”文件,并将此代码放在文件的开头:

header("Location:".base_url());

It'll redirect your 404 page to home page.

它会将您的404页面重定向到主页。

#8


0  

First open routes.php in the application/config folder and set a custom controller name.

首先在application / config文件夹中打开routes.php并设置自定义控制器名称。

 $route['404_override'] = 'custom404'; //custom404 is class name. 

Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here.

创建一个新控制器并在其中编写以下代码。代码很容易理解。所以我不打算在这里解释一下。

<?php 
class custom404 extends CI_Controller 
{
public function __construct() 
{
    parent::__construct(); 
} 

public function index() 
{ 
    $this->output->set_status_header('404'); 
    $data['content'] = 'error_404'; // View name 
    $this->load->view('index',$data);//loading in my template 
} 
 } 
 ?> 

Create a view file with name 'error_404.php' with your custom message. That is all you need to do and will be having your custom 404 page now.

使用您的自定义消息创建名为“error_404.php”的视图文件。这就是您需要做的所有事情,现在将拥有您的自定义404页面。

#9


0  

No need to define any complexity. The process is very simple. First, go to your application->config->routes.php file and change this with your controller's function, where you simply load the header, footer, and 404.php page.

无需定义任何复杂性。这个过程非常简单。首先,转到您的application-> config-> routes.php文件,并使用您的控制器功能进行更改,您只需加载页眉,页脚和404.php页面。

$route['404_override'] = 'welcome/_404';

Then come to your controller's page

然后进入控制器页面

function _404(){
$this->load->view("header");
$this->load->view("404");
$this->load->view("footer");

}

}

The same question is also available here Display a view for 404 error in CodeIgniter

此处也可以使用相同的问题在CodeIgniter中显示404错误的视图

#1


17  

there is another way which I use: by overriding Exception core class of Codeigniter. Firstly make sure your config file(system/application/config/config.php) subclass prefix is as following $config['subclass_prefix'] = 'MY_';

我还有另一种方法:通过覆盖Codeigniter的Exception核心类。首先确保您的配置文件(system / application / config / config.php)子类前缀如下所示$ config ['subclass_prefix'] ='MY_';

Then make a file named MY_Exceptions.php in system/application/libraries. Then override the function show_404() function here as follows.

然后在system / application / libraries中创建一个名为MY_Exceptions.php的文件。然后在此处覆盖函数show_404()函数,如下所示。

class MY_Exceptions extends CI_Exceptions{
    function MY_Exceptions(){
        parent::CI_Exceptions();
    }

    function show_404($page=''){    

        $this->config =& get_config();
        $base_url = $this->config['base_url'];

        $_SESSION['error_message'] = 'Error message';
        header("location: ".$base_url.'error.html');
        exit;
    }
}

Now Error controller will be your error page, where the page will be redirected for 404 error.

现在,错误控制器将成为您的错误页面,其中页面将被重定向为404错误。

#2


7  

Custom 404 page is create by using the following 3 steps,

使用以下3个步骤创建自定义404页面,

Step 1: First open routes.php in application/config and set a custom controller name,

步骤1:首先在application / config中打开routes.php并设置自定义控制器名称,

$route['404_override'] = 'my404'; //my404 is class name. 

Step 2: Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here,

步骤2:创建一个新控制器并在其中编写以下代码。代码很容易理解。所以我不打算在这里解释一下,

    <?php 
class my404 extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct(); 
    } 

    public function index() 
    { 
        $this->output->set_status_header('404'); 
        $data['content'] = 'error_404'; // View name 
        $this->load->view('index',$data);//loading in my template 
    } 
    } 
    ?>

Step 3: Create a view file with name 'error_404.php' with your custom message.

第3步:使用您的自定义消息创建名为“error_404.php”的视图文件。

#3


2  

Simple Solution.

简单解决方案

Create your custom error or Page not found page within view.

在视图中创建自定义错误或“未找到页面”页面。

Create a controller for error page. Within index function load the view of error page that you already created in previous step.

为错误页面创建控制器。在索引函数内加载您在上一步中已创建的错误页面的视图。

Now, Go to your application/config/routes.php

现在,转到您的application / config / routes.php

you should find $route['404_override'] = '';

你应该找到$ route ['404_override'] ='';

Its the '' mean its calling the default error page.

它的意思是它调用默认的错误页面。

Now place your controller name for error page .

现在将控制器名称放在错误页面上。

It will work.

它会工作。

Suppose my Controller for error page is 'error'. and view page is 'notfound' .

假设我的Controller错误页面是'错误'。并且查看页面是'未发现'。

So, code for Error.php controller will be:

因此,Error.php控制器的代码将是:

<?php
class Error extends CI_Controller
{
    function index()
        {
           $this->load->view('notfound'); 
        }

}

Now, I replaced $route['404_override'] = ''; to $route['404_override'] = 'error'; in application/config/routes.php.

现在,我更换了$ route ['404_override'] =''; to $ route ['404_override'] ='错误';在application / config / routes.php中。

#4


2  

You don't need to create any controller or modify the code, rather than modify one view file. Just placed your 404 page HTML into the application/views/errors/html/error_404.php file.

您不需要创建任何控制器或修改代码,而不是修改一个视图文件。只需将404页面HTML放入application / views / errors / html / error_404.php文件即可。

If you want to use base_url() or any other CodeIgniter functions, initialize CI_Controller class like the below code.

如果要使用base_url()或任何其他CodeIgniter函数,请像下面的代码一样初始化CI_Controller类。

<?php
  $ci = new CI_Controller();
  $ci =& get_instance();
  $ci->load->helper('url');
?>

#5


1  

Well you want to show your custom 404page when page not found in your website you just replace your custom file on core file which is located in application/error/error_404.php just replace your file here and you are good to go else where you can follow other method by using routes and custom controller for you custom 404 page. But the shortest and easiest way is replacing the core file with custom 404error page file

那么你想在你的网站上找不到页面时显示你的自定义404页面你只需要在application / error / error_404.php中的核心文件上替换你的自定义文件,只需在这里替换你的文件,你就可以去其他地方了。通过使用路由和自定义控制器为您自定义404页面按照其他方法。但最简单和最简单的方法是使用自定义404error页面文件替换核心文件

#6


1  

Very very very very more simple solution...
Just edit /application/views/error_404.php with your custom 404 page :)

非常非常非常简单的解决方案...只需使用您的自定义404页面编辑/application/views/error_404.php :)

#7


0  

For a simple solution, go to your "error_404.php" file in "application/errors/" directory and put this code at the beginning of the file:

要获得一个简单的解决方案,请转到“application / errors /”目录中的“error_404.php”文件,并将此代码放在文件的开头:

header("Location:".base_url());

It'll redirect your 404 page to home page.

它会将您的404页面重定向到主页。

#8


0  

First open routes.php in the application/config folder and set a custom controller name.

首先在application / config文件夹中打开routes.php并设置自定义控制器名称。

 $route['404_override'] = 'custom404'; //custom404 is class name. 

Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here.

创建一个新控制器并在其中编写以下代码。代码很容易理解。所以我不打算在这里解释一下。

<?php 
class custom404 extends CI_Controller 
{
public function __construct() 
{
    parent::__construct(); 
} 

public function index() 
{ 
    $this->output->set_status_header('404'); 
    $data['content'] = 'error_404'; // View name 
    $this->load->view('index',$data);//loading in my template 
} 
 } 
 ?> 

Create a view file with name 'error_404.php' with your custom message. That is all you need to do and will be having your custom 404 page now.

使用您的自定义消息创建名为“error_404.php”的视图文件。这就是您需要做的所有事情,现在将拥有您的自定义404页面。

#9


0  

No need to define any complexity. The process is very simple. First, go to your application->config->routes.php file and change this with your controller's function, where you simply load the header, footer, and 404.php page.

无需定义任何复杂性。这个过程非常简单。首先,转到您的application-> config-> routes.php文件,并使用您的控制器功能进行更改,您只需加载页眉,页脚和404.php页面。

$route['404_override'] = 'welcome/_404';

Then come to your controller's page

然后进入控制器页面

function _404(){
$this->load->view("header");
$this->load->view("404");
$this->load->view("footer");

}

}

The same question is also available here Display a view for 404 error in CodeIgniter

此处也可以使用相同的问题在CodeIgniter中显示404错误的视图