Skip to content

Commit 668a257

Browse files
committed
minor #2513 [Docs][LiveComponent] Add very simple download files docs (romain, zefyx)
This PR was merged into the 2.x branch. Discussion ---------- [Docs][LiveComponent] Add very simple download files docs | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | License | MIT Motivated by symfony/ux#1516 (comment) I don't know if it's enough. Maybe a dedicated section about the behavior of turbo when a LiveAction of a Live Component is returning a ``RedirectResponse`` should be more appropriate ? Commits ------- e525ee33f remove note about ongoing development 9a361b7a1 restore unrelated end of line formatting 99d3d21ef keep only the necessary code to illustrate the LiveAction b4b3a5352 Improve tip about Turbo behavior 546ef1ebe fix "Download" text indentation 105fa04d5 fix rst formating d0756fd77 Add very simple download files doc
2 parents 4861d59 + 56abc9f commit 668a257

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

doc/index.rst

+33
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,39 @@ The files will be available in a regular ``$request->files`` files bag::
13261326
need to specify ``multiple`` attribute on HTML element and end ``name``
13271327
with ``[]``.
13281328

1329+
.. _downloads:
1330+
1331+
Downloading files
1332+
-----------------
1333+
1334+
Currently, Live Components do not natively support returning file responses directly from a LiveAction. However, you can implement file downloads by redirecting to a route that handles the file response.
1335+
1336+
Create a LiveAction that generates the URL for the file download and returns a `RedirectResponse`::
1337+
1338+
#[LiveAction]
1339+
public function initiateDownload(UrlGeneratorInterface $urlGenerator): RedirectResponse
1340+
{
1341+
$url = $urlGenerator->generate('app_file_download');
1342+
return new RedirectResponse($url);
1343+
}
1344+
1345+
.. code-block:: html+twig
1346+
1347+
<div {{ attributes }} data-turbo="false">
1348+
<button
1349+
data-action="live#action"
1350+
data-live-action-param="initiateDownload"
1351+
>
1352+
Download
1353+
</button>
1354+
</div>
1355+
1356+
1357+
.. tip::
1358+
1359+
When Turbo is enabled, if a LiveAction response redirects to another URL, Turbo will make a request to prefetch the content. Here, adding ``data-turbo="false"`` ensures that the download URL is called only once.
1360+
1361+
13291362
.. _forms:
13301363

13311364
Forms

0 commit comments

Comments
 (0)