Home >Backend Development >PHP Tutorial >How Can I Access One CodeIgniter Controller from Another?
Accessing Controllers from Other Controllers in CodeIgniter
In CodeIgniter, it is often desirable to load a controller from another controller without explicitly specifying the controller in the URL. This can help maintain code cleanliness and organization, particularly when you have a library that you want to integrate without cluttering up your main controller.
To achieve this, you can use the following steps:
Load the Controller Library:
In the controller from which you want to load another controller, use the following line of code:
$this->load->library('../controllers/desired_controller');
Access the Controller Function:
After loading the controller library, you can access its functions by calling them as methods of the loaded library object. For example:
$this->desired_controller->function_name();
This approach allows you to keep your controllers clean and separated while still allowing you to access other controller functions when necessary.
The above is the detailed content of How Can I Access One CodeIgniter Controller from Another?. For more information, please follow other related articles on the PHP Chinese website!