Asked  7 Months ago    Answers:  5   Viewed   41 times

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

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);

However, when I go to localhost/token.php, I get this error message:

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

I'm not sure if it matters, but my PHP version is 7.0.

 Answers

73

As per the documentation, you need to include the autoloader.

E.g., in your php file:

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

But... typically your vendor folder would be at the same level than your html folder (the one where you are serving content).

E.g.:

- Project root
--- vendor
--- html
----- index.php

Then in your index.php you'd do:

 require __DIR__ . '/../vendor/autoload.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.

Wednesday, March 31, 2021
 
hnkk
answered 7 Months ago
18

If you prefer to keep slim under ext (as you mentioned here Slim framework - How to autoload Slim/Slim.php instead of using require?) instead of using it as a composer package, I believe this will work:

{
    "autoload": {
        "psr-0": {
            "": "ext/"
        }
    }
}
Wednesday, March 31, 2021
 
KouiK
answered 7 Months ago
92

If you don't keep track of which classes exist, autoloading won't help you.

But the situation would be pretty much the same if you tried to include all files and see which classes would come out of it, when there would be a chance that you missed a file.

Whatever it is what you try to do: Try it differently. I highly doubt you have to instantiate ALL these classes when serving one single request. It would not be very beneficial to the performance if you had to scan ALL declared classes for an interface, and then create an instance for all of them.

Provide some kind of registering method to let the application know about your classes. If you really have to use them all, so be it. But scanning whatever code has been run previously does not sound right (and you didn't explain what you do at all).

Saturday, May 29, 2021
 
Puneet
answered 5 Months ago
77

For PHPExcel to work properly you have to include PHPExcel.php like this:

include_once('PHPExcel/PHPExcel.php');

Also, in some cases reinstalling whole package can help.

Saturday, May 29, 2021
 
Kevin_Kinsey
answered 5 Months ago
27

The archive parameter is resolved relative to the codebase parameter. So in your case the plugin will look for a file MyApplet.class included in a file AppletPackage/JAR.jar.

You should change this to the following:

<applet code="AppletPackage.MyApplet" archive="JAR.jar" height="800" width="800">

This will resolve to AppletPackage/MyApplet.class inside JAR.jar in the same directory as the HTML file.

Sunday, August 15, 2021
 
kwhohasamullet
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :