出于某种原因,每当我尝试从控制器调用模型中的函数时,它都会返回错误`
PHP Fatal error: Call to undefined method Test_model::ajax() in /var/www/CodeIgniter/application/controllers/blog.php on line 19, referer: http://localhost/CodeIgniter/index.php/blog`
这是模型文件夹中名为 test_model.php 的模型
<?php
class Test_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function ajax(){
echo 'ajax successful';
}
}
?>
这是我的控制器,它试图在上面的模型中使用 ajax 方法
<?php
class Blog extends CI_Controller {
public function index()
{
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->view('blogview', $data);
}
public function comments()
{
echo 'Look at this!';
}
public function ajax()
{
$this->load->model("test_model");
$this->test_model->ajax();
}
}
?>
那么为什么我会收到未定义的方法错误?
编辑 通过 sugesstion 我将我的控制器更改为以下内容,以包括在 index 的操作中加载模型
<?php
class Blog extends CI_Controller {
public function index()
{
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
$this->load->model("test_model");
$this->load->view('blogview', $data);
}
public function comments()
{
echo 'Look at this!';
}
public function ajax()
{
$this->test_model->ajax();
}
}
?>
这将返回错误
PHP Fatal error: Call to a member function ajax() on a non-object in /var/www/CodeIgniter/application/controllers/blog.php on line 19, referer: http://localhost/CodeIgniter/index.php/blog