CodeIgniter Forums
Load a controller into another controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Load a controller into another controller (/showthread.php?tid=74176)



Load a controller into another controller - Mekaboo - 08-12-2019

Hello all!

I have my Users.php controller that operates login/register and it works perfectly but I recently purchased a FB style script which has wall post, chat, and friends portion that I want to implement into my site. This script has its own controllers/models of course but I how do I add these within my Users.php so that everything connects and all the tables within my DB work together? 



Heart Heart ,
Mekaboo


RE: Load a controller into another controller - Wouter60 - 08-13-2019

If you have a controller for login, I'm sure you are using a session variable to "remember" if a user is logged in. Or a library that is active accross all controllers to check if a user is logged in.
Your FB like controller also needs to check this. If the user is not logged in, store the current url in a session variable, redirect to the login page, and after a succesful login, redirect to the url that was stored in the session.

I have this concept in MY_Controller.php (in application/core).
PHP Code:
protected function force_login()

 if (! 
$this->ion_auth->logged_in() ) {
 
$this->session->set_userdata('redirect_after_login',current_url());
 
redirect('/login');
 exit;
 } 



In every controller method that is only accessible for logged in users, I have this line:
PHP Code:
$this->force_login(); 

If the user is already logged in, the application will simply continue with what it was doing. Otherwise, the user is routed to the login page, and after logging in, the application continues where it was.

I hope this will help you.


RE: Load a controller into another controller - albertleao - 08-13-2019

Loading a controller from another controller breaks the MVC pattern and generally isn't the best practice. Controllers are really just supposed to pick which data you're displaying from your back end and sending it to your frontend.

I'd recommend writing a few services or libraries and moving all that facebook code in there. Then when you have that, call the library/service from your controller. This makes it so that you can pull that library from anywhere fairly easily, and makes it so that you can unit test each library and mock out your api calls.


RE: Load a controller into another controller - Mekaboo - 08-13-2019

(08-13-2019, 08:04 AM)Wouter60 Wrote: If you have a controller for login, I'm sure you are using a session variable to "remember" if a user is logged in. Or a library that is active accross all controllers to check if a user is logged in.
Your FB like controller also needs to check this. If the user is not logged in, store the current url in a session variable, redirect to the login page, and after a succesful login, redirect to the url that was stored in the session.

I have this concept in MY_Controller.php (in application/core).
PHP Code:
protected function force_login()

 if (! 
$this->ion_auth->logged_in() ) {
 
$this->session->set_userdata('redirect_after_login',current_url());
 
redirect('/login');
 exit;
 } 



In every controller method that is only accessible for logged in users, I have this line:
PHP Code:
$this->force_login(); 

If the user is already logged in, the application will simply continue with what it was doing. Otherwise, the user is routed to the login page, and after logging in, the application continues where it was.

I hope this will help you.

Thank ya this helps alot! My main goal is when a person logs in certain components work after logging in. Say for instance when a person logins into FB..they are able to post on their wall, DM, etc. It mostly about the members who are in the DB are connected and interact with one another and I feel that will be possible if the smaller controllers were connected to the Users controller. Heart


RE: Load a controller into another controller - Mekaboo - 08-13-2019

(08-13-2019, 08:24 AM)albertleao Wrote: Loading a controller from another controller breaks the MVC pattern and generally isn't the best practice. Controllers are really just supposed to pick which data you're displaying from your back end and sending it to your frontend.

I'd recommend writing a few services or libraries and moving all that facebook code in there. Then when you have that, call the library/service from your controller. This makes it so that you can pull that library from anywhere fairly easily, and makes it so that you can unit test each library and mock out your api calls.

I seen other sites talk about adding libraries but I wanted to come here and get more insight. I think doing the service/library route in the best option! Heart


RE: Load a controller into another controller - albertleao - 08-13-2019

(08-13-2019, 11:23 AM)Mekaboo Wrote: I seen other sites talk about adding libraries but I wanted to come here and get more insight. I think doing the service/library route in the best option! Heart


Just remember, if your controllers are becoming "fat" (have lots of code in them), you're probably doing something wrong.