2017-02-16 17 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 
    } 
} 

私は次のコードを使用して、readmeと同様に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

の先頭にnamespace RobThree\Auth\Providers\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

私はあなたがそれが完全に解決の名前空間を経由して、OTR use文のいずれかを介してクラスQrを参照する必要がphpqrcode.php

関連する問題