Unable to locate the specified class: Session.php

2019-02-22 10:30发布

The browser:

Unable to locate the specified class: Session.php

This is my Controller:

<?php

class Chat extends CI_Controller {

    public function __construct() {

        parent::__construct();
        $this->load->model('Chat_model');
    }

    public function index() {

        $this->view_data['chat_id'] = 1;
        $this->view_data['student_id'] = $this->session->userdata('student_id');
        $this->view_data['page_content'] = 'chat';
        $this->load->view('chat');
    }

    public function ajax_addChatMessage() {

        $chat_id = $this->input->post('chat_id');
        $student_id = $this->input->post('student_id');
        $bericht = $this->input->post('chat_id', TRUE);

        $this->Chat_model->addChatMessage($chat_id, $student_id, $bericht);
    }
}

When I put my model in comment in the parent::__construct(); // $this->load->model('Chat_model'); the error is gone.

This is my Chat_model:

<?php

class Chat_model extends CI_Controller {

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

    public function addChatMessage($chat_id, $student_id, $bericht) {

        $query = "INSERT INTO tbl_chatberichten (chat_id, student_id, bericht) VALUES (?,?,?)";
        $this->db->query($query, array($chat_id, $student_id, $bericht));

    }

}

10条回答
Emotional °昔
2楼-- · 2019-02-22 10:55

Add changes into your library configurations in application/config/autoload.php file

$autoload['libraries'] = array('database', 'session');

and in application/config/config.php set the encryption key(any key u like)

$config['encryption_key'] = 'thu23456789#[n,';

If you still get same error then copy System/library/Session/Session.php to System/library/ folder, then it should work

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-22 11:03

If you use Codeigniter Modular Extensions HMVC this error can occur if you forget to change your class to extend MX_Controller instead of CI_Controller

So in your case you would start your class with:

class Chat extends MX_Controller {}

Instead of:

class Chat extends CI_Controller {}

Hope this helps someone who runs into the same issue

查看更多
霸刀☆藐视天下
4楼-- · 2019-02-22 11:03

Change your load library configuration in application/config/autoload.php file

  $autoload['libraries'] = array('database', 'session');

In application/config/config.php set the encryption key(any key u like)

  $config['encryption_key'] = 'thu23456789#[n,';
查看更多
我只想做你的唯一
5楼-- · 2019-02-22 11:04

My solution: Preface: If you don't use hooks, this will not be the solution for you.

I had this same issue, after upgrading to v3.0.6, and I definitely had everything setup correctly, as this is an existing site just being upgraded to v3+. My issue boiled down to hooks that I had loading 'pre-controller'. My hooks worked with v 2.0.X of CodeIgniter, but not with v3+.

If you are loading hooks before the session class is loaded, and your hook has a dependency on the session class, that may be your culprit. Try changing any pre-controller hooks to post-controller, or commenting them out completely to see if that fixes your issue. This is all located in application/config/hooks.php.

查看更多
Explosion°爆炸
6楼-- · 2019-02-22 11:09
class Chat_model extends CI_Controller

should be

class Chat_model extends CI_Model
查看更多
时光不老,我们不散
7楼-- · 2019-02-22 11:09

In my case the library name and controller class name was same i.e my controller class name was Ajaxer and also my library class name was Ajaxer. I changed my library class name to Ajaxer_lib and its resolved the issue.

查看更多
登录 后发表回答