Saturday, January 29, 2022

[FIXED] How to load a library from composer?

Issue

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?


Solution

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');
// ...


Answered By - SteeveDroz

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.