简体   繁体   English

在我的项目中找不到 PHP 库

[英]Can't Find PHP Library In My Project

I'm not a web dev (I'm a mobile developer) and trying to figure this out.我不是网络开发人员(我是移动开发人员)并试图解决这个问题。

  1. I'm using this library: https://github.com/ktamas77/firebase-php我正在使用这个库: https : //github.com/ktamas77/firebase-php

  2. I got 'composer' installed on my machine by following the documentation on composer's site.我按照 composer 网站上的文档在我的机器上安装了“composer”。

  3. I have installed the library in #1 by using this command: 'php composer.phar require ktamas77/firebase-php dev-master'我使用以下命令在 #1 中安装了库:'php composer.phar require ktamas77/firebase-php dev-master'

  4. In a certain screen or php file in my project, I'm adding this:在我项目的某个屏幕或 php 文件中,我添加了以下内容:

    $firebase = new \\Firebase\\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN); $firebase = new \\Firebase\\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN);

and when I run or go to that specific screen, there's an error like this:当我运行或转到该特定屏幕时,会出现如下错误:

Message: Class 'Firebase\\FirebaseLib' not found消息:找不到类“Firebase\\FirebaseLib”

My question is: Why is that?我的问题是:为什么会这样? Why can't this freakin php project find that FirebaseLib?为什么这个奇怪的 php 项目找不到 FirebaseLib? I can verify that I have such file in my project, cause I can open the file from that code above.我可以验证我的项目中有这样的文件,因为我可以从上面的代码打开文件。

The path of that file is this:该文件的路径是这样的:

'/Applications/MAMP/htdocs/xxx-cms/vendor/ktamas77/firebase-php/src/firebaseLib.php'

Is adding 'require' at the top of my php file required?是否需要在我的 php 文件顶部添加“require”?

I tried adding require but the error goes to that require when adding that.我尝试添加 require ,但是在添加它时错误会转到那个 require 。

Lastly, I even modified my composer.json and added "autoload" as was suggested here: How does PHP connection to firebase work?最后,我什至修改了我的 composer.json 并按照此处的建议添加了“自动加载”: PHP 与 firebase 的连接如何工作?

{
    "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",
        "ktamas77/firebase-php": "dev-master"
    },
    "suggest": {
        "paragonie/random_compat": "Provides better randomness in PHP 5.x"
    },
    "require-dev": {
        "mikey179/vfsStream": "1.1.*"
    },
    "autoload": {
        "classmap": ["vendor/ktamas77/firebase-php/src/firebaseLib.php"]
        "files": ["vendor/ktamas77/firebase-php/src/firebaseLib.php"]
    }

}

Unfortunately I wasn't able to do this instruction from that SO answer:不幸的是,我无法从那个 SO 答案中执行此指令:

Then simply require Composer's Autoloader with require "vendor/autoload.php";然后简单地使用 require "vendor/autoload.php" 来 require Composer 的 Autoloader; and new Firebase to autoload the class.和新的 Firebase 来自动加载类。

Thanks.谢谢。

You need to add the autoloader from composer into your base script or some bootstrap file.您需要将 Composer 中的自动加载器添加到您的基本脚本或一些引导程序文件中。

Something like:类似的东西:

require "vendor/autoload.php";

Autoloading - Composer Docs自动加载 - Composer 文档

Check the Composer documentation for more in depth usage of the vendor/autoload.php , where and how to use it, etc. From those docs:查看 Composer 文档,了解有关vendor/autoload.php更深入用法、在何处以及如何使用它等。从这些文档中:

For libraries that specify autoload information, Composer generates a vendor/autoload.php file.对于指定自动加载信息的库,Composer 会生成一个 vendor/autoload.php 文件。 You can simply include this file and start using the classes that those libraries provide without any extra work:您可以简单地包含此文件并开始使用这些库提供的类,而无需任何额外工作:

require __DIR__ . '/vendor/autoload.php';

$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));
$log->addWarning('Foo');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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