I have one controller named home.php
in which a function named podetails
is there. I want to call this function in another controller user.php
.
Is it possible to do so? I have read about HMVC
in CI, but I want to know is it possible to do without using hmvc?
Asked
Active
Viewed 4.8k times
9

user2936213
- 1,021
- 1
- 8
- 19
-
explain with example and use – Adarsh M Pallickal Jan 14 '14 at 09:44
-
5I have explained it very clearly. Please read the question again. – user2936213 Jan 14 '14 at 09:45
-
please specify what is the use? – Adarsh M Pallickal Jan 14 '14 at 09:51
-
1I just want to use that function in user.php controller. thats it. – user2936213 Jan 14 '14 at 09:52
-
Suppose function is in first controller what is the use of recall it in another controller? – Adarsh M Pallickal Jan 14 '14 at 09:59
-
its my project requirement. its nothing related to "use of the function" – user2936213 Jan 14 '14 at 10:01
-
You should not do that, anyway, and besides you cannot without HMVC, in the current CI's structure. You could always redirect, though – Damien Pirsy Jan 14 '14 at 10:07
-
7So you are using common code for both controllers. Then you make it as library function / or helper function. You can call anywhere in project. – Kumar V Jan 14 '14 at 10:07
-
Or extend your controller and make a method there (I do it this way). – Kyslik Jan 14 '14 at 10:56
-
@Kyslik can I have an example please? – user2936213 Jan 14 '14 at 10:58
2 Answers
12
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
}
}
2
write the podetails() as a function within a helper file.
then load that helper in both of the controllers.
in the controller you just call podetails()
Suppose:
--controller 1--
function podetails()
{
podetails(); // will call function in helper ;
}
--controller 2--
function podetails()
{
podetails(); // will call function in helper ;
}

ReNiSh AR
- 2,782
- 2
- 30
- 42