我的模型和控制器出了什么问题?

我的模型和控制器出了什么问题?

问题描述:

For the life of me I cannot understand why I am receiving errors with my install of CodeIgniter and this current MVC set up.

The error I see is

Fatal error: Call to undefined method Login::users_model() in /var/www/application/controllers/login.php on line 17

Controller application/controllers/login.php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

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

    public function index()
    {

    $data = '';

    $this->load->model('users_model');

    $data['testing'] = $this->users_model()->test_user();

    $this->load->view('home',$data);


    }
}

Model application/models/users_model.php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_model extends CI_Model {

  function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }


  function test_user() {
      return 'Test User #1';
  }



}

View application/views/home.php

echo $testing;

对于我的生活,我无法理解为什么我在安装CodeIgniter和当前的MVC设置时收到错误 。 p>

我看到的错误是 p>

致命错误:在/ var / www / application / controllers中调用未定义的方法Login :: users_model() 第17行/login.php p>

控制器 strong> application / controllers / login.php p>

  if(!  defined('BASEPATH'))exit('不允许直接访问脚本'); 
 
class登录扩展CI_Controller {
 
公共函数__construct()
 {
 parent :: __ construct(); 
}  
 
公共函数索引()
 {
 
 $ data =''; 
 
 $ this-> load-> model('users_model'); 
 
 $ data ['  testing'] = $ this-> users_model() - > test_user(); 
 
 $ this-> load-> view('home',$ data); 
 
 
} \  n} 
  code>  pre> 
 
 

模型 strong> application / models / users_model.php p>

  if(  !defined('BASEPATH'))退出('没有直接脚本访问权限 允许'); 
 
class Users_model扩展CI_Model {
 
函数__construct()
 {
 //调用模型构造函数
 parent :: __ construct(); 
} 
 
 
函数 test_user(){
返回'测试用户#1'; 
} 
 
 
 
} 
   code>  pre> 
 
 

查看 strong > application / views / home.php p>

  echo $ testing; 
  code>  pre> 
  div>

No need for function bracket with model name

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

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

    public function index()
    {

    $data = '';

    $this->load->model('users_model');

    $data['testing'] = $this->users_model->test_user();

    $this->load->view('home',$data);

    }
}

Replace $data['testing'] = $this->users_model()->test_user(); with $data['testing'] = $this->users_model->test_user(); in your controller.

Simply load your model inside the constructor

public function __construct()
{
    parent::__construct();
    $this->load->model('users_model');
}

Then simply call the relevant functions inside any controller functions like this.

public function index()
{

$data = '';

$data['testing'] =  $this-> users_model-> test_user();

$this->load->view('home',$data);
}