Welcome Guest, Not a member yet? Register   Sign In
Not always showing 404_error custom page
#1

Hi, 

I have been working with CI for just few weeks, and am still discovering and learning new things about it. Today I have faced an issue and although I have done some reasearch, I am not able to solve it. 

The thing is that I am running codeigniter (version 3) in localhost. And I am trying to customize the 404 error page. To do so, after following some tutorials out there, I did the following changes. 

First, I have created a CONTROLLER (Not_found.php): 
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class 
Not_found extends CI_Controller {
  
  
public function __construct() {
    parent::__construct();
  }
  
  
public function index() {
    $this->output->set_status_header('404');
    
    $this
->load->view('templates/header');
    $this->load->view('pages/404');
    $this->load->view('templates/footer');
  }


Then, I have generated the view, that can be found under: views/pages/404.php. 

To finish the job, I have modified the ROUTING adapting the file /config/routes.php as follows: 
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

$route['content/(:any)'] = 'content/view/$1';
$route['wishlist'] = 'wishlist/view';
$route['admin'] = 'admin/view';

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = 'not_found';
$route['translate_uri_dashes'] = FALSE

Now, what happens is this. If I try to access an url that does not exist (for example: /localhost/this-does-not-exist), it shows the CI standard 404 error page. But, if I try to access any other random url containing a folder, then it works fine (for example: /localhost/folder/this-does-not-exist). 

Also, if I try to access urls under the content folder, they also show the CI standard 404 error page. So I guess that the problem is regarding the (:any) in the routes. But I am not able to find a solution to this while keeping both functionalities working together. 

What are your thoughts on this?

Thank you so much!
Regards Smile
Reply
#2

By the way, I forgot to share also the code inside the Pages.php controller (the Content.php controller is very similar): 
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class 
Pages extends CI_Controller {
  
  
public function view($page 'home'){
    if(!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
      show_404();
    }
    
    $data
['title'] = ucfirst($page);
    
    $this
->load->view('templates/header');
    $this->load->view('pages/'.$page$data);
    $this->load->view('templates/footer');
  }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB