【问题标题】:using namespace of library inside symfony controller在 symfony 控制器中使用库的命名空间
【发布时间】:2019-11-08 15:42:39
【问题描述】:

我的控制器中有以下代码,基本上是尝试使用我通过 composer 安装的this

use Symfony\Component\HttpFoundation\Response;
require __DIR__.'/../../vendor/autoload.php';

class MainController
{

    public function number()
    {

        $username = 'someusername';
        $password = 'somepassword';
        $debug = true;
        $truncatedDebug = false;

        $ig = new \InstagramAPI\Instagram($debug, $truncatedDebug);


        $number = random_int(0, 100);

        return new Response(
            '<html><body>Lucky number: '.$number.'</body></html>'
        );
    }
}

但是它给了我以下内容

Attempted to load class "Instagram" from namespace "InstagramAPI".
Did you forget a "use" statement for another namespace?

我尝试添加 use namespace Instagram ,但没有成功

【问题讨论】:

  • 如果您注释掉 Instagram 行,您是否会收到与 Response 相同的错误?
  • 您是否使用文档中显示的 composer require 命令安装?并且已经在下面指出,由于框架会处理它,因此无需再次要求 autoload.php。
  • 出于好奇,我运行了“composer require mgp25/instagram-php”,它似乎都安装了。必须安装 php bcmath 扩展。并且 instagram 包安装了一堆依赖项。但这一切都按预期进行。因此,请仔细检查您的安装是否没有错误。

标签: php symfony composer-php


【解决方案1】:

根据https://github.com/mgp25/Instagram-API/blob/master/README.md,命名空间称为InstagramAPI

试试new \InstagramAPI\Instagram($debug, $truncatedDebug);

【讨论】:

  • 这也不起作用,我尝试从命名空间“InstagramAPI”加载类“Instagram”。您是否忘记了另一个命名空间的“使用”语句?
  • 确保您可以在vendor 文件夹中看到mgp25/instagrap-php 文件夹。也不需要require __DIR__.'/../../vendor/autoload.php';,因为 symfony 已经处理了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多