• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use external php library in codeigniter?

#1
Hey! 

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:

Caxy/HtmlDiff/HtmlDiff

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

Autoload.php

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


View file
Code:
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:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

use 
Caxy\HtmlDiff\HtmlDiff;

class 
Files extends CI_Controller {
    
   public function 
index(){

   }


 i get this
PHP Code:
An uncaught Exception was encountered
Type
Error

Message
: Class 'Caxy\HtmlDiff\HtmlDiff' not found

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

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

Any ideas or suggestions? 

Thanks!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#2
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:
<?php
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:
<?php
use Caxy\HtmlDiff\HtmlDiff;

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

class 
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
Reply

#3
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

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.