1

I am trying to learn Codeigniter. I am trying to use application/core/MY_Controller in my CI application. MY_Controller.php as:

class MY_Controller extends CI_Controller {

  protected $data = array();

  function __construct() {
    parent::__construct();
  }

  function render_page($view) {
    //do this to don't repeat in all controllers...
    $this->load->view('templates/header', $this->data);
    //menu_data must contain the structure of the menu...
    //you can populate it from database or helper

    $this->load->view($view, $this->data);
    $this->load->view('templates/footer', $this->data);
  }

}

Now I started to write home controller as :

class Home extends MY_Controller {
         function __construct() {
    parent::__construct();
  }

        public function view($page = 'home')
        {
         $this->load->helper('text');
            $data['records']= $this->services_model->getAll();
            if ( ! file_exists('application/views/pages/'.$page.'.php'))
            {
                // Whoops, we don't have a page for that!
                show_404();
            }

            $data['title'] = ucfirst($page); // Capitalize the first letter


            $this->load->view('pages/'.$page, $data);


        } 

My views folder as

+pages
  +home.php
+templates
  +footer.php
  +header.php. 

Here are my config and route files.

$config['base_url'] = 'http://localhost/~ytsejam/kirmiziblog/';
$config['index_page'] = 'index.php';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

I get 404 page not found error . How can I change my application to work with MY_Controller ?

tereško
  • 58,060
  • 25
  • 98
  • 150
ytsejam
  • 3,291
  • 7
  • 39
  • 69
  • You need to call `home` controller instead of `pages`. – Yan Berk Aug 01 '12 at 14:06
  • $this->load->view('home/'.$page, $data);...like this? – ytsejam Aug 01 '12 at 14:08
  • like this: `$route['default_controller'] = 'home/view'; $route['(:any)'] = 'home/view/$1';` – Yan Berk Aug 01 '12 at 14:09
  • application/core/MY_Controller.php and the other file is application/controllers/home.php. Fixed the name error. I did the routes as you say . but still 404 error. – ytsejam Aug 01 '12 at 14:13
  • What happens if you change `if ( ! file_exists('application/views/pages/'.$page.'.php'))` to `if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))` ? – Mudshark Aug 06 '12 at 14:34

1 Answers1

0

I also had this problem with extending core classes. My problem was that is was working on my local server, but not on my production server. I was receiving the 404 error in production, and only on my controllers that used the class extensions. After some research I noticed that my local server was running php version 5.3.x, while my production server was running 5.2.x. To fix this I had to upgrade my production server to 5.3.

To find out what version of php your site is using, place this command in your view:

<?php echo phpversion() ?>
brightintro
  • 1,016
  • 1
  • 11
  • 16