CodeIgniter Forums

Full Version: How can I add outside code in codeigniter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, I have an outside code or script, for example, including functions, classes inside even connecting database. Is there any way to add this code into codeigniter as class libraries or customized codes? Please explain more details if yes. Thanks much
It's possible to put use your existing code as a library in CodeIgniter. A library in CI is basically a class with one or more methods (functions) inside it.
You library goes into the application/libraries folder, and the file name must begin with a capital letter. E.g. Mylibrary.php

In order to use the library, load it from your controller:
PHP Code:
$this->load->library('mylibrary'); 

If your library contains a method named "display_page", call it like this:
PHP Code:
$this->mylibrary->display_page(); 

If your existing code is not in a class, but in procedural style, you can use it as a helper in CodeIgniter. Put the code in a file in the application/helpers folder. In order to access the functions inside the helper, call it like this:
PHP Code:
$this->load->helper('helperfile'); 
Now, the functions are available.