找不到类Firebase \ JWT \ JWT [英] Class Firebase\JWT\JWT 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 \Firebase\JWT\JWT;
$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中找不到类'Firebase \ JWT \ JWT':堆栈跟踪:#0 {main}抛出在/var/www/html/token中.php在第12行
Fatal error: Uncaught Error: Class 'Firebase\JWT\JWT' 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';
您尝试使用作曲器和模块很高兴,但是如果您是我,我将尝试阅读有关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.
这篇关于找不到类Firebase \ JWT \ JWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!