How to Load a Controller from Another Controller in Codeigniter

How to load a controller from another controller in codeigniter?

you cannot call a controller method from another controller directly

my solution is to use inheritances and extend your controller from the library controller

class Controller1 extends CI_Controller {

public function index() {
// some codes here
}

public function methodA(){
// code here
}
}

in your controller we call it Mycontoller it will extends Controller1

include_once (dirname(__FILE__) . "/controller1.php");

class Mycontroller extends Controller1 {

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

public function methodB(){
// codes....
}
}

and you can call methodA from mycontroller

http://example.com/mycontroller/methodA

http://example.com/mycontroller/methodB

this solution worked for me

How to call a controller from another controller in codeigniter?

No You cant do it.

What you have to do it is create that function in model and call it through your controllers. So it will work fine.

Ex

In Model

function get_id()
{
//some argument
}

In controller 1

$this->Model_name->get_id()

In controller 2

$this->Model_name->get_id()

How to call one controller function in another controller in codeigniter

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
}

}

Call controller within another controller - CodeIgniter

Then, just redirect the page. Else if you want to just call the function, call it via AJAX.

It depends what you exactly want to do. If you want to just invoke the function, its not the right way. Controller as it defines itself controls the flow of the pages that comes on sequence. Controller is responsible to send commands to its associated view to change the view's presentation of the model.

So, if you are saying you want to call controller within another controller, that should mean you are about to redirect to another page.

Updated answer:

Just assume you have new_function on maincontroller that calls the function from othercontroller. The function does not need to be defined on othercontroller.
Add the following line on routes.php.

$routes['maincontroller/new_function'] = 'othercontroller/new_function';

Now, you can call the function of othercontroller as maincontroller/new_function.

Calling a Controller function in another Controller in CodeIgniter

Why not extend the controllers so the login method is within a MY controller (within the core folder of your application) and all your other controllers extend this. For example you could have:

class MY_Controller extends CI_Controller {
public function is_logged()
{
//Your code here
}
}

and your main controllers could then extend this as follows:

class Home_Controller extends MY_Controller {
public function show_home()
{
if (!$this->is_logged()) {
return false;
}
}
}

For further information visit: Creating Core System Classes

New link is here:
https://www.codeigniter.com/user_guide/general/core_classes.html?highlight=core%20classes

Codeigniter : calling a method of one controller from other

This is not supported behavior of the MVC System. If you want to execute an action of another controller you just redirect the user to the page you want (i.e. the controller function that consumes the url).

If you want common functionality, you should build a library to be used in the two different controllers.

I can only assume you want to build up your site a bit modular. (I.e. re-use the output of one controller method in other controller methods.) There's some plugins / extensions for CI that help you build like that. However, the simplest way is to use a library to build up common "controls" (i.e. load the model, render the view into a string). Then you can return that string and pass it along to the other controller's view.

You can load into a string by adding true at the end of the view call:

$string_view = $this->load->view('someview', array('data'=>'stuff'), true);

Get data from another controller in Codeigniter

Try to use the built-in Codeigniter get_vars() public method that allows you to read the value of _ci_cached_vars :

var_dump($aObj->load->get_vars());

Or get_var() if using a key :

var_dump($aObj->load->get_var('data'));

Not able to call another controller method in codeigniter

You can load controller as library in Codeigniter2. But I don't think it is possible in Codeigniter3.

Load Controller as Library :

$this->load->library('../controllers/controller_name');

# Calling Methdod

$this->controller_name->method_name();


Related Topics



Leave a reply



Submit