找不到类 FirebaseJWTJWT [英] Class FirebaseJWTJWT not found
问题描述
我想在我的代码中使用纯 firebase/php-jwt 库.首先,我转到 /var/www/html/
并像官方图书馆页面建议的那样,我这样做
I want to use pure firebase/php-jwt library in my code. Firstly, I go to /var/www/html/
and like the official library page is suggesting, I do this
composer require firebase/php-jwt
运行此命令后,我看到在 /var/www/html/
中创建了一个新的供应商文件夹,其中包含许多文件和子文件夹.然后我在 /var/www/html/
中创建我的 token.php
文件,其内容与官方页面中的内容类似:
After I run this command, I see that a new vendor folder is created inside /var/www/html/
with a lot of files and subfolders. I then create my token.php
file right in /var/www/html/
with contents similar to that from the official page:
<?php
use FirebaseJWTJWT;
$key = "example_key";
$token = array(
"iss" => "http://example.org",
"aud" => "http://example.com",
"iat" => 1356999524,
"nbf" => 1357000000
);
$jwt = JWT::encode($token, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));
print_r($decoded);
但是,当我转到 localhost/token.php 时,我收到此错误消息:
However, when I go to localhost/token.php, I get this error message:
致命错误:未捕获的错误:在/var/www/html/token.php:12 中找不到类FirebaseJWTJWT" 堆栈跟踪:#0 {main} 被抛出到/var/www/html/token.php 第 12 行
Fatal error: Uncaught Error: Class 'FirebaseJWTJWT' not found in /var/www/html/token.php:12 Stack trace: #0 {main} thrown in /var/www/html/token.php on line 12
我不确定这是否重要,但我的 PHP 版本是 7.0.
I'm not sure if it matters, but my PHP version is 7.0.
推荐答案
根据 文档,您需要包含自动加载器.
As per the documentation, you need to include the autoloader.
例如,在您的 php 文件中:
E.g., in your php file:
require __DIR__ . '/vendor/autoload.php';
但是...通常您的 vendor
文件夹与您的 html
文件夹(您在其中提供内容的文件夹)处于同一级别).
But... typically your vendor
folder would be at the same level than your html
folder (the one where you are serving content).
例如:
- Project root
--- vendor
--- html
----- index.php
然后在您的 index.php 中:
Then in your index.php you'd do:
require __DIR__ . '/../vendor/autoload.php';
很高兴您尝试使用 Composer 和模块,但如果我是您,我会尝试阅读一些有关 php 的 自动加载功能,让您了解正在发生的事情,以及如何从中获利.
It is nice that you trying to use composer and modules, but if I were you I'd try to read on a bit about php's autoloading features, so you understand what's going on, and how you can profit from them.
这篇关于找不到类 FirebaseJWTJWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!