phpcodeigniteremojione

class client not found emojione when using on codenigter


Hello Experts I am in a big problem I am using emojione library to convert short name to image the emojis works fine when using on code php but i want this to use with codegniter i decided to created a model so i can easily access on al over for that I created a function inside my EmojiModel.php

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

class EmojiModel extends CI_Model {
    public function __construct() {
         parent::__construct();
         require APPPATH . 'third_party/emojione/LoadEmojione.php';
    }

    public function LoadImageEmoji($mojiText) {
        $emoji = new \EmojiLoad();
        return $emoji->LoadImageEmoji($mojiText);
    }
}

inside third party library I hade added the library and created a file named LoadEmoji.php and simply included that file inside model constructor

application\third_party\emojione\LoadEmojione.php

namespace Emojione;
require 'vendor/autoload.php';

class EmojiLoad {
    public function LoadImageEmoji($textEmoji) {
        $client = new Client(new Ruleset());
        $client->imagePathPNG = 'https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/png/'; 
        return $client->toImage($textEmoji);
    }
}

The error I am getting is

An uncaught Exception was encountered Type: Error

Message: Class "EmojiLoad" not found

I am not sure what should I do because it is important in this library to use namespace i am confused what should I do here


Solution

  • Change this line $emoji = new \EmojiLoad(); to $emoji = new \Emojione\EmojiLoad();. Your EmojiLoad class is in the Emojione namespace, so you need to add the namespace before the classname when using it.


    Alternatively, as shirshak007 suggested, if you're only using your custom EmojiLoad class in the EmojiModel class, you can also use the Emoijone library directly in the model.

    In that case I would also suggest using CodeIgniter's composer_autoload function:

    In application/config/config.php, set $config['composer_autoload'] to TRUE.

    This expects the composer.json file to live in the application folder, so copy it there from the application/third_party/emojione folder and then delete the application/third_party/emojione folder. If you then do composer install from the application folder, it'll create a new vendor folder inside the application folder.

    Then use the Emojione library directly in the model:

    <?php
    
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    use Emojione\Client;
    use Emojione\Ruleset;
    
    class EmojiModel extends CI_Model {
        public function __construct() {
             parent::__construct();
        }
    
        public function LoadImageEmoji($mojiText) {
            $client = new Client(new Ruleset());
            $client->imagePathPNG = 'https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/png/'; 
            return $client->toImage($mojiText);
        }
    }