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));
}
}
class Chat_model extends CI_Controller
should be
class Chat_model extends CI_Model
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 {}
I get the same error message when I involve the PDF library with the class name Pdf.php and load it via autoload as 'pdf'.
My mistake, the controller that will display my page I also named Pdf.php, and the error message appears ( that's the reason why I found this question :) ). The problem was immediately solved after I replaced the name of my controller with another name.
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
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.
I encountered with same error. I attached "session" on --->
yourproject/application/config/autoload.php
$autoload['drivers'] = array("session");
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,';
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.
In your Chat_model, try to change class Chat_model extends CI_Controller to class Chat_model extends CI_Model.
If none of the above solution worked, my workaround was:
In my autoload config, I had a library called 'auth'.
however, in my custom controller, somehow it was being messed up because 'auth' wasn't being auto-loaded(I've checked everything). Since I am only using it when logging in, I removed it from my autoload config and load it in my custom controller.
I never had this issue before but suddenly one day it happened.
Using [CodeIgniter v3]
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.
Model Extends CI_Model Not CI_Controller
class Chat_model extends CI_Model {
.............
}
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
Related
I am trying extending CodeIgniter controller in my application using composer but it's not working.
This give me
Fatal error: Class 'CI_Controller' not found in D:\xampp\htdocs\ci-dev\application\core\MY_Controller.php on line 11
i knew that if i add spl_autoload_register in my config.php then it is work but i want to use composer.
here is my all set up.
i create MY_Controller in my application/core/MY_Controller.php
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
}
}
after this i add admin controller in application/libraries/Admin_Controller.php
class Admin_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
and front-end controller in application/libraries/Frontend_Controller.php
class Frontend_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
This is my default controller index call
class Welcome extends Frontend_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
i set up my composer like this in config.php
$config['composer_autoload'] = FCPATH.'../vendor/autoload.php';
and composer.json file like this
"autoload": {
"files" : [
"application/core/MY_Controller.php",
"application/libraries/Admin_Controller.php",
"application/libraries/Frontend_Controller.php"
]
},
CodeIgniter loads CI_Controller after your vendor/autoload.php file, and since you're listing them under the "files" option in your composer.json, they're included immediately as opposed to right when you need them.
That's not only what causes the error, but also beats the entire purpose of using an autoloader - if you'd be explicitly listing the includes, you might as well just require_once them.
What's common in CI, is to require or even directly declare your multiple base controller classes from inside MY_Controller.php - then you know they'll be available exactly when you need them.
But if you insist on loading them through Composer, there's a work-around - list system/core/Controller.php under the autoloaded files as well.
After Hosting Php codeignitor website I Got a Error "Unable to locate the model you have specified: Common_model"
But Its Works in my local server
How I can solve this issue
I already change First letter of the model class name capitalized but no result
my model class
class Common_model extends CI_Model
{
}
and controller is
<?php
class Show extends CI_Controller
{ var $pageLimit = 6;
public function __construct()
{
parent::__construct();
$this->load->library('ion_auth');
$this->load->model('common/Common_model');
$this->load->helper('url');
$this->load->helper('form');
$this->load->helper('custom');
}
}
If you are are using Codeignitor 3 than make sure you file name also start with capital letter as:
Common_model.php
It's recommended to use your models inside the model folder not model/anyfolder
Codeignitor 3 Change Log User Manual
I hope Your model file structure is like
application
model
common
Common_model.php
File name should be Common_model.php.
Load in Controller should be
$this->load->model('common_model');
I always load it with the name only, this way
$this->load->model('Common_model');
Always the controller is on the same module (if you are using modules)
I have created custom controller called "MY_Controller.php" in Application/core, and successfully invoked by inheriting through application controller.
//application/core
class MY_AdminController extends CI_Controller {
function __construct() {
parent::__construct();
}
}
//application/controllers
class User extends MY_AdminController {
public function __construct(){
parent::__construct();
}
}
It works fine. I just changed my file name from "MY_Controller.php" to MY_AdminController.php, and following same class name, but it is throwing following error,
Fatal error: Class 'MY_AdminController' not found
As per the documentation, Whenever you create a class with the MY_ prefix the CodeIgniter Loader class will load this after loading the core library, then why its throwing error...!!!
Go to your config.php and change
$config['subclass_prefix'] = 'MY_'; to $config['subclass_prefix'] = 'MY_Admin';
Expanding on Patel,
the issue is that MY_ is the prefix to the original core files.
Controller, Model, View etc.
MY_ will be used to seek the name of the controller, for example, MY_controller searches for CI_controller.
You cannot load random names using the MY_prefix. you use MY_ to extend the already existing names.
I think the problem is with the class name, you may have not changed the class name from MY_Controller to MY_AdminController
You can use custom classes. But CI only loads the classes with the class prefix in the config file (e.g. MY_). So I explained how it works and created a workaround to load classes automatically. You can find it here https://stackoverflow.com/a/22125436/567854.
Hope this helps :)
If you want to include all the files that are in your core folder. Then write the following code in end of config.php file.Path to file is application/config/config.php
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'core/' . $class . EXT );
}
}
By using this you can create multiple classes.
I was able to implement the Tank Auth library with my website, but have an issue when I move the autoload of the library from the Auth controller to the codeignitier autoload library.
As you can see below I have commented out the auto load of the tank Auth library, if I load it here then everything works fine.
class Auth extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
//$this->load->library('form_validation');
$this->load->library('security');
// $this->load->library('tank_auth');
$this->lang->load('tank_auth');
}
THIS is now I load it in the codeignitier, as you can see pretty straight forward
$autoload['libraries'] = array('form_validation','email','upload','tank_auth');
this is the controller that I have defined
class MY_Controller extends CI_Controller
{
public $layout;
public function __construct()
{
//this sets where the header and footer file is loacated
parent::__construct();
$this->layout = 'layout/master';
}
}
The error I get when autoloading is The model name you are loading is the name of a resource that is already being used: users. Obviously it looks like its trying to create the object twice.
Why would autoloading before the Auth library cause this issue, when its auto loaded anyway in the Auth controller, maybe I'm missing some vital piece of understanding of codignitier.
thanks
Why don't you try renaming the library file? Besides, there is no harm in loading the library in the _construct.
I have class MY_Controller extends CI_Controller and common logic for big profile section, so I'va tried to create class Profile extends MY_Controller with common logic for profile section and all class related to this section should extends this Profile class as I understand right, but when I tried to create class Index extends Profile I recieve an error:
Fatal error: Class 'Profile' not found
CodeIgniter tries to find this class in index.php which I am running.
Where is my mistake? Or maybe there is anoter better way to mark out common logic?
I take it you have put your MY_Controller in /application/core, and set the prefix in the config.
I would be careful about using index as a class name though. As a function/method in Codeigniter it has a dedicated behaviour.
If you then want to extend that controller you need to put the classes in the same file.
E.g. In /application core
/* start of php file */
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
...
}
class another_controller extends MY_Controller {
public function __construct() {
parent::__construct();
}
...
}
/* end of php file */
In /application/controllers
class foo extends MY_Controller {
public function __construct() {
parent::__construct();
}
...
}
or
class bar extends another_controller {
public function __construct() {
parent::__construct();
}
...
}
I found this page on Google because I had the same problem. I didn't like the answers listed here so I created my own solution.
1) Place your parent class in the core folder.
2) Place an include statement at the beginning of all classes that include the parent class.
So a typical controller might look like this:
<?php
require_once APPPATH . 'core/Your_Base_Class.php';
// must use require_once instead of include or you will get an error when loading 404 pages
class NormalController extends Your_Base_Class
{
public function __construct()
{
parent::__construct();
// authentication/permissions code, or whatever you want to put here
}
// your methods go here
}
The reason I like this solution is, the whole point of creating a parent class is to cut down on code repetition. So I don't like the other answer that suggested copy/pasting the parent class into all of your controller classes.
It is possible with Codeigniter 3. Just including the parent file is enough.
require_once(APPPATH."controllers/MyParentController.php");
class MyChildController extends MyParentController {
...
All classes you are extending should live in application/CORE directory so in your case both My_Controller and Profile should live there. All "end point" controllers will live in application/controllers folder
UPDATE
I stand corrected. Extended classes should live in the same file. #Rooneyl's answer shows how to implement
After some struggle with version 3 and this issue I decided this was not a bad solution...
require_once BASEPATH.'core/Controller.php';
require_once APPPATH.'core/MYCI_Controller.php';
to add this second line where the first exists in the system/core/CodeIgniter.php
[If it's not too late, I recommend strongly against php and/or CodeIgniter.]