0

I am trying to move from codeigniter 2 to codeigniter 3. Normally in most of our functions in our controller we call other controller sometimes like this,

 $this->load->controller('login');
 return $this->login->index();

But in codeigniter 3 this crashes, I checked how routes worked in this stack overflow question.

How to load a controller from another controller in codeigniter?

But the above url does not work for me, since changing routes will cancel all the database trasactions I use with mysql gets cancelled.

Is there a good solution to load different controller, I thought of moving it to a library and calling it like that but I think there is a better way. Also the HMVC might be a solution but its still doesn't solve the issue for a simple purpose.

mr aurora
  • 169
  • 1
  • 2
  • 11
  • If you need to call controllers with in controllers normal MVC does not work you need to add third party https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc –  Oct 12 '17 at 12:01
  • This is a old tutorial but a good one to start one https://www.youtube.com/watch?v=8fy8E_C5_qQ&list=PLBEpR3pmwCawDZ6FgNYoyvicEz4HrJPec the tutorial has step by step guides note though some things have changed since the tutorial was made like the class and file naming method etc in CI 3 > –  Oct 12 '17 at 12:02

2 Answers2

0

According to the link you gave you cannot do that - I am not aware of the ability to $this->load->controller in CI3.

Your better option is to create a MY_Controller and extend your controllers from that and then place shared controller functions within that.

Antony
  • 3,875
  • 30
  • 32
  • The link I gave was to show that routes might been an option, but it clearly isn't. Your solution does work but moving around 15000 lines of code is a very intensive task, there might be a easier solution to this. Hence the question. – mr aurora Oct 12 '17 at 11:15
  • In my own experience I have created myself a login library (noting the name of your controller) which I then pull into MY_Controller. IMHO a login controller should probably be a library - it will be more helpful in future projects as well. – Antony Oct 12 '17 at 11:19
  • Exactly! Thats one of the conclusions I came to as well. But I posted the question here thinking if there is someone with a better answer it would be very helpful :') – mr aurora Oct 12 '17 at 11:20
0

I think you are looking for HMVC solution. Check This Out

Update : By implementing HMVC, you can load any of your controller as a module like following

function abc(){
  $this->load->module("my_auth");
  $this->my_auth->check_login();
}
Waqas Yousaf
  • 64
  • 1
  • 3
  • 10