Symfony 中 TWIG 中的 endroid Qr 代码

endroid Qr Code in TWIG in Symfony

所以我想通过在 Symfony 的 TWIG 模板中输入 URL 来动态制作二维码。我安装了 endroid/QrCode 包,看起来没问题,但我不确定如何让 TWIG 创建二维码图像!

所以在 GitHub 页面上,它对 TWIG 的描述是:

Twig extension

The bundle provides a Twig extension for generating a QR code URL, path or data URI. You can use the second argument of any of these functions to override any defaults defined by the bundle or set via your configuration.

<img src="{{ qrcode_path(message) }}" />
<img src="{{ qrcode_url(message, { writer: 'eps' }) }}" />
<img src="{{ qrcode_data_uri(message, { writer: 'svg', size: 150 }) }}" />

所以我想到了一个测试,最简单的事情是这样的:

<img src="{{ qrcode_url('http://www.test.com') }}" />

但是,这会引发错误:

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "endroid_qrcode_generate" as such route does not exist.").

有人得到了这个工作的 TWIG 扩展吗?

确保包含捆绑包的路由配置,如 documentation

中所述

Add the following section to your routing to be able to handle QR code URLs. This step can be skipped if you only use data URIs to display your images.

将路由配置添加到例如app/config/routing.yml:

EndroidQrCodeBundle:
    resource: "@EndroidQrCodeBundle/Controller/"
    type:     annotation
    prefix:   /qrcode