php - Twig: passing file path as argument to url -


i want make files downloadble on click, not work. here comes code:

files.html.twig

{% set filepath = asset('bundles/example/files/filename.pdf') %} <a href="{{ url('_bundle_route_to_download', { 'filepath': filepath }) }}"> <button class="button button-icon button-download">download</button></a> 

controller.php

 /**     * @route("/example/download/{filepath}", name="_bundle_route_to_download")     */     public function downloadfile($filepath)     {       //...     } 

this error get:

no route found "get    /example/download//bundles/example/files/filename.pdf%3f1418390151 

what wrong syntax?

your syntax not wrong, symfony2 routing looking route not exist. need url_encode parameter.

the symfony2 routing looking url matching /example/download//bundles/example/files/ not found.

{% set filepath = (asset('bundles/example/files/filename.pdf'))|url_encode %} <a href="{{ url('_bundle_route_to_download', { 'filepath': filepath }) }}"> <button class="button button-icon button-download">download</button></a> 

it untested should doing trick.

for more information, see twig url encode

though i'd not recommend pass through parameter ... -> that's how that.


Comments