achll
achll

Reputation: 163

calling x controller function inside a y controller function codeigniter

I'm new to codeigniter and I have two controllers: utility.phpand welcome.php.

In utility.php, I have functions:

   function getdata() {
     //code here
   }

   function logdata() {
     //code here
   }

Inside welcome.php, I have this function:

   function showpage() {
     //some code here
     //call functions here
   }

What I want to do is inside my welcome.php, I want to call the functions from utility.php. How do I do this? Thanks.

Upvotes: 1

Views: 319

Answers (3)

Kyslik
Kyslik

Reputation: 8385

That is out of concept, if you want to call code in different controllers do following:

  1. create helper and call function whenever (even in view) you want.
  2. extend CI_Controller so you can use one or more functions in any controller that is extended.

Lets start with helper:

create file in folder application/helpers/summation_helper.php

use following sample of code

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

function sum($a, $b) {

    //$CI =& get_instance(); 
    // if you want to use $this variable you need to get instance of it so now instead of using $this->load... you may use $CI->load
    $return = $a + $b;
    return number_format($return, 3);

}

If you are going to use your helper in many controllers/views please load it in autoload, otherwise just load it manualy $this->load->helper('summation');

Extending Controller_CI: this is way better approach if you are using database. Please follow this tutorial, which explains it all.

*I have crafted an answer just before site went down posting this from cellphone.

Upvotes: 0

Gregor
Gregor

Reputation: 622

You don't is the short answer.

The point of MVC is to have your code well organized.

What you can do though is create a library in the libraries folder where you put methods you need in more than one controller.

For example you can make a mylog library, where you can put all your log related stuff. In any controller you will then call:

$this->load->library('mylog');
$this->mylog->logdata();

Besides functions that deal with data models should reside in models. You can call any model from any controller in CI

Upvotes: 1

Guns
Guns

Reputation: 2728

Refrence from here

To extend controller please either follow this tutorial or see some code below.


differences between private/public/protected


make a file in folder /application/core/ named MY_Controller.php

Within that file have some code like

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

class MY_Controller extends CI_Controller {

    protected $data = Array(); //protected variables goes here its declaration

    function __construct() {

        parent::__construct();
        $this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not        
        $this->load->model('some_model'); //this can be also done in autoload...
        //load helpers and everything here like form_helper etc
    }

    protected function protectedOne() {

    }

    public function publicOne() {

    }

    private function _privateOne() {

    }

    protected function render($view_file) {

        $this->load->view('header_view');
        if ($this->_is_admin()) $this->load->view('admin_menu_view');

        $this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
        $this->load->view('footer_view');

    }

    private function _isAdmin() {

        return TRUE;

    }

}

and now in any of yours existing controllers just edit 1st or 2nd line where

class <controller_name> extends MY_Controller {

and you are done

also note that all your variables that are meant to be used in view are in this variable (array) $this->data

example of some controller that is extended by MY_Controller

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

class About extends MY_Controller {

    public function __construct() {

        parent::__construct();

    }

    public function index() {
        $this->data['today'] = date('Y-m-d'); //in view it will be $today;
        $this->render('page/about_us'); //calling common function declared in MY_Controller
    }

}

Upvotes: 1

Related Questions