【问题标题】:Laravel Google 2fa QR Code not displayingLaravel Google 2fa QR码不显示
【发布时间】:2020-06-30 16:35:00
【问题描述】:

我目前正在使用 Google 2fa 的包,并从我的控制器中以字符串变量的形式提取 SVG QR 码,然后根据包要求将 QR 码添加为 .问题是这不显示图像,我认为这是由于我从控制器中提取的字符串值。

这是我从控制器收到的变量的值:

在我的刀片文件中回显它时,它只是回显字符串值。如果我要复制没有“”的字符串值,Laravel 会将该值识别为 html 并显示我的二维码。有没有办法回显它让刀片将其识别为 html 代码?或者,当以这种方式将其作为变量拉入时,如何在刀片文件中显示此 SVG 文件?如果有人愿意帮助我,将不胜感激!

链接到 Laravel 包 -> https://github.com/antonioribeiro/google2fa-laravel

我的控制器:

public function register(Request $request)
    {
        //Validate the incoming request using the already included validator method
        $this->validator($request->all())->validate();

        // Initialise the 2FA class
        $google2fa = app('pragmarx.google2fa');

        // Save the registration data in an array
        $registration_data = $request->all();

        // Add the secret key to the registration data
        $registration_data["google2fa_secret"] = $google2fa->generateSecretKey();

        // Save the registration data to the user session for just the next request
        $request->session()->flash('registration_data', $registration_data);

        // Generate the QR image. This is the image the user will scan with their app
        // to set up two factor authentication
        $QR_Image = $google2fa->getQRCodeInline(
            config('app.name'),
            $registration_data['email'],
            $registration_data['google2fa_secret']
        );

        // Pass the QR barcode image to our view
        return view('google2fa.register', ['QR_Image' => $QR_Image, 'secret' => $registration_data['google2fa_secret']]);
    }

我的观点:

<div>
     <img src="{{ $QR_Image }}">
</div>

【问题讨论】:

  • 您尝试过什么调试问题?有没有可以分享的代码?
  • 尝试{!! $QR_Image !!}} - 它正在逃避它。
  • 问题:如果你要嵌入一个 static SVG 图像,你会选择&lt;img src="&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;svg&gt;...&lt;/svg&gt;"&gt; ...吗?不,你不会?那你为什么要在这里做同样的事情呢?
  • 如果您想通过图像元素的src 属性像这样嵌入它,您需要将您的 SVG 代码转换为数据 URI。 css-tricks.com/lodge/svg/09-svg-data-uris

标签: php laravel image svg


【解决方案1】:

这应该很容易:

<div>
     {!! $QR_Image !!}
</div>

您需要 {!! !!} 来呈现 HTML/SVG 数据而不转义它。并且您需要删除 img 标签,因为它不是真实图像,而是 SVG。

【讨论】:

  • 谢谢你,Farkie,它有效!不敢相信这么简单,感谢您的宝贵时间!
猜你喜欢
  • 2016-02-07
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
相关资源
最近更新 更多