2012-08-01 135 views
1

我想学习Codeigniter。我正在尝试在我的CI应用程序中使用application/core/MY_Controller。 MY_Controller.php为:扩展核心类Codeigniter - 404错误

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); 
    } 

} 

现在我就开始给家里写信控制器:

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); 


     } 

我的意见

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

以下文件夹中的我的配置和路由文件。

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

我收到404页未找到错误。我如何更改我的应用程序以使用MY_Controller?

+0

你需要调用'home'控制器,而不是'pages'。 – 2012-08-01 14:06:06

+0

$ this-> load-> view('home /'.$ page,$ data); ...像这样? – ytsejam 2012-08-01 14:08:25

+0

像这样:'$ route ['default_controller'] ='home/view'; $ route ['(:any)'] ='home/view/$ 1';' – 2012-08-01 14:09:12

回答

0

我也有扩展核心类的这个问题。我的问题是在我的本地服务器上工作,而不是在我的生产服务器上。我在生产中收到了404错误,并且只在使用类扩展的控制器上。经过一番研究,我发现我的本地服务器运行的是php版本5.3.x,而我的生产服务器运行的是5.2.x.要解决这个问题,我不得不将我的生产服务器升级到5.3。

要了解您的网站使用PHP的什么版本,在您看来把这个命令:

<?php echo phpversion() ?>