Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extend the code igniter controller class?

In my CI system\libraries directory I have a new class named DD_Controller.php. This file looks like this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class DD_Controller extends Controller 
{   
    protected $ddauthentication;


    function __construct()
    {           
        parent::Controller();
        $this->ddauthentication = "Authenticated";
    }
}
?>

My application controller is defined like this:

class Inquiry extends DD_Controller 
{...}

The Inquiry class works fine when I extend Controller, but I get a

Fatal error: Class 'DD_Controller' not found in C:\development\localhost\applications\inquiry\controllers\inquiry.php on line 4

When I extend DD_Controller. In the config file I have the prefix defined as such:

$config['subclass_prefix'] = 'DD_';

Any idea of what I'm missing?

TIA

like image 242
ChronoFish Avatar asked Oct 26 '09 17:10

ChronoFish


People also ask

Which keyword is used to extend of class in CodeIgniter?

The extends keyword is used to derive a class from another class. This is called inheritance. A derived class has all of the public and protected properties of the class that it is derived from.

How do I make a CI controller?

CodeIgniter permits you to do this. Simply create sub-directories under the main application/controllers/ one and place your controller classes within them. Each of your sub-directories may contain a default controller which will be called if the URL contains only the sub-directory.

What is the default controller in CodeIgniter?

Defining a Default ControllerCodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes. php file and set this variable: $route['default_controller'] = ' Blog ';

How will you call a constructor in CodeIgniter?

Constructor is a function that is automatically called when instantiated. this function helps us to intialize the things that we are going to need frequently in our code like when we have to load the models of helpers like form e.t.c. $this->load->model('Model_name');


2 Answers

This is a better approach. Do the following:

  1. Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
  2. Open this the file you just created and add your multiple classes, like so:

    class Admin_Parent extends CI_Controller {
        public function __construct() {
            parent::__construct();
        }
    
        public function test() {
            var_dump("from Admin_Parent");
        }
    }
    
    class User_Parent extends CI_Controller {
    
        public function __construct() {
            parent::__construct();
        }
    
        public function test(){
            var_dump("from User_Parent");
        }
    
    }
    
  3. Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php

  4. Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:

    class Adminchild extends Admin_Parent {
    
        function __construct() {
            parent::__construct();
        }
    
        function test() {
            parent::test();
        }
    
    }
    
like image 196
CodeGodie Avatar answered Oct 19 '22 01:10

CodeGodie


DD_Controller.php should be in /system/application/libraries/

If you're using the same CI for multiple apps, and you want them all to be able to extends their controllers to your custom one then you can extend the base Controller class in the same file.

In system/libraries/Controller.php below the Controller class:

class Mega_Controller extends Controller {
    function Mega_Controller()
    {
        parent::Controller();
        // anything you want to do in every controller, ye shall perform here.
    }
}

Then you'll be able to do this in your app controllers:

class Home extends Mega_Controller {
    ....

Since the extended controller class you created will be available. I think this is better then overwriting the base controller, but that would work as well.

like image 30
Matthew Rapati Avatar answered Oct 19 '22 01:10

Matthew Rapati