CodeIgniter: Load controller within controller
If you're interested, there's a well-established package out there that you can add to your Codeigniter project that will handle this:
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/
Modular Extensions makes the CodeIgniter PHP framework modular. Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory, that can be dropped into other CodeIgniter applications.
OK, so the big change is that now you'd be using a modular structure - but to me this is desirable. I have used CI for about 3 years now, and can't imagine life without Modular Extensions.
Now, here's the part that deals with directly calling controllers for rendering view partials:
// Using a Module as a view partial from within a view is as easy as writing:
<?php echo modules::run('module/controller/method', $param1, $params2); ?>
That's all there is to it. I typically use this for loading little "widgets" like:
- Event calendars
- List of latest news articles
- Newsletter signup forms
- Polls
Typically I build a "widget" controller for each module and use it only for this purpose.
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
CodeIgniter load controller within a controller HMVC
I found a solution at https://github.com/EllisLab/CodeIgniter/pull/1818
There is a real HMVC and it works. I hope that this will be included in upcoming CodeIgniter release.
My test results are shown in the images.
controllers/demo.php
class Demo extends CI_Controller {
public function index()
{
$this->load->controller('welcome');
$this->load->view('demo');
}
}
(source: github.com/EllisLab/CodeIgniter)
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
.
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);
How to load more than one controller in another controller in CodeIgniter
You shouldn't be loading other controllers. Each request should be handled by a single controller. If you require common behaviour you have the following options:
/application/core/MY_Controller.php
and extend that class- Move the behaviour to a model
- Move the behaviour to a library or helper
If you are unfamiliar with the MVC pattern, this forum post might help you. It's from an old thread, but the principles still apply.
Load controller from module hmvc codeigniter
First off lower case for folder names. Only first letter must be upper case for controller names and models etc UCFIRST as explained here http://www.codeigniter.com/user_guide/general/styleguide.html#file-naming HMVC wont pick up CI_Controllers controllers only MX_Controllers
class Core_test_controller extends MX_controller {...}
class Insertcontroller extends MX_Controller {...}
As said here
<?php
/** module and controller names are different, you must include the method name also, including 'index' **/
modules::run('module/controller/method', $params, $...);
/** module and controller names are the same but the method is not 'index' **/
modules::run('module/method', $params, $...);
/** module and controller names are the same and the method is 'index' **/
modules::run('module', $params, $...);
/** Parameters are optional, You may pass any number of parameters. **/
Related Topics
Outputting All PHP Errors to Database Not Error_Log
Get All Instances of a Class in PHP
Separate Space-Delimited Words in a String
Detecting Image Type from Base64 String in PHP
Any Way to Specify Optional Parameter Values in PHP
Is My Understanding of PHP Sessions Correct
Access Denied for User 'Root'@'Localhost' with PHPmyadmin
Does PHP Execution Stop After a User Leaves the Page
Is It Ever Ok to Store Password in Plain Text in a PHP Variable or PHP Constant
Is It Secure to Store a Password in a Session
Can PHP and ASP.NET Run Together Within the Same Web Site in Iis 7.5
Extracting All Values Between Curly Braces Regex PHP
How to Find a Whole Word in a String in PHP Without Accidental Matches