How do I call a controller within a controller |
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');
Why do you load a controller within a controller? What are you trying to achieve that CI cannot already do?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/ (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
(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.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
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.
|
Welcome Guest, Not a member yet? Register Sign In |