2017-01-24 119 views
2

我跑sudo composer require lcobucci/jwt來安裝東西。在Laravel找不到類5

然後,我在我的文件的頂部添加use Lcobucci\JWT\Configuration;

然後,我開始使用示例代碼

 $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?) 

我不斷收到此錯誤

類「Lcobucci \智威湯遜\配置'找不到

我該如何預防?


這裏是我的整個composer.json文件

{ 
    "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" 
    } 
} 
+2

你把它添加到作曲家的自動加載路徑中了嗎? – apokryfos

+0

存儲庫沒有提到任何有關的信息。 ... :(= https://github.com/lcobucci/jwt – ihue

+0

如何手動添加它?您可以詳細說明或發佈您的答案或建議 – ihue

回答

1

你已經安裝的程序包^3.2版本。如果您查看此版本的代碼和文檔,則不存在Configuration對象;有一個Builder對象。

如果你只是去https://github.com/lcobucci/jwt,你會看到他們的主分支。根據他們的文檔,他們目前正在研究他們的下一個主要版本:

重要提示:這是我們下一個主要版本(v4)的文檔,它會改變。如果你正在使用穩定版本,你應該去分支3.2。

確保你看的版本的文檔使用的是:

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

這是3.2的示例代碼(您正在使用的版本):

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?) 
+0

好的眼睛,感謝您的更正。 – ihue

3

嘗試運行composer dumpauto命令

+0

我這樣做並刷新頁面,我得到了同樣的錯誤。我用我目前的'composer.json'更新了我的帖子。你是否發現任何奇怪的東西? – ihue

+0

@ihue試圖用'php artisan cache:clear'清除緩存。如果它不起作用,刪除'bootstrap/cache/services .php'文件手動。 –