2017-06-15 61 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