In web development, sometimes you have to use multiple scripts but preferably with same design. I personally prefer CodeIgniter PHP framework for website development. So I end up in situation where I had to integrate third party scripts with CodeIgniter but keeping the same layout and design site wide. Not just design but also the business logic (processing) behind specific sections of design such as header, footer and sidebar.

Foremost, I did search on making calls to controller or view externally and end up having headache as there were no fitting solutions available. Then I started exploring CodeIgniter’s system folder to understand the internal processing on better level.

The solution I found is divided into two parts:

  1. Making calls from outside of CI
  2. Recognising those calls

 

Lets first talk about the second part. To write it simply, I had to pass variable or something which is on global level and I can look up for in CI system to recognise it as external call. So I created constant using define() function. Lets call it: EXT_CALL

Now, to recognise it in CI system, I edited CI_INSTALLATION/system/core/CodeIgniter.php file around line number 357-360 in ‘Call the requested method’ part. Look for:

call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));

And enclose it under IF statement validating EXT_CALL constant. Something like:

if(!defined('EXT_CALL')){ // Checking if EXT_CALL constant is defined

    call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));

}
Now, lets talk about first part, making external calls. It would be better to look at code first than explanation.
define('EXT_CALL', true); // Added EXT_CALL constant to mark external calls

$_GET['controller/method'] = ''; // add pair to $_GET with call route as key



$current = getcwd(); // Save current directory path

chdir('PATH/TO/CI_INSTALLATION'); // change directory to CI_INSTALLATION



require_once 'index.php'; // Add index.php (CI) to script



$CI =& get_instance(); // Get instance of CI



// NOTE: IF DYNAMIC CALLING!!

call_user_func_array(array(&$CI, 'method'), array('controller','method')); // replace controller and method with call route

// OR

// FOR STATIC CALLING!!

$CI->method('controller','method'); // replace controller and method with call route

// eg: $CI->list('welcome','list'); If calling welcome/list route.



$OUT->_display(); // to display output. (quick one)



// Or if you need output in variable,

$output = $CI->load->view('VIEW_NAME',array(),TRUE); // To call any specific view file (bit slow)

// You can pass variables in array. View file will pick those as it works in CI



chdir($current); // Change back to current directory
As I said earlier, first thing done in this part is, defining the constant to recognise external call. CI looks for the route given in $_GET variable as key to make call. So I added a pair in $_GET variable defining the route. Next part is to load CI (including index.php file of CI) and to avoid messing the file system hierarchy of CI, I changed the current directory to CI Installation directory as well as saving the path of third party script in $current variable. Then I created instance of CI root by calling get_instance() function.
Next step is again divided into 2 parts, whether you want to call just view file or you want to execute business logic as well (calling controller).
To call view file:
$output = $CI->load->view('VIEW_NAME',array(),TRUE);
First param should be the name of view file, second is for data variables and third to whether echo or return the output.
And to call the controller:
If you are not sure about the route (Dynamic route)
call_user_func_array(array(&$CI, 'method'), array('controller','method'));
Replace both method occurrences with method name and controller with name of controller.
Or If the route is known and can be hard coded then you can simply call
$CI->method('controller','method');
Again replace both method occurrences and controller as well.
And at last, change current directory back to third party one saved in $current variable.
That’s it, you should be able to call view or controller from outside of CI.
Tech Updates & Freebies

Tech Updates & Freebies

Receive tech updates, product reviews and patches, early wordpress codes and much more!

You have Successfully Subscribed!