0

So I must be having a brain fart and need a hand. I'm trying to use BoxPacker in my Codeigniter project to figure out how many items I can fit in some boxes. I've installed BoxPacker into a "application/third_party/boxpacker" folder. But now how do I actually use it?

For some reason my brain is telling me I have to make my own library to interface with the third party programming but then I just have another brain fart and have no clue how to implement it. It's been a long week so I'm pretty burnt out and looking for a hand.

EDIT: So, I created a library named BoxPacker.php with the following code: `

class BoxPacker
{
    function __construct()
    {
        require_once APPPATH."third_party/boxpacker/vendor/autoload.php";
    }
}

In my controller I then call:

$this->load->library('BoxPacker'); $packer = new BoxPacker();

But when I try using the functions in the third part code like below, I get the following error Exception: Call to undefined method BoxPacker::addBox():

$packer->addBox(new TestBox('Le petite box', 300, 300, 10, 10, 296, 296, 8, 1000));

Tyler Fontaine
  • 131
  • 2
  • 12
  • Possible duplicate of [CodeIgniter Third party class not loading](https://stackoverflow.com/questions/21016626/codeigniter-third-party-class-not-loading) – Francisco Hahn Aug 29 '18 at 19:38
  • Did you generate an autoload file? – Alex Aug 29 '18 at 20:02
  • @Alex Yes, when it was installed with composer it created the autoload file which I have my library pointing too. – Tyler Fontaine Aug 29 '18 at 20:04
  • I'm going to be honest I've never tried to load a namespaced library that way in CI. Although it should work, you can just forgo the autoload.php and try my answer here: https://stackoverflow.com/questions/49461123/how-do-i-include-the-oop-defiant-randomdotorg-library-in-codeigniter/49461228#49461228 – Alex Aug 29 '18 at 20:07
  • @Alex I actually tried that earlier but if you download the stuff from the BoxPacker link in the original post and try it, there ends up being a ton of errors. Some code is in a "dvdoug" folder while some is in a "psr" folder and if you move stuff around it gets fussy. – Tyler Fontaine Aug 29 '18 at 20:18

1 Answers1

0

Besides installing the third party library in your application/third_party directory, you must create a new library in application/libraries that requires the third party code and extends it like this:

Application/libraries/Yourlib.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    // include the 3rd party class
    require_once APPPATH."/third_party/class_name/filename.php";

    // Extend the class so you inherit all functions, etc.
    class NewClass extends ThirdPartyClass {
        public function __construct() {
            parent::__construct();
        }
        // library functions
        public function something(){
            ...code...
       }

Then, on your controller you must load the new library with:

$this->load->library('yourlib');

After doing this, you may $this->yourlib as normal

Javier Larroulet
  • 3,047
  • 3
  • 13
  • 30
  • Since it was installed with Composer I have my required path set to the autoload file that was generated. But if I try extending the class I get an error saying that the class is not found. – Tyler Fontaine Aug 29 '18 at 20:06