可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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)); } }
回答1:
class Chat_model extends CI_Controller
should be
class Chat_model extends CI_Model
回答2:
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
回答3:
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
回答4:
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.
回答5:
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,';
回答6:
Add this instead of your Chat controller constructor
public function __construct() { parent::__construct(); $this->load->library('session'); $this->load->model('Chat_model'); }
This loads the session library to your controller so that you can use the methods.
回答7:
Model Extends CI_Model Not CI_Controller class Chat_model extends CI_Model { ............. }
回答8:
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.
回答9:
Modify your library configurations in application/config/autoload.php file
$autoload['libraries'] = array('database','ci_session');
Set the encryption key for your applicatiion in application/config/config.php
$config['encryption_key'] = 'sdjfkdjkfj';
Copy system/libararies/Session/Session.php to application/libraries/ Rename application/libraries/Session.php to CI_Session.php
Now, CI_Session object will be available through below line
$this->ci_session