【问题标题】:Use oauth2-client with codeigniter class not found将 oauth2-client 与未找到的 codeigniter 类一起使用
【发布时间】:2018-05-08 19:42:27
【问题描述】:

我正在尝试使用来自oauth2-client 的 oauth2-client。

我通过 composer 安装了这个包,它在我的根目录中创建了一个供应商文件夹,其中包含有联盟包。 然后我将它复制到我的 application/third_party/vendor 文件夹中。

在我的控制器中,我正在尝试这样做:

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

class APl extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        // Force SSL
        $this->force_ssl();

        // Form and URL helpers always loaded (just for convenience)
        $this->load->helper('url');
        $this->load->helper('form');

        //Load Libraries
        $this->load->library('managers/NameManager');
        $this->load->library('managers/BreadcrumbManager');
    }

    public function aPLogin() {
        $provider = new \League\OAuth2\Client\Provider\GenericProvider([
            'clientId'                => 'demoapp',    // The client ID assigned to you by the provider
            'clientSecret'            => 'demopass',   // The client password assigned to you by the provider
            'redirectUri'             => 'http://example.com/your-redirect-url/',
            'urlAuthorize'            => 'http://brentertainment.com/oauth2/lockdin/authorize',
            'urlAccessToken'          => 'http://brentertainment.com/oauth2/lockdin/token',
            'urlResourceOwnerDetails' => 'http://brentertainment.com/oauth2/lockdin/resource'
        ]);
    }
}

在我的配置文件中,我尝试输入以下代码:

$config['composer_autoload'] = TRUE;
require FCPATH . 'third_party\vendor\autoload.php';

在我的 index.php 文件中看起来像这样:

require FCPATH . 'third_party\vendor\autoload.php';

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 */
require_once BASEPATH.'core/CodeIgniter.php';

我尝试通过以下方式更改 FCPATH 要求:

require_once APPPATH.'third_party\vendor\autoload.php';

也试过了:

include_once BASEPATH.'../application/third_party/vendor/autoload.php';

那么我要么得到:

Fatal error: require(): Failed opening required 'D:\wamp\www\codeigniter\third_party\vendor\autoload.php' (include_path='.;C:\php\pear') in D:\wamp\www\codeigniter\index.php on line 311

Message: Class 'League\OAuth2\Client\Provider\GenericProvider' not found

【问题讨论】:

  • 您的主要问题是 CodeIgniter 期望您的供应商目录位于应用程序目录中。那是您的 composer.json 文件所属的地方,您将在其中运行您的 composer 命令来安装/更新包。
  • @BrianGottier 谢谢你的回答,我的目录结构是这样的:imgur.com/a/BPhnU
  • 这正是它不起作用的原因。供应商目录必须与third_party 处于同一级别。并且 codeigniter 会自动运行 autoload.php,所以你不必自己做。
  • @BrianGottier 我将 vendor 文件夹移到了 third_party 之外,所以现在看起来像这样:imgur.com/a/XcDRl 现在在我的配置中我只需要 $config['composer_autoload'] = TRUE;在我的 index.php 中,我还需要一些东西吗?
  • 您只需要$config['composer_autoload'] = TRUE;。您需要让您的 composer.json 文件与/vendor//third_party/ 处于同一级别。完成后,运行 composer install 和/或 update,因为 composer 需要更新所有包的路径。

标签: php codeigniter oauth-2.0 composer-php


【解决方案1】:

我只是想向您展示我如何使用您尝试使用的软件包。我设置了全新安装的 CodeIgniter,然后执行以下操作...

首先,我在 config/config 中设置 composer 自动加载:

$config['composer_autoload'] = TRUE;

接下来是一个最小的 composer.json,位于 APPPATH:

{
    "minimum-stability": "dev",
    "require": {
        "league/oauth2-client": "2.2.1"
    }
}

从命令行:

cd /path/to/application
composer install

然后,在我的欢迎控制器中输入:

public function oauth_test()
{
    $provider = new \League\OAuth2\Client\Provider\GenericProvider([
        'clientId'                => 'demoapp',    // The client ID assigned to you by the provider
        'clientSecret'            => 'demopass',   // The client password assigned to you by the provider
        'redirectUri'             => 'http://example.com/your-redirect-url/',
        'urlAuthorize'            => 'http://brentertainment.com/oauth2/lockdin/authorize',
        'urlAccessToken'          => 'http://brentertainment.com/oauth2/lockdin/token',
        'urlResourceOwnerDetails' => 'http://brentertainment.com/oauth2/lockdin/resource'
    ]);

    echo '<pre>';
    var_dump( $provider );
    echo '</pre>';
}

当我在浏览器中转到 /welcome/oauth_test 时,我得到了这个:

object(League\OAuth2\Client\Provider\GenericProvider)#15 (17) {
  ["urlAuthorize":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  string(51) "http://brentertainment.com/oauth2/lockdin/authorize"
  ["urlAccessToken":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  string(47) "http://brentertainment.com/oauth2/lockdin/token"
  ["urlResourceOwnerDetails":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  string(50) "http://brentertainment.com/oauth2/lockdin/resource"
  ["accessTokenMethod":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  NULL
  ["accessTokenResourceOwnerId":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  NULL
  ["scopes":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  NULL
  ["scopeSeparator":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  NULL
  ["responseError":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  string(5) "error"
  ["responseCode":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  NULL
  ["responseResourceOwnerId":"League\OAuth2\Client\Provider\GenericProvider":private]=>
  string(2) "id"
  ["clientId":protected]=>
  string(7) "demoapp"
  ["clientSecret":protected]=>
  string(8) "demopass"
  ["redirectUri":protected]=>
  string(37) "http://example.com/your-redirect-url/"
  ["state":protected]=>
  NULL
  ["grantFactory":protected]=>
  object(League\OAuth2\Client\Grant\GrantFactory)#16 (1) {
    ["registry":protected]=>
    array(0) {
    }
  }
  ["requestFactory":protected]=>
  object(League\OAuth2\Client\Tool\RequestFactory)#17 (0) {
  }
  ["httpClient":protected]=>
  object(GuzzleHttp\Client)#18 (1) {
    ["config":"GuzzleHttp\Client":private]=>
    array(7) {
      ["handler"]=>
      object(GuzzleHttp\HandlerStack)#19 (3) {
        ["handler":"GuzzleHttp\HandlerStack":private]=>
        object(Closure)#26 (2) {
          ["static"]=>
          array(2) {
            ["default"]=>
            object(Closure)#24 (2) {
              ["static"]=>
              array(2) {
                ["default"]=>
                object(GuzzleHttp\Handler\CurlMultiHandler)#20 (5) {
                  ["factory":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                  object(GuzzleHttp\Handler\CurlFactory)#21 (2) {
                    ["handles":"GuzzleHttp\Handler\CurlFactory":private]=>
                    array(0) {
                    }
                    ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=>
                    int(50)
                  }
                  ["selectTimeout":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                  int(1)
                  ["active":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                  NULL
                  ["handles":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                  array(0) {
                  }
                  ["delays":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                  array(0) {
                  }
                }
                ["sync"]=>
                object(GuzzleHttp\Handler\CurlHandler)#22 (1) {
                  ["factory":"GuzzleHttp\Handler\CurlHandler":private]=>
                  object(GuzzleHttp\Handler\CurlFactory)#23 (2) {
                    ["handles":"GuzzleHttp\Handler\CurlFactory":private]=>
                    array(0) {
                    }
                    ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=>
                    int(3)
                  }
                }
              }
              ["parameter"]=>
              array(2) {
                ["$request"]=>
                string(10) ""
                ["$options"]=>
                string(10) ""
              }
            }
            ["streaming"]=>
            object(GuzzleHttp\Handler\StreamHandler)#25 (1) {
              ["lastHeaders":"GuzzleHttp\Handler\StreamHandler":private]=>
              array(0) {
              }
            }
          }
          ["parameter"]=>
          array(2) {
            ["$request"]=>
            string(10) ""
            ["$options"]=>
            string(10) ""
          }
        }
        ["stack":"GuzzleHttp\HandlerStack":private]=>
        array(4) {
          [0]=>
          array(2) {
            [0]=>
            object(Closure)#27 (1) {
              ["parameter"]=>
              array(1) {
                ["$handler"]=>
                string(10) ""
              }
            }
            [1]=>
            string(11) "http_errors"
          }
          [1]=>
          array(2) {
            [0]=>
            object(Closure)#28 (1) {
              ["parameter"]=>
              array(1) {
                ["$handler"]=>
                string(10) ""
              }
            }
            [1]=>
            string(15) "allow_redirects"
          }
          [2]=>
          array(2) {
            [0]=>
            object(Closure)#29 (1) {
              ["parameter"]=>
              array(1) {
                ["$handler"]=>
                string(10) ""
              }
            }
            [1]=>
            string(7) "cookies"
          }
          [3]=>
          array(2) {
            [0]=>
            object(Closure)#30 (1) {
              ["parameter"]=>
              array(1) {
                ["$handler"]=>
                string(10) ""
              }
            }
            [1]=>
            string(12) "prepare_body"
          }
        }
        ["cached":"GuzzleHttp\HandlerStack":private]=>
        NULL
      }
      ["allow_redirects"]=>
      array(5) {
        ["max"]=>
        int(5)
        ["protocols"]=>
        array(2) {
          [0]=>
          string(4) "http"
          [1]=>
          string(5) "https"
        }
        ["strict"]=>
        bool(false)
        ["referer"]=>
        bool(false)
        ["track_redirects"]=>
        bool(false)
      }
      ["http_errors"]=>
      bool(true)
      ["decode_content"]=>
      bool(true)
      ["verify"]=>
      bool(true)
      ["cookies"]=>
      bool(false)
      ["headers"]=>
      array(1) {
        ["User-Agent"]=>
        string(56) "GuzzleHttp/6.2.1 curl/7.47.0 PHP/7.0.22-0ubuntu0.16.04.1"
      }
    }
  }
}

因此,您在此之外所做的任何事情都是不必要的。如果是我,我想我会尝试删除整个供应商目录并重试。另外,我注意到您使用的是 Windows,虽然我认为这无关紧要,但我使用的是 Linux(这是大多数 Web 代码的生产环境)。

【讨论】:

  • 这对我有用!我不确定我之前做错了什么,但这确实成功了。非常感谢!
猜你喜欢
  • 2018-10-02
  • 2015-01-16
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-29
  • 2021-01-11
相关资源
最近更新 更多