【问题标题】:Class not found PHP (2FA package)找不到类 PHP(2FA 包)
【发布时间】:2017-02-16 19:05:07
【问题描述】:

我正在使用这个包: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
  }
}

然后我使用以下代码尝试生成二维码,类似于自述文件:

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

但是我得到了这个错误..

致命错误:未捕获的错误:在 /var/www/public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php:13 中找不到类 'RobThree\Auth\Providers\Qr\QRCode' 堆栈跟踪: #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} 抛出 /var/www /public/vendor/robthree/twofactorauth/lib/Providers/Qr/MyProvider.php 在第 13 行

谁能帮我解决这个问题?

【问题讨论】:

    标签: php


    【解决方案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
      }
    }
    

    第一个解决方案导致代码更整洁,尤其是深度嵌套的命名空间类。

    【讨论】:

      【解决方案2】:

      如果您按照教程进行操作,则可以使用以下代码

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

      【讨论】:

      • 请在您的回答中解释这与提问者所拥有的有何不同,以及这种差异对于解决他们的问题有何重要意义。
      【解决方案3】:

      我通过将namespace RobThree\Auth\Providers\Qr; 添加到phpqrcode.php 的顶部来修复它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-25
        相关资源
        最近更新 更多