简体   繁体   中英

php composer autoload class not found error

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

//Include Composer's autoloader
include 'vendor/autoload.php';

public function test_auth() {       
try{
      $hybridauth = new Hybridauth\Hybridauth($config);

        //Attempt to authenticate users with a provider by name
        $adapter = $hybridauth->authenticate('Twitter'); 

        //Returns a boolean of whether the user is connected with Twitter
        $isConnected = $adapter->isConnected();

        //Retrieve the user's profile
        $userProfile = $adapter->getUserProfile();

        //Inspect profile's public attributes
        var_dump($userProfile);

        //Disconnect the adapter 
        $adapter->disconnect();
    }
    catch(\Exception $e){
        echo 'Oops, we ran into an issue! ' . $e->getMessage();
    }
}

An uncaught Exception was encountered

Type: Error

Message: Class 'Hybridauth\\Hybridauth\\Hybridauth' not found

Filename: C:\\xampp\\htdocs\\paymatrix_v2\\application\\controllers\\Hauth.php

Line Number: 35

Backtrace:

File: C:\\xampp\\htdocs\\paymatrix_v2\\index.php Line: 294 Function: require_once

composer.json file

{
"description": "The CodeIgniter framework",
"name": "codeigniter/framework",
"type": "project",
"homepage": "https://codeigniter.com",
"license": "MIT",
"support": {
    "forum": "http://forum.codeigniter.com/",
    "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
    "irc": "irc://irc.freenode.net/codeigniter",
    "source": "https://github.com/bcit-ci/CodeIgniter"
},
"require": {
    "php": ">=5.2.4",
    "mailgun/mailgun-php": "^2.1",
    "php-http/curl-client": "^1.6",
    "guzzlehttp/psr7": "^1.3",
    "aws/aws-sdk-php": "3.*",
    "pipl/piplapis-php" : "^5.0",
    "hybridauth/hybridauth": "^2.9"
},
"require-dev": {
    "mikey179/vfsStream": "1.1.*",
    "aws/aws-sdk-php": "dev-master"
},
"autoload": {
        "classmap": ["vendor/pipl/piplapis-php/src","vendor/pipl/"]
}

}

autoload.php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit9da23362304113093d59b5cbcc0e2b35::getLoader();

hybrid auth location

vendor/hybridauth/hybridauth/

This file already included in codeigniter core file. dont need to incluse again in your class file

include 'vendor/autoload.php';  

Within your function

public function test_auth() {
  // Before code
    $config = [
        'callback' => 'https://example.com/path/to/script.php',
        'keys' => [ 'key' => 'your-twitter-consumer-key', 'secret' => 'your-twitter-consumer-secret' ]
    ];

    try {
        $twitter = new Hybridauth\Provider\Twitter($config);

        $twitter->authenticate();

        $accessToken = $twitter->getAccessToken();
        $userProfile = $twitter->getUserProfile();
        $apiResponse = $twitter->apiRequest( 'statuses/home_timeline.json' );
    }
    catch(\Exception $e){
        echo 'Oops, we ran into an issue! ' . $e->getMessage();
    }

        }

Check the usage from the package README

https://github.com/hybridauth/hybridauth

I have also faced the same issue. When I install a package using the composer on the root location then it was still saying that the third party class not found. So here is my solution. First of all, I change the "composer_autoload" config to TRUE.

$config['composer_autoload'] = TRUE;

By default after change the above configuration Codeigniter looking vendor/autoload file under APPPATH(application folder) which is incorrect. So I change the constant to FCPATH(root path) because the root is the correct path to install the third-party packages using the composer.

change from System/core/Codeigniter.php:165

if ($composer_autoload === TRUE)
{
    file_exists(APPPATH.'vendor/autoload.php')
        ? require_once(APPPATH.'vendor/autoload.php')
        : log_message('error', '$config[\'composer_autoload\'] is set to TRUE but '.APPPATH.'vendor/autoload.php was not found.');
}

to

if ($composer_autoload === TRUE)
{
    file_exists(FCPATH.'vendor/autoload.php')
        ? require_once(FCPATH.'vendor/autoload.php')
        : log_message('error', '$config[\'composer_autoload\'] is set to TRUE but '.FCPATH.'vendor/autoload.php was not found.');
}

and finally, run

composer dumpautoload

in case if still not working. Happy coding!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM