2017-02-16 49 views
0

我正在使用此包裝:https://github.com/RobThree/TwoFactorAuth,我正在嘗試遵循指南中您可以使用自己的QR碼提供程序的部分。找不到類別PHP(2FA包裝)

我下載了phpqrcode.php文件,並將其放置在TwoFactorAuth.php所在的目錄下。

require_once是像指南中的頂部,我得到的錯誤:

Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in /var/www/public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php on line 4

因此,與TwoFactorAuth.php目錄我加myprovider.php用下面的代碼:

<?php 
namespace RobThree\Auth\Providers\Qr; 

require_once(__DIR__ . '/../../phpqrcode.php'); 

class MyProvider implements IQRCodeProvider { 
    public function getMimeType() { 
    return 'image/png';        // This provider only returns PNG's 
    } 

    public function getQRCodeImage($qrtext, $size) { 
    ob_start();          // 'Catch' QRCode's output 
    QRCode::png($qrtext, null, QR_ECLEVEL_L, 3, 4); // We ignore $size and set it to 3 
                // since phpqrcode doesn't support 
                // a size in pixels... 
    $result = ob_get_contents();     // 'Catch' QRCode's output 
    ob_end_clean();         // Cleanup 
    return $result;         // Return image 
    } 
} 

我然後使用以下代碼嘗試生成QR碼,與自述文件類似:

<?php 
require_once __DIR__ . '/vendor/autoload.php'; 
$mp = new RobThree\Auth\Providers\Qr\MyProvider(); 
$tfa = new RobThree\Auth\TwoFactorAuth('My Company', 6, 30, 'sha1', $mp); 
$secret = $tfa->createSecret(); 
echo $tfa->getQRCodeImageAsDataUri('Bob Ross', $secret); 
?> 

但後來我得到這個錯誤..

Fatal error: Uncaught Error: Class 'RobThree\Auth\Providers\Qr\QRCode' not found in /var/www/public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php:13 Stack trace: #0 /var/www/public/vendor/robthree/twofactorauth/lib/TwoFactorAuth.php(146): RobThree\Auth\Providers\Qr\MyProvider->getQRCodeImage('otpauth://totp/...', 200) #1 /var/www/public/test.php(6): RobThree\Auth\TwoFactorAuth->getQRCodeImageAsDataUri('Bob Ross', 'ID2Y3P5C6N2NXKD...') #2 {main} thrown in /var/www/public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php on line 13

誰能幫助我?

回答

1

頂端您需要或者通過它完全解決了命名空間,OTR通過use語句引用類Qr。將namespace RobThree\Auth\Providers\Qr;添加到代碼的頂部將代碼置於該庫的名稱空間內,這是一種非常糟糕的設計模式。您的代碼不僅僅是RobThree Auth庫的一部分,而且當您需要使用另一個庫時會發生什麼?

請嘗試使用此代碼,而不是嘗試使用此代碼,並且不會遇到問題。另外,我建議命名空間自己的類,在像MyOrganisation\MyLibrary,我已經包含如下:

<?php 

require_once(__DIR__ . '/../../phpqrcode.php'); 

namespace MyOrganisation\MyLibrary; 

use RobThree\Auth\Providers\Qr\QRCode; 

class MyProvider implements IQRCodeProvider { 
    public function getMimeType() { 
    return 'image/png';        // This provider only returns PNG's 
    } 

    public function getQRCodeImage($qrtext, $size) { 
    ob_start();          // 'Catch' QRCode's output 
    QRCode::png($qrtext, null, QR_ECLEVEL_L, 3, 4); // We ignore $size and set it to 3 
                // since phpqrcode doesn't support 
                // a size in pixels... 
    $result = ob_get_contents();     // 'Catch' QRCode's output 
    ob_end_clean();         // Cleanup 
    return $result;         // Return image 
    } 
} 

其他選項的代碼(我最初提到)是:

<?php 

require_once(__DIR__ . '/../../phpqrcode.php'); 

namespace MyOrganisation\MyLibrary; 

class MyProvider implements IQRCodeProvider { 
    public function getMimeType() { 
    return 'image/png';        // This provider only returns PNG's 
    } 

    public function getQRCodeImage($qrtext, $size) { 
    ob_start();          // 'Catch' QRCode's output 
    RobThree\Auth\Providers\Qr\QRCode::png($qrtext, null, QR_ECLEVEL_L, 3, 4); // We ignore $size and set it to 3 
                // since phpqrcode doesn't support 
                // a size in pixels... 
    $result = ob_get_contents();     // 'Catch' QRCode's output 
    ob_end_clean();         // Cleanup 
    return $result;         // Return image 
    } 
} 

第一種解決方案會導致更多整潔的代碼,尤其是深層嵌套的命名空間類。

0

我固定它通過增加namespace RobThree\Auth\Providers\Qr;phpqrcode.php