Skip to content
Advertisement

How to call a class from vendor folder when using a composer?

I am investigating jwt token examples from this link – https://github.com/firebase/php-jwt

So i run:

composer require firebase/php-jwt

And now i have new files and folders in my root directory of my site: vendor, composer.json and composer.lock. Folder “vendor” contents “firebase/php-jwt” folder.

So i tried to run an example script inside my root site’s folder (test.php, for example) with this content:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
use FirebaseJWTJWT;

$key = "example_key";
$payload = array(
    "iss" => "http://example.org",
    "aud" => "http://example.com",
    "iat" => 1356999524,
    "nbf" => 1357000000
);

/**
 * IMPORTANT:
 * You must specify supported algorithms for your application. See
 * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
 * for a list of spec-compliant algorithms.
 */
$jwt = JWT::encode($payload, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));

print_r($decoded);



$decoded_array = (array) $decoded;


JWT::$leeway = 60; // $leeway in seconds
$decoded = JWT::decode($jwt, $key, array('HS256'));
?>

And when i run it, i see:

Fatal error: Uncaught Error: Class ‘FirebaseJWTJWT’ not found in /var/www/html/jwt/test.php:20 Stack trace: #0 {main} thrown in /var/www/html/jwt/test.php on line 20

So how can i use a class FirebaseJWTJWT right?

Advertisement

Answer

From composer’s Autoloading page:

For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work:

require __DIR__ . '/vendor/autoload.php';
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement