0

I load a composer library suited for CodeIgniter called SteeveDroz\Asset, that I can access without problem with $asset = new SteeveDroz\Asset.

I would like to be able to load it with CodeIgniter $this->load->library('SteeveDroz\Asset'), but I get the error message

Unable to load requested class: SteeveDroz\Asset

Is it possible to achieve what I want? If yes, how?

SteeveDroz
  • 6,006
  • 6
  • 33
  • 65
  • if the current answer doesn't help, you can try my answer here: https://stackoverflow.com/questions/49461123/how-do-i-include-the-oop-defiant-randomdotorg-library-in-codeigniter/49461228#49461228 just upvote it if it works! – Alex Jun 12 '19 at 07:52

2 Answers2

1

As mentionned Alex in his comment, it is required to make an adapter library. I created an all purpose class for that:

application/libraries/ComposerAdapter.php

class ComposerAdapter
{
    private $object;

    public function __construct($object)
    {
        $this->object = $object;
    }

    public function __call($method, $args)
    {
        return call_user_func_array([$this->object, $method], $args);
    }
}

application/libraries/Asset.php

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

require('ComposerAdapter.php');

class Asset extends ComposerAdapter
{
    public function __construct()
    {
        parent::__construct(new SteeveDroz\Asset());
    }
}

application/config/autoload.php

// ...
$autoload['libraries'] = array('asset');
// ...
SteeveDroz
  • 6,006
  • 6
  • 33
  • 65
0

if you are using CodeIgniter 3 you can modify application/config/config.php and set

$config['composer_autoload'] = TRUE

or

$config['composer_autoload'] = FCPATH .'vendor/autoload.php';

this will automatically load all your composer dependencies.

Krunal Shah
  • 3
  • 1
  • 5
  • I already did that (that's why the part with the `new` works), but I still can't use `$this->steevedroz\asset->something()`. – SteeveDroz Jun 12 '19 at 07:52
  • calling it that would never work, you'd have to make an adapter library (see this smarty one for reference: https://github.com/Vheissu/Ci-Smarty/blob/master/libraries/MY_Parser.php) to interface CI and that code which is completely unnecessary. just init it like you would any other php class `new class();` – Alex Jun 12 '19 at 08:01