Chester Sabado
Chester Sabado

Reputation: 243

How can I redirect a 404 Error in a custom 404 page using Codeigniter?

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!

Upvotes: 24

Views: 49199

Answers (10)

Joe Edman
Joe Edman

Reputation: 11

The $routes->set404Override() function in CodeIgniter allows you to customize the behavior when a requested route is not found. This is useful for creating a custom 404 page by defining a method to handle such cases.

In the example provided:

$routes->set404Override('App\Controllers\YourController::your_method');
  1. App\Controllers\YourController: Replace this with the name of your custom controller.
  2. your_method: This is the method inside the controller that will handle 404 errors. You need to define this method.

How it works

When a route does not match any of the defined ones, CodeIgniter will call the specified method. For instance:

namespace App\Controllers;

use CodeIgniter\Controller;

class YourController extends Controller
{
    public function your_method()
    {
        // Customize your 404 page here
        return view('errors/404');
    }
}

It replaces the default CodeIgniter 404 page and allows you to customize the error page's content and style.

Upvotes: 0

Reza Fahmi
Reza Fahmi

Reputation: 262

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

Upvotes: 1

heySushil
heySushil

Reputation: 499

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.

$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

Upvotes: 1

JoyGuru
JoyGuru

Reputation: 1833

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.

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

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

Upvotes: 4

Arup Adhikari
Arup Adhikari

Reputation: 21

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

you should find $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' .

So, code for Error.php controller will be:

<?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.

Upvotes: 2

Vinod VT
Vinod VT

Reputation: 7149

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

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

$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,

    <?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.

Upvotes: 10

Minu Alex
Minu Alex

Reputation: 117

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

 $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.

Upvotes: 1

Aitazaz Khan
Aitazaz Khan

Reputation: 1605

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

Upvotes: 1

Sardar Younus
Sardar Younus

Reputation: 75

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:

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

It'll redirect your 404 page to home page.

Upvotes: 3

sumanchalki
sumanchalki

Reputation: 1467

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

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

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.

Upvotes: 20

Related Questions