CodeIgniter Forums
How can I add outside code in codeigniter? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: How can I add outside code in codeigniter? (/thread-63997.html)



How can I add outside code in codeigniter? - sulee154151 - 01-01-2016

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


RE: How can I add outside code in codeigniter? - Wouter60 - 01-01-2016

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.