![]() |
How do I call a controller within a controller - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: How do I call a controller within a controller (/showthread.php?tid=62933) |
How do I call a controller within a controller - dondo - 09-09-2015 This code works before in CI2: Inside /core/MY_loader.php ------------------------------------ PHP Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Inside /application/controllers/controller.php (this is my controller that wants to call another controller) ------------------------------------------------------------------------ PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Inside /application/controllers/products/sample.php (sample controller that supposedly been called by controller.php) ------------------------------------------------------------------------ PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); RE: How do I call a controller within a controller - includebeer - 09-12-2015 Why do you load a controller within a controller? What are you trying to achieve that CI cannot already do? RE: How do I call a controller within a controller - dondo - 09-13-2015 (09-12-2015, 10:12 AM)includebeer Wrote: Why do you load a controller within a controller? What are you trying to achieve that CI cannot already do? Because I have a dynamic URL. Sample: http://test.com/<username> -> this will go to user profile controller or http://test.com/<storename> -> this will go to store profile controller RE: How do I call a controller within a controller - albertleao - 09-14-2015 (09-13-2015, 09:03 PM)dondo Wrote:(09-12-2015, 10:12 AM)includebeer Wrote: Why do you load a controller within a controller? What are you trying to achieve that CI cannot already do? I would figure out a different way of doing it then. Calling a controller from inside a controller completely defeats the purpose of MVC. I see what you are trying to do. Some sites get around this by making the url have a trailing letter for example "/u/<username>" and /s/<storename>". If you need to use the same code in 2 controllers, that piece of code should be put in a library or a model. RE: How do I call a controller within a controller - mwhitney - 09-14-2015 In this case, I would recommend either setting up a method to determine the routes dynamically or setup one or more _remap() methods to handle the requests. |