By default, as many of you probably know, codeigniter loads controllers from application/controllers (URI Segment 0). Well... I changed it so that it loads them fom application/modules/(URI Segment 0)/controllers/(URI Segment 1 / not set: URI Segment 0). Up to here it all works fine, but when I create a controller, I cannot use any of the default function as $this->load or etc... Example:
/**
* Home Controller Class
*/
class Home extends CI_Controller
{
function __construct()
{
$this->load->database();
}
public function index()
{
echo "Testing...";
}
}
This example would always result in an error:
Severity: Notice
Message: Undefined property: Home::$load
Filename: controllers/home.php
Line Number: 10
Well I don't know how you exactly changed the Controller location.
But when you extend the CI_Controller, and you use a constructor method, you need to execute the parent's __construct() method at first, as follows:
class Home extends CI_Controller
{
function __construct()
{
/* Execute the CI_Controller constructor */
parent::__construct();
$this->load->database();
}
}
That's because your local constructor will be overriding the one in the parent controller class. So you need to manually call it.
CodeIgniter Doc.
Similar topics on SO:
Undefined $load property error after upgrading CodeIgniter 1.7 to 2.1
Codeigniter will not load language
Related
I have this class in /libraries.
class CI_Decorator extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
The error I get is very weird:
Message: Undefined property: CI_Decorator::$load
Filename: libraries/Form_validation.php
Backtrace:
File: /application/libraries/CI_Decorator.php
And is because I try to get CI_Controller constructor with
parent::__construct();
UPDATE:
I am trying to implement this Decorator but I think was developed for an earlier version of CI.
Change the file name to Decorator.php and in the class file use this.
class Decorator{
//there is no parent so you cannot call a parent constructor
//public function __construct() {
//parent::__construct();
//}
}
Only preface a file (or class) name with "CI_" when you wish to replace one of CodeIgniter's core classes. If a core class with that name does not exist the load will fail.
If you want to extend a native class then you will preface the class and file name with MY_ or with the prefix you set in config.php
$config['subclass_prefix'] = 'YOURPREFIX_';
Extending CI_Controller more than once is frustrating too and you will run into a different but related problem. You will find many answers to that question here on Stack Overflow. In the search box above use the search term "[codeigniter] extending CI_controller" (without the quote marks)
I am a beginner in CodeIgniter framework and I have problem with links. I found a couple of pages in which is explained how to link pages, but from some reason I have problem to link two pages. I did this:
Insert Labwork
and that should call controller_proffesor method :
function index(){
$this->load->view('proffesor_view_insert_labwork');
}
i try also this:
Insert Labwork
but when i click on that link this is what i get:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Controller_proffesor::$load
Filename: controllers/controller_proffesor.php
Line Number: 7
Backtrace:
i have also include this line in my autoload file :
$autoload['helper'] = array('url','form');
What am I doing wrong?
Within the constructor have you added:
parent::__construct();
Try adding it inside __construct(). It invokes the constructor of inherited parent class CI_Controller.
Your code should look like this..
class Controller_proffesor extends CI_Controller {
public function __construct()
{
parent::__construct();
//followed by your helper, model load statements
}
NOTE: Refer this https://stackoverflow.com/a/21339891/5176188 for more details. This may be a duplicate question.
Try this:
In your config file you must set something like this:
$autoload['helper'] = array('url','form','html');
In the controller, you must forget to extend it and the name also must concide with the name of your class and it is also like the model extends it to CI_Model
Class Class_Name extends CI_Controller {
public function __construct () {
parent::__construct
// you can load here the session and models
}
}
Hope it helps.
I was creating objects of a class in Codeigniter core classes which I will extend in my controllers but there seems to be problem with loading model after object is created. Here is what I did
Core File:
require_once('folder/systemize.php'); // class systemize is in this file
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$systemize = new systemize();
$systemize->init();
}
After this object is initialized, loading model will not work
Controller file which extends MY_Controller
class Log extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('access_model', 'TEST');
$this->TEST->foo('param1', 'param2'); //function inside access_model
}
It will give me these errors
A PHP Error was encountered
Severity: Notice Message: Undefined property: Log::$TEST
Fatal error: Call to a member function foo() on a non-object
These errors are solved if I don't create object of systemize. Can anyone tell me the how to solve this problem?
EDIT: Systemize File
class Systemize extends CI_Controller
{
function __construct() {
parent::__construct();
}
public function init() {
if('2' == '3') // false for now so it does nothing
{
$this->load->view("system/view_file");
exit();
}
}
Php error indicates on Log File where TEST->foo is called
If I Load Model before creating object, it works but not vice-versa. I still don't understand why is Codeigniter behaving like this?
Ok after going through system files of Codeigniter and printing and echoing inside them I found the source of problem. Now the behavior of CI_Controller is that when we extend CI_Controller, it creates object of Controller and Model and stores it with reference to the pointer.
Now what I did is that I extended systemize with CI_Controller.
MY_Controller extends CI_Controller which means $obj is created storing pointer to all objects of CI which will be used.
But when I manually created object of systemize extending CI_Controller, it has created object of only Controller and not Model and replaced the $obj.
That is why loading model will cause problem since $obj doesn't have Model Object in it.
The solution the problem I found is I did not create object of systemize in MY_Controller. Instead I extended MY_Controller with Systemize and it started working flawlessly.
Another solution is if you don't need CI functionality in your class you can create object of class and not extend it with CI_Controller.
Why I get this error after upgrading CodeIgniter from v1.7 to v2.1?
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Site::$load
Filename: libraries/Website.php
Line Number: 25
Fatal error: Call to a member function library() on a non-object in C:\xampp\htdocs\travel\application\libraries\Website.php on line 25
The library application/library/website
class Website extends CI_Controller {
public static $current_city;
public function __construct() {
$this->load->library('language'); // line 25
$this->language->loadLanguage();
$this->load_main_lang_file();
$this->load_visitor_geographical_data();
$this->load->library('bread_crumb');
}
}
You forgot to call __construct method of CI_Controller class:
public function __construct()
{
// Call CI_Controller construct method first.
parent::__construct();
$this->load->library('language'); // line 25
$this->language->loadLanguage();
$this->load_main_lang_file();
$this->load_visitor_geographical_data();
$this->load->library('bread_crumb');
}
Note: If you're creating a Controller, it should be placed in application/controllers/, not in application/libraries/.
If the child (inheritor) class has a constructor, the parent constructor won't be called, because you'll override parent's constructor with the child one, unless you explicitly call parent's constructor using parent::__construct();. That's the concept of Polymorphism in object-oriented programming
If you don't call parent::__construct(); when the application controller is initializing, you'll lose Loader and Core class and $this->load would never works.
Using parent::__construct(); is needed only if you want to declare __construct() method in your Controller which it will override the parent's one.
That's true for models as well, but using parent::__construct(); in your model just logs a debug message Model Class Initialized, So if you need to know when Model is initialized (in logs), keep using that, If not, ignore it.
So I'm trying to extend the input library (CI 2.1.1), and when I call my custom save query function, its saying the function doesn't exist.
File: MY_Input.php, in the applications/libraries folder:_
class MY_Input extends CI_Input {
var $CI;
function __construct() {
parent::__construct();
$this->CI =& get_instance();
}
function save_query($query_array) {
$this->CI->db->insert('ci_query', array('query_string' => http_build_query($query_array)));
}
}
And in the controller I'm calling the function like this
$query_id = $this->input->save_query($query_array);
So what on earth am I doing wrong that it's giving me this error:_
Fatal error: Call to undefined method CI_Input::save_query() in ....
Can't see why it's not working, I even checked the user guide and according to it I guess I'm doing it right. :/
The CI_Input class is a core library (new thing in CI2.0.0). You will have to put your MY_Input.php file under application/core/ to make the framework pick it up.
When in doubt, look for the original class under system/core or system/libraries and mirror it under application/.