I created a base_model extends CI_Model class and put it in application/model. Then, I created another class that extends base_model, but I get the following error:
Cannot find this class
Other resources told me to put the base_model in:
application/core
or:
application/libraries
However, I would like to put it in application/models for convenience. Can I put this class in application/models and still have it work correctly?
Try putting a MY_Model class in your application/core folder instead. First-level inheritance was built into CI by default, i.e.:
{APPATH}/core/MY_Model.php:
<?php
class MY_Model extends CI_Model {
public function __construct()
{
parent::__construct();
}
}
In that same file, if you want alternate parent classes, try:
...
class Base_model extends MY_Model {
public function __construct()
{
parent::__construct();
}
}
...
If you don't put it in MY_Model, YOU ARE RESPONSIBLE for loading the base model first (before an extended class references it), AND you can't have a file in your models/ folder called Base_model.php, for example.
Related
I have the following scenario:
Controller:
class Collect extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('phirehose');
$this->load->library('oauthphirehose');
$this->load->library('ghettoqueuecollector');
}
function index() {
// Start streaming/collecting
$this->ghettoqueuecollector('datos');
...
}
Class:
A)
class Ghettoqueuecollector extends Oauthphirehose {
...
}
B)
abstract class Oauthphirehose extends Phirehose {
...
}
C)
abstract class Phirehose {
...
}
When I try to use the controller gives this error:
"Fatal error: Cannot instantiate abstract class Phirehose"
That is lacking adapt? Codeigniter outside these classes work using require. Can I use them in CI? Thanks
$this->load->library() is made to include, instantiate and configure classes, it's not just an alias for require() - you can't use it in that manner.
If you need to extend an abstract class, you'll have to manually include/require it from the file that extends it, like this:
libraries/Oauthphirehose.php:
require_once __DIR__.'/Phirehose.php';
abstract class Oauthphirehose extends Phirehose {}
libraries/Ghettoqueuecollector.php:
require_once __DIR__.'/Oauthphirehose.php';
class Ghettoqueuecollector extends Oauthphirehose {}
Then you just load the one library that you actually need to instantiate:
$this->load->library('ghettoqueuecollector');
Am working with wiredesignz modular extensions for codeigniter, and i was wondering if its possible to access a regular codeigniter controller's public property from a module's controller
for example, this is a regular ci controller
<?php
class Dog extends CI_Controller {
public $name;
function __construct()
{
$this->name = "xyz";
}
}
and this a module controller
<?php
class Test extends MX_Controller {
function get_name()
{
//access the $name property of the dog controller here
}
}
If you're using HMVC there's no reason why all or any of your controllers can't inherit from MX_Controller. You can have controllers in your normal application/controllers or application/core folders that inherit MX_Controller, they don't have to be "module" controllers.
If you need to access properties from one controller in another why not create a base controller e.g. MY_Controller that extends MX_Controller, put it in either application/controllers or application/core and then every time you create a "module" controller simply inherit from MY_Controller instead of MX_Controller.
Don't forget you can load any module controller and use it like a library class.
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.]
I need to have tow type of extended controller class, one for admin, and other for the rest.
I create first one named MY_Controller, how to make the other.
like the first ...
In the file core/Admin_controller.php
Class Admin_controller extends CI_Controller{
...
}
or
Class Admin_controller extends MY_Controller{
...
}
And then ...
Class MyFirstAdmin extends Admin_controller{
...
}
I have strictly followed the how-to article by Phil Sturgeon, to extend the base controller. But I get still some errors.
My 3 classes:
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
// application/libraries/Public_Controller.php
class Public_Controller extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
// application/controllers/user.php
class User extends Public_Controller{
public function __construct(){
parent::__construct();
}
}
Fatal error: Class 'Public_Controller' not found in /srv/www/xxx/application/controllers/user.php on line 2
Curious is that the following snippet is working, if I directly extends from MY_Controller:
// application/controllers/user.php
class User extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
I have loaded the controllers via __autoload() or manually. The controllers are loaded succesfully.
CI-Version: 1.7.3
You need to require the Public Controller in your MY_Controller
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
require(APPPATH.'libraries/Public_Controller.php');
You get the error because Public_Controller was never loaded. Doing this would allow you to extend from Public_Controller
I like what you are doing because I do that all the time.
You can do this also in your MY_Controller when you want to create an Admin_Controller
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
require(APPPATH.'libraries/Public_Controller.php'); // contains some logic applicable only to `public` controllers
require(APPPATH.'libraries/Admin_Controller.php'); // contains some logic applicable only to `admin` controllers
You should place Public_controller in with MY_Controller inside MY_Controller.php
// application/libraries/MY_Controller.php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
}
}
class Public_Controller extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
I use __construct everywhere and it works fine I recently wrote up an article on how to do this in relation to wrapping your auth logic into your extended controllers. It's about half way down when I start discussing constructing your controllers.
Problem was solved here: http://devcrap.net/pl/2011/09/04/codeigniter-dziedziczenie-z-my_controller-extends-my_controller/. In polish but code is good :]
I had problem like this,After some search I found error was made myself,Because my controller class name was MY_Controller but file name was My_Controller[Case not matching].
Note:- In localhost I didnt have any error.
In extended controller I Use
class Home extends MY_Controller{
function __construct() {
parent::__construct();
}
}
even I got the error.
After changing my file name to MY_Controller it started to work well.
I have a custom controller class called MY_Controller it extends CI_Controller and it works fine. It is located at application/core and it has custom functions lo load views in my site.
I use an abstract class My_app_controller that extends MY_Controller for my_app specific behabior, I want every controller in my_app to extend this abstract class. (I use diferent apps in the site, so some apps will extend My_app_controller and other apps will extend My_other_apps_controllers)
But when I try to extend My_app_controller from any controller in my application, "Main_app_controller extends My_app_controller" generates a Class 'My_app_controller' not found exception.
I found two solutions:
use include_once in Main_app_controller.php file.
include_once APPPATH.'controllers/path/to/My_app_controler.php';
break the "one class per file" rule of code igniter and define my My_app_controller just in the same file MY_Controller is (under application/core).
Manual says:
Use separate files for each class, unless the classes are closely
related
Well... they are.
Anyway, I prefered to use the include_once solution as I think it is better to have one file per class and My_app_controller is located under application/controllers/my_app folder. (so application/controllers/other_apps will exist)