2017-06-15 57 views
1

因此,我想通过在Symfony中的TWIG模板中提供一个URL来动态创建QR代码。我安装了endroid/QrCode软件包,似乎很好,但我不确定如何让TWIG创建QR码图像!Symfony中的TWIG中的endroid Qr代码

从GitHub的页面

因此,所有它说的嫩枝是:

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扩展?

+0

您确实在'Symfony'右边注册了包? – DarkBee

+0

当然,在AppKernel.php中如果我没有,我不认为它甚至会知道在哪里寻找函数qrcode_url – MicWit

+0

那么在[QrController]中定义了路由(https://github.com/ endroid/QrCode/blob/master/src/Bundle/Controller/QrCodeController.php),你是否在添加bundle后清除缓存? – DarkBee

回答

2

确保您包括对包的路由配置,因为它是在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 
+0

工作的很好,我读了TWIG扩展的文档,不需要这部分,因为它看起来像它的控制器信息。谢谢。 – MicWit