CodeIgniter Forums

Full Version: How to use external php library in codeigniter?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I'm currently creating a project where users can compare two text files (html styled or not), see the differences between those two and then request for embeding those changes in the main file (something like GitHub but without all this fuss of using commands to push changes).

What i found is this library:


I placed the "Caxy" folder inside "libraries" folder of my project and what i get after autoloading the library is:


PHP Code:
$autoload['libraries'] = array(

View file
Fatal error: Class 'Caxy\HtmlDiff\AbstractDiff' not found in "path/path/path/......" on line 10

When on the other hand, i try to use it like this:
PHP Code:
('BASEPATH') OR exit('No direct script access allowed');


Files extends CI_Controller {
   public function 


 i get this
PHP Code:
An uncaught Exception was encountered

: Class 'Caxy\HtmlDiff\HtmlDiff' not found

: ...\application\controllers\Files.php 

After searching online i didn't find any solid answer on this issue..

Any ideas or suggestions? 

The problem is that CI can find the 'caxy/htmldiff/htmldiff' file, but cannot find any of the files it needs to load.

My suggestion is to install caxy/php-htmldiff using Composer as described on their Readme page.

Then in the file /application/config.config.php use the following setting in the Composer auto-loading section

PHP Code:
$config['composer_autoload'] = FCPATH.'vendor/autoload.php'

In the controller that will load and use htmldiff put the following immediately after the opening PHP tag, e.g.

PHP Code:
use Caxy\HtmlDiff\HtmlDiff

Do not use
PHP Code:
$autoload['libraries'] = array( 'caxy/htmldiff/htmldiff'); 

You should not use $this->load->library(). either. Instead, when you want to instantiate HtmlDiff use "new". Below I create a class property to hold the HtmlDiff object when you create one. In this example I do so in the compare method

PHP Code:
use Caxy\HtmlDiff\HtmlDiff;

defined('BASEPATH') OR exit('No direct script access allowed');

Files extends CI_Controller
    protected $htmldiff;

    public function compare($oldHtml$newHtml)
        $this->htmldiff = new HtmlDiff($oldHtml$newHtml);
        //do your thing

If you want to use a HtmlDiffConfig Object include the following line right after the first "use: statement

PHP Code:
use Caxy\HtmlDiff\HtmlDiffConfig
Since i don't know how to user composer yet, i think i should start learning it in order to complete this! Tongue

Thanks for you help! Smile