Rajan
Rajan

Reputation: 2425

How to call Base Controllers Method in Codeigniter?

I Have Defined MY_ Controller in my core folder.

Then i have two controllers:

Admin_Controller

Customer_Controller

Now i want to put a query into my Customer_Controller whose result i can access all the controller which extends to Customer_Controllers.

I have put this code in Customer_Controller

                    public function get_users()
                    {
                        $id = $this->session->userdata('id');
                        $this->db->select('*');
                        $this->db->join('tenant','tenant.id = sites.tenant_id');
                        $this->db->where('tenant.id',$id);
                        $this->db->from('sites');   
                        $query = $this->db->get();
                        $result = $query->result(); 


                        $sitedata = json_decode(json_encode($result));

                        $this->session->set_userdata($sitedata);
                    }

Now when i have a child class something like this

<?php

    class User extends Customer_Controller
    {

        public function__construct()
        {
            parent::__construct();
        }

        public function index()
        {

            //Get Results from Customer_Controller

        }
    }

how do this?

Upvotes: 1

Views: 1215

Answers (4)

noushid p
noushid p

Reputation: 1483

you can follow this:

class Customer_Controller extends My_Controller
{
 public function customer()
  {
     return 'costomers';
  }

}

then

class User extends Customer_Controller
{
  //call the function from Customer_Controller
   $this->customer();
}

Upvotes: 2

jonghyon lee
jonghyon lee

Reputation: 99

Codeigniter is MVC framework. So You will put get_users in model part. example: you must making custom_model.php in models folder.

 <?php 
  class custom_model extends CI_Model {

      function __construct()
      {
          parent::__construct();
      }
      public function get_users($id)
      {
          $this->db->select('*');
          $this->db->join('tenant','tenant.id = sites.tenant_id');
          $this->db->where('tenant.id',$id);
          $this->db->from('sites');   
          $query = $this->db->get();
          $result = $query->result(); 
          return $result;
      }
   }
   ?>

next step: you are remake custom_controller.

public function get_users()
{
   $id = $this->session->userdata('id');
   $this->load->model('custom_model');
   $result=$this->cutom_model->get_users($id);
   $sitedata = json_decode(json_encode($result));
   $this->session->set_userdata($sitedata);

}

and next step:

class User extends Customer_Controller
{

    public function__construct()
    {
        parent::__construct();
    }

    public function index()
    {

        //

    }
    public function get_users(){
         parent::get_users();
    }

Upvotes: 1

Vadivel S
Vadivel S

Reputation: 660

You Try get_instance() like this in you User Controller

  public function get_users()
                {
                    $id = $this->session->userdata('id');
                    $this->db->select('*');
                    $this->db->join('tenant','tenant.id = sites.tenant_id');
                    $this->db->where('tenant.id',$id);
                    $this->db->from('sites');   
                    $query = $this->db->get();
                    $result = $query->result(); 


                    $sitedata = json_decode(json_encode($result));

                    $this->session->set_userdata($sitedata);
                 return $result;
                }

class User extends CI_controller
{

    public function__construct()
    {
        parent::__construct();
    }

    public function index()
    {

        $CI=&get_instance();
        $result=$CI->get_users();
        foreach($result as $row)
        { 
          echo $row->id;//here add you table field name for id 
         }

    }
}

Upvotes: 1

Ronak Rathod
Ronak Rathod

Reputation: 440

Let me give you a suggestion. Instead of creating a controller and extending the same, why don't you create a library class. And then load your library inside the controller you want.

for example here is your library class file.

    <?php

    class users{

        private $session;
        public function __construct(){
            $CI =& get_instance();
            $this->session=$CI->session;
        }

        public function get_users()
        {
              // do the code and return the 
        }
    }

AND in your controller

<?php

    class User extends CI_Controller
    {

        public function__construct()
        {
            parent::__construct();
        }

        public function index()
        {
             $this->load->library('users');
             $users = new users();
             $userinfo = users->get_users();
            //Results from library

        }
    }

Upvotes: 1

Related Questions