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)
Related
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.
In my CI 2.2 project I want to make my parent controll with app's common functionality for use in all app and for this I create file :
application/libraries/N_Controller.php :
<?php
class N_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
But on first attempt to use it in file
application/controllers/admin/admin.php
<?php
class Admin extends N_Controller
{
public function __construct()
{
parent::__construct();
I got error:
PHP Fatal error: Class 'N_Controller' not found in /controllers/admin/admin.php on line 3, referer: http://local-ci22.com/admin/hostel/edit/15
I tried to add in application/config/autoload.php file :
$autoload['libraries'] = array( 'AppSmarty', 'AppUtils', 'N_Controller');
But it did not help. Which is the correct way ?
if you want to extend the functionality of a system class. you need to follow this recomendations.
place your extended class in /application/core, be sure that you name it exactly like the name and casing of the class created.
your class should extend from CI_Model or CI_Controller, depending on your needs.
wherever you implement your new class, be sure that you honor the same name casing from your extended class.
you should have configured the $config['subclass_prefix'] on /application/config/config.php. let's say in your case with the value 'N_'
what i can see from your code, you are not extending from CI_Controller and your path seems wrong.
Informative note: the /application/library is used to place classes and libraries from 3rd parties that won't fit into CI schemas.
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
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.]