douzhan8652 2018-04-14 19:58
浏览 266
已采纳

无法使用composer vendor作为codeigniter库

I am trying to use this (https://github.com/Achse/geth-jsonrpc-php-client) library using composer and codeigniter. But I am getting below error:

Type: Error

Message: Class 'GuzzleClient' not found

Filename: /var/www/html/test/application/libraries/Ethereum.php

Line Number: 7

And below is my library code:

<?php

class Ethereum
{
    public function __construct()
    {
        $httpClient = new GuzzleClient(new GuzzleClientFactory(), 'localhost', 8545);
        $client     = new Client($httpClient);

        return $client;
    }

}

Composer is wel loaded as other dependancies are working fine. Not able to find out the issue. plz help

  • 写回答

1条回答 默认 最新

  • dongwen7371 2018-04-14 20:19
    关注

    When using Composer you typically have to include an autoload.php file. You also need to used namespaces for the Composer dependencies.

    Usually the vendor directory is on the same level as application so it's handy to define a constant to point to the vendor directory.

    define('VENDOR', substr(FCPATH, 0, strpos(APPPATH, 'application/')) . "vendor/");
    

    Your library maybe needs to look more like this.

    <?php
    require VENDOR . 'autoload.php';
    
    //I'm not certain this is the correct namespace but hopefully you get the idea
    use Achse\GethJsonRpcPhpClient; 
    
    class Ethereum
    {
        protected $httpClient;
        protected $client;
    
        public function __construct()
        {
            $this->httpClient = new GuzzleClient(new GuzzleClientFactory(), 'localhost', 8545);
            $this->client     = new Client($httpClient);
        }
    
        //example using the class property $client
        public getResult()
        {
            return $this->client->callMethod('eth_getBalance', ['0xf99ce9c17d0b4f5dfcf663b16c95b96fd47fc8ba', 'latest']);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?