Welcome Guest, Not a member yet? Register   Sign In
Include external library
#1

[eluser]ephlodur[/eluser]
Hello all,
I would like to include PHPRtfLite in one of my project.
I was wondering what is the best way to include that external lib
in the CI Framework.

Thanks for your help.
#2

[eluser]heenji[/eluser]
you could try this steps:

copy phprtflite's lib content to CI application/librarys/PHPRtfLite,
create a PHPRTFLITE.php in application/librarys,it content like following:
Code:
<?php
if (!defined('BASEPATH')) {exit('No direct script access allowed');}
class CI_PHPRTFLITE
{
/**
* Constructor
*
* @param string $class class name
*/
function __construct($class = NULL)
{

ini_set('include_path',
ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries/PHPRtfLite');

if ($class)
{
require_once (string) $class . EXT;
log_message('debug', "PHPRtfLite Class $class Loaded");
}
else
{
log_message('debug', "PHPRtfLite Class Initialized");
}
}

/**
* PHPRtfLite Class Loader
*
* @param string $class class name
*/
function load($class)
{
require_once (string) $class . EXT;
log_message('debug', "PHPRtfLite Class $class Loaded");
}
}
?>

make a controller and method like this:
Code:
public function testrtf(){
  $this->load->library('phprtflite');
        $this->phprtflite->load('PHPRtfLite/PHPRtfLite');

  PHPRtfLite::registerAutoloader();
  $rtf = new PHPRtfLite();
  $sect = $rtf->addSection();
  $sect->writeText('<i>hello<b>world</b></i>.', new PHPRtfLite_Font(12), new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_CENTER));

  // save rtf document
  $rtf->save('hello.rtf');
  echo 'create ok,and download <a href="'.base_url().'hello.rtf">here</a>';
  
}

#3

[eluser]CroNiX[/eluser]
Only CORE CI classes get prefixed with CI_, not your own libraries or controllers...
#4

[eluser]ephlodur[/eluser]
thank you very much for your response..
so far I get
Code:
An Error Was Encountered
Unable to load the requested class: phprtflite
I have change the CI_PHPRTFLITE to MY_PHPRTFLITE
I guess I need to adjust the path. For now this is my configuration
I have downloaded the PHPrtfLite and unzip the content to application/libraries
that mean the path to the lib directory is application/libraries/PHPRtfLite/lib.

I'm not sure to what level the call in the controller should be pointing
Code:
$this->load->library('phprtflite');
$this->phprtflite->load('PHPRtfLite/PHPRtfLite');

Thanks a lot for your help
#5

[eluser]Mauricio de Abreu Antunes[/eluser]
If your file is in libraries/ you really dont need another config.
Just load like this:
Code:
$this->load->library('your_lib');
#6

[eluser]CroNiX[/eluser]
You also shouldn't use MY_ as a prefix unless you are extending a CI Core class.
http://ellislab.com/codeigniter/user-gui...asses.html
#7

[eluser]Mauricio de Abreu Antunes[/eluser]
^
Agreed!
Classes with MY as prefix extend Controller or Model class.

Code:
class MY_Model extends CI_Model {};
class MY_Controller extends CI_Controller {};
#8

[eluser]ephlodur[/eluser]
Thanks to all for your reply
I have change the class name and the file name as below:
file name PHPRtfLite.cpp
Code:
class PHPRtfLite
{
}
in my controller now I used
Code:
$this->load->library('phprtflite');
I still get the same error... have any of you use the lib "PHPRtfLite" before in your project.
#9

[eluser]ephlodur[/eluser]
Hello heenji and all,
Thanks a lot for your help.
I have manage to load the class with minor changes to the file provided by heenji.
The content of the "lib" directory for PHPRtfLite is located in application/libraries/PHPRtfLite
the file below "PHPRtfLite.php" is located in the directory application/libraries.
Code:
&lt;?php
if (!defined('BASEPATH')) {exit('No direct script access allowed');}
class PHPRtfLite
{
        /**
        * Constructor
        *
        * @param string $class class name
        */
        $rtf = NULL;
        function __construct()
        {


        $dir = dirname(__FILE__);
        require_once $dir . '/PHPRtfLite/PHPRtfLite.php';

        ini_set('include_path',
        ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries/PHPRtfLite/');

                if ($rtf)
                {
                require_once (string) $rtf . EXT;
                log_message('debug', "PHPRtfLite Class $rtf Loaded");
                }
                else
                {
                log_message('debug', "PHPRtfLite Class Initialized");
                }
        }

        /**
        * PHPRtfLite Class Loader
        *
        * @param string $class class name
        */
        function load($rtf)
        {
                require_once (string) $rtf . EXT;
                log_message('debug', "PHPRtfLite Class $rtf Loaded");
        }
}
?&gt;
and in the controller the code below was used to test that it was working
Code:
public function testrtf()
        {

         $this->load->library('PHPRtfLite/PHPRtfLite');
         PHPRtfLite::registerAutoloader();
         $rtf = new PHPRtfLite();
         $sect = $rtf->addSection();
         $sect->writeText('<i>hello<b>world</b></i>.', new PHPRtfLite_Font(12), new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_CENTER));

         // save rtf document
         $rtf->save('hello.rtf');
         echo 'create ok,and download <a href="'.base_url().'hello.rtf">here</a>';

        }




Theme © iAndrew 2016 - Forum software by © MyBB