Extending Core Class Codeigniter - 404 Error

自古美人都是妖i 提交于 2019-12-11 09:35:50

问题


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 ?


回答1:


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() ?>


来源:https://stackoverflow.com/questions/11760986/extending-core-class-codeigniter-404-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!