php – Class Not Found in Laravel 5-ThrowExceptions

Exception or error:

I ran sudo composer require lcobucci/jwt to install the stuff.

Then, I added use Lcobucci\JWT\Configuration; on top of my file

Then, I start use the sample code

        $config = new Configuration(); // This object helps to simplify the creation of the dependencies
                               // instead of using "?:" on constructors.
        $token = $config->createBuilder()
        ->issuedBy('https://login.uat.telenet.be/openid') // Configures the issuer (iss claim)
        ->canOnlyBeUsedBy('site') // Configures the audience (aud claim)
        ->identifiedBy('888254e8-f1e8-4956-86fa-a6c0f61a6421', true) // Configures the id (jti claim), replicating as a header item
        ->issuedAt(time()) // Configures the time that the token was issue (iat claim)
        ->canOnlyBeUsedAfter(time() + 60) // Configures the time that the token can be used (nbf claim)
        ->expiresAt(time() + 3600) // Configures the expiration time of the token (exp claim)
        ->with('uid', 1) // Configures a new claim, called "uid"
        ->getToken(); // Retrieves the generated token

        $token->getHeaders(); // Retrieves the token headers
        $token->getClaims(); // Retrieves the token claims

        echo $token->getHeader('jti'); // will print "4f1g23a12aa"
        echo $token->getClaim('iss'); // will print "http://example.com"
        echo $token->getClaim('uid'); // will print "1"
        echo $token; // The string representation of the object is a JWT string (pretty easy, right?)

I kept getting this error

Class ‘Lcobucci\JWT\Configuration’ not found

How do I prevent that ?


Here is my entire composer.json file

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "illuminate/html": "^5.0",
        "laracasts/utilities": "~2.0",
        "barryvdh/laravel-debugbar": "^2.0",
        "sammyk/laravel-facebook-sdk": "~3.0",
        "doctrine/dbal": "^2.5",
        "lcobucci/jwt": "^3.2"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1",
        "mcamara/laravel-localization": "1.0.*"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "Helpers\\": "app/Helpers/"
        },
        "files": ["app/Helper.php"]
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "pre-update-cmd": [
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-root-package-install": [
            "php -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}
How to solve:

You’ve installed version ^3.2 of the package. If you look at the code and documentation for this version, there is no Configuration object; there is a Builder object.

If you just go to https://github.com/lcobucci/jwt, you will be looking at their master branch. According to their documentation, they are currently working on their next major version:

Important: this is the documentation of our next major release (v4) and it WILL change. If you are using the stable version you should go to branch 3.2.

Make sure you look at the documentation for the version you are using:

https://github.com/lcobucci/jwt/tree/3.2

This is their example code for 3.2 (the version you are using):

use Lcobucci\JWT\Builder;

$token = (new Builder())->setIssuer('http://example.com') // Configures the issuer (iss claim)
                        ->setAudience('http://example.org') // Configures the audience (aud claim)
                        ->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
                        ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
                        ->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim)
                        ->setExpiration(time() + 3600) // Configures the expiration time of the token (nbf claim)
                        ->set('uid', 1) // Configures a new claim, called "uid"
                        ->getToken(); // Retrieves the generated token


$token->getHeaders(); // Retrieves the token headers
$token->getClaims(); // Retrieves the token claims

echo $token->getHeader('jti'); // will print "4f1g23a12aa"
echo $token->getClaim('iss'); // will print "http://example.com"
echo $token->getClaim('uid'); // will print "1"
echo $token; // The string representation of the object is a JWT string (pretty easy, right?)

Answer:

Try to run composer dumpauto command

Answer:

First of all remove the vendor folder and try to reinstall the composer with the command

composer update 

have been facing an issue for more than 2 hours trying different ways and then try this and my issue resolved :D/
enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *