Welcome Guest, Not a member yet? Register   Sign In
Difficulties in setting up a custom 404 page.
#1

[eluser]duartix[/eluser]
I’m using CodeIgniter 2.1.4
I’m trying to show a custom error 404 page, but it’s not loading when I type a non existent URI.

my routes.php is as follows:

Code:
| There area two reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router what URI segments to use if those provided
| in the URL cannot be matched to a valid route.
|
*/

$route['default_controller'] = "home";
$route['404_override'] = 'error_404';

I’ve also defined views/error_404.php like this:

Code:
<div class="centeredImage">
    <a href="/glup2" target="_blank">
        <img src="css/images/404.png" />
    </a>
</div>

However what shows is a very sober Error 404 page. :-(
Shouldn't it be this simple?

TIA.
#2

[eluser]Pert[/eluser]
What you are defining in routes.php are controllers, not views.

So you need to create error_404 controller, then use that to load your error_404 view.
#3

[eluser]duartix[/eluser]
[quote author="Pert" date="1377780827"]What you are defining in routes.php are controllers, not views.[/quote]
That's a good tip, I had no idea, thank you.

[quote author="Pert" date="1377780827"]So you need to create error_404 controller, then use that to load your error_404 view.[/quote]

OK, basically I created this:

Code:
class error_404 extends CI_Controller {

    public function index() {
        $this->load->view('error_404.php');
    }

}

and it worked like a charm on execution ! Smile Thanks a million!

However, if I manually type a non existent URI, it will still shows the default 404 page. :-(
Is there some simple workaround?
#4

[eluser]gummiforweb[/eluser]
Extend a custom controller and setup the custom 404 page.

1). Create file inside /application/core/MY_Controller.php
Code:
&lt;?php
class My_Controller extends CI_Controller
{
public function __construct()
{
  parent::__construct();
}

public function _remap($method, $params = array())
{
  if (method_exists($this, $method))
  {
   return call_user_func_array(array($this, $method), $params);
  }
  $this->_parse404();
}

public function _parse404()
{
  header("HTTP/1.1 404 Not Found");
  $this->load->view('my_404_page');
}
{


2). Instead of extending CI_Controller, extends MY_Controller on all your controller inside the controllers folder.

3). You are done. Now, if a method doesn't exits inside the controller class, it'll run the _parse404() methods, which will rander the my_404_page.php view file inside the view folder.


Reference:
Remapping Function Calls: http://ellislab.com/codeigniter/user-gui...#remapping
Extending Core Class: http://ellislab.com/codeigniter/user-gui...asses.html
#5

[eluser]Pert[/eluser]
What route settings are you using?




Theme © iAndrew 2016 - Forum software by © MyBB