Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter: Load controller within controller

I have a home controller with an index action that displays a set of featured products. However, the products are managed through a product controller including a proprietary model and views.

How do I access product information from within the index action in the home controller? Instancing product won't work as the class isn't loaded at runtime and CodeIgniter doesn't provide a way to dynamically load controllers. Putting the product class into a library file doesn't really work, either.

To be precise, I need the product views (filled with data processed by the product controller) inserted in the index view. I'm running CodeIgniter 2.0.2.

like image 413
Daniel Avatar asked May 22 '11 22:05

Daniel


People also ask

Can we call one controller from another controller in CodeIgniter?

Codeigniter (and MVC in general) aren't designed to work this way. Controllers don't call other controllers. If you find yourself needing to do that it's likely an indicator that your app architecture needs some refactoring.

How can we call one controller function from another controller in MVC PHP?

Show activity on this post. Create new Helper (e.g PermissionHelper. php ) then move the funtion to it and call it where you want using : PermissionHelper::permission();

What is default controller in CodeIgniter?

Defining a Default ControllerCodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes. php file and set this variable: $route['default_controller'] = ' Blog ';

What is the use of constructor in CodeIgniter?

Constructor is a function that is automatically called when instantiated. this function helps us to intialize the things that we are going to need frequently in our code like when we have to load the models of helpers like form e.t.c.


2 Answers

Load it like this

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

and call the following method:

$this->instructor->functioname() 

This works for CodeIgniter 2.x.

like image 180
Zulqurnain abbas Avatar answered Sep 18 '22 15:09

Zulqurnain abbas


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.

like image 34
Wesley Murch Avatar answered Sep 19 '22 15:09

Wesley Murch