【问题标题】:php composer autoload class not found errorphp composer自动加载类未找到错误
【发布时间】:2019-11-18 19:31:46
【问题描述】:
<?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();
    }
}

遇到未捕获的异常

类型:错误

消息:找不到类“Hybridauth\Hybridauth\Hybridauth”

文件名:C:\xampp\htdocs\paymatrix_v2\application\controllers\Hauth.php

行号:35

回溯:

文件:C:\xampp\htdocs\paymatrix_v2\index.php 线路:294 函数:require_once

composer.json 文件

{
"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/"]
}

}

自动加载.php

// autoload.php @generated by Composer

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

return ComposerAutoloaderInit9da23362304113093d59b5cbcc0e2b35::getLoader();

混合身份验证位置

vendor/hybridauth/hybridauth/

【问题讨论】:

  • 请显示您的自动加载文件以及 Hybridauth 的位置。
  • hybridauth 位于 vendor 文件夹中,自动加载文件包含以下代码:&lt;?php // autoload.php @generated by Composer require_once __DIR__ . '/composer/autoload_real.php'; return ComposerAutoloaderInit9da23362304113093d59b5cbcc0e2b35::getLoader();
  • autoload.json,不是autoload.php,你应该给我hybridauth的完整路径。
  • 自动加载文件名为 autoload.php 它在 vendor 文件夹中
  • 尝试运行composer dump-autoload

标签: php composer-php hybridauth


【解决方案1】:

此文件已包含在 codeigniter 核心文件中。不需要在你的类文件中再次包含

include 'vendor/autoload.php';  

在你的职能范围内

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();
    }

        }

从包 README 中检查使用情况

https://github.com/hybridauth/hybridauth

【讨论】:

  • 尝试了所有可能的方法。问题不在于混合身份验证,而在于作曲家本身。它无法在我的供应商文件夹中找到类。这很可能是自动加载问题
【解决方案2】:

我也遇到了同样的问题。当我在根位置使用作曲家安装包时,它仍然说找不到第三方类。 所以这是我的解决方案。 首先,我将“composer_autoload”配置更改为 TRUE。

$config['composer_autoload'] = TRUE;

默认情况下,更改上述配置后,Codeigniter 会在 APPPATH(应用程序文件夹)下查找供应商/自动加载文件,这是不正确的。所以我把常量改成FCPATH(root path),因为root是使用composer安装第三方包的正确路径。

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

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

最后,运行

作曲家转储自动加载

如果仍然无法正常工作。 编码愉快!!!

【讨论】:

    猜你喜欢
    • 2017-02-01
    • 2017-02-28
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2017-12-26
    • 2020-11-03
    • 2020-01-21
    • 2018-05-27
    相关资源
    最近更新 更多