1

I need to call a controller say 'faq_view' inside admin controller as the URL structure admin/faq_view like this how I can do this?

e.g:

site.com/maincontroller/function

site.com/maincontroller/othercontroller/function

Cœur
  • 37,241
  • 25
  • 195
  • 267
TED
  • 1,829
  • 5
  • 18
  • 36
  • http://stackoverflow.com/questions/14165895/how-to-load-a-controller-from-another-controller-in-codeigniter – The Alpha Dec 02 '13 at 08:12
  • @SheikhHeera : thanx! I am using HMVC but your link doesn't support my question – TED Dec 02 '13 at 08:30

3 Answers3

4

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.

hsuk
  • 6,770
  • 13
  • 50
  • 80
  • the real purpose of this is I have modules, that should be called under admin/ i.e : admin/faq , admin/user. One way I know using routes can do this. But I really want to call those sub modules through admin/ is it possible? – TED Dec 02 '13 at 08:54
2

You can always call a controller inside another controller, but this only works for calling one controller as far as I have tried. Let's say you are trying to load a controller inside a controller. You can try this:

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

Then do this:

    $this->myothercontroller->function_name();

That's it! You can now access any function inside myothercontroller (controller) in your current controller. I hope this helps too.

warfreak92
  • 324
  • 4
  • 15
  • This can be called only once. If you try to call another controller, you will get an `Undefined property`. – machineaddict Jun 17 '15 at 08:05
  • @machineaddict, does that mean I am doing a bad practice here with my code? If it is, then please tell me how can I do better. Thanks – warfreak92 Jun 18 '15 at 01:21
  • 1
    A controller should not be called in another controller, this is not how MVC works. However, HMVC is a better choise where you can do that. But if you don't want to install HMVC to Codeigniter, this is a shortcut and it's a good one if you use it only once in every controller. – machineaddict Jun 18 '15 at 07:11
1

Your controllers are part of the presentation layer and should not contain application logic. That means you should never need to call a controller from another controller, instead refactor your application and move the domain logic to the model layer.

Now if you have a method that you need in multiple controllers, say for example you need a template method that automatically adds your header and footer views.

If that is the case, create a base class that your controllers extend.

If you are talking about just a routing issue, then just use the routes file for that. I don't like the CI automatic routing and it should be avoided as it will result in duplicate URLs for the same resource.

Patrick
  • 922
  • 11
  • 22