找不到类PHP(2FA包)

时间:2017-02-16 19:05:07

标签: php

我正在使用此软件包:https://github.com/RobThree/TwoFactorAuth并且我正在尝试按照指南中您可以使用自己的QR代码提供程序的部分。

我下载了phpqrcode.php文件并将其放在TwoFactorAuth.php所在的目录中。

require_once在指南中位于顶部时,我收到错误:

  

致命错误:命名空间声明语句必须是第4行/var/www/public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php脚本中的第一个语句或任何声明调用之后

因此,在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);
?>

但后来我收到了这个错误..

  

致命错误:未捕获错误:类&#39; RobThree \ Auth \ Providers \ Qr \ QRCode&#39;在/var/www/public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php:13中找不到。堆栈跟踪:#0 /var/www/public/vendor/robthree/twofactorauth/lib/TwoFactorAuth.php (146):RobThree \ Auth \ Providers \ Qr \ MyProvider-&gt; getQRCodeImage(&#39; otpauth://totp /...&# 39;,200)#1 /var/www/public/test.php (6):RobThree \ Auth \ TwoFactorAuth-&gt; getQRCodeImageAsDataUri(&#39; Bob Ross&#39;,&#39; ID2Y3P5C6N2NXKD ...&#39;)/ var / www / public中抛出#2 {main}第13行/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php

任何人都可以帮我吗?

3 个答案:

答案 0 :(得分:1)

您需要通过它的完全解析的命名空间,通过Qr语句引用类use。将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
  }
}

第一个解决方案导致更简洁的代码,特别是对于深度嵌套的命名空间类。

答案 1 :(得分:1)

如果您遵循了本教程,则可以使用以下代码

\QRCode::png($qrtext, null, QR_ECLEVEL_L, 3, 4); 

答案 2 :(得分:0)

我通过将namespace RobThree\Auth\Providers\Qr;添加到phpqrcode.php

的顶部来修复它