php CodeIgniter - 调用非对象上的成员函数 model()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17688688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter - Call to a member function model() on a non-object
提问by mXX
I have a login form that is being created in the view. Then in my controller I'm trying to load my model but it doesn't seem to load. Looked up a few answer here on stackoverflow and almost everybody says autoload model, import the database library,etc. I did that but I'm still getting the error
我有一个正在视图中创建的登录表单。然后在我的控制器中,我试图加载我的模型,但它似乎没有加载。在stackoverflow上查了一些答案,几乎每个人都说自动加载模型,导入数据库库等。我这样做了,但我仍然收到错误
Fatal error: Call to a member function model() on a non-object in C:\xampp\htdocs\project\application\controllers\login.php on line 11
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$load
Filename: controllers/login.php
Line Number: 11
login_view
登录视图
<?php
$loginEmail = array('placeholder' => "Email", 'name' => "loginEmail");
$loginPassword = array('placeholder' => "Wachtwoord", 'name' => "loginPassword");
$loginSubmit = array('name' => "loginSubmit", 'class' => "btn", 'value' => "Inloggen");
$loginForgot = array('name' => "loginForgot", 'class' => "link", 'value' => "Wachtwoord vergeten?");
echo form_open('login/login', array('class' => 'grid-100 formc'));
echo form_input($loginEmail);
echo form_password($loginPassword);
echo form_submit($loginSubmit);
echo form_submit($loginForgot);
?>
login_controller
登录控制器
<?php
Class Login extends CI_Controller{
function index(){
$data['content'] = 'login_view';
$this->load->view('templates/template', $data);
}
function login(){
$this->load->model('login_model');
$query = $this->login_model->validate();
if($query){
$data = array(
'username' => $this->input->post('loginEmail'),
'loggedin' => true
);
$this->session->set_userdata($data);
redirect('profile/myprofile');
}
}
}
?>
login_model
登录模型
<?php
Class Login_model extends CI_Model{
function validate(){
$this->db->where('email', $this->input->post('loginEmail'));
$this->db->where('password', md5($this->input->post('loginPassword')));
$query = $this->db->get('tbl_users');
if($query->num_rows == 1){
return true;
}
}
}
?>
autoload.php
自动加载.php
$autoload['libraries'] = array('database', 'session');
What am I missing here?
我在这里缺少什么?
回答by SirCharles
You don't have a constructor, in your login controller:
您的登录控制器中没有构造函数:
public function __construct() {
parent::__construct();
}
回答by Karan Ashar
According to CodeIgniter docs
根据 CodeIgniter 文档
If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it: parent::__construct();
如果您打算在任何控制器中使用构造函数,则必须在其中放置以下代码行: parent::__construct();
Refer CI Constructorfor more.
有关更多信息,请参阅CI 构造函数。