Search code examples
codeignitercodeigniter-2

Call controller within another controller - CodeIgniter


I need to call a controller say 'faq_view' inside admin controller as the URL structure admin/faq_view like this how I can do this?

e.g:

site.com/maincontroller/function

site.com/maincontroller/othercontroller/function


Solution

  • Then, just redirect the page. Else if you want to just call the function, call it via AJAX.

    It depends what you exactly want to do. If you want to just invoke the function, its not the right way. Controller as it defines itself controls the flow of the pages that comes on sequence. Controller is responsible to send commands to its associated view to change the view's presentation of the model.

    So, if you are saying you want to call controller within another controller, that should mean you are about to redirect to another page.

    Updated answer:

    Just assume you have new_function on maincontroller that calls the function from othercontroller. The function does not need to be defined on othercontroller. Add the following line on routes.php.

    $routes['maincontroller/new_function'] = 'othercontroller/new_function';
    

    Now, you can call the function of othercontroller as maincontroller/new_function.