Skip to content

Commit

Permalink
minor #2513 [Docs][LiveComponent] Add very simple download files docs…
Browse files Browse the repository at this point in the history
… (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
  • Loading branch information
smnandre committed Jan 25, 2025
2 parents 4861d59 + 56abc9f commit 668a257
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,39 @@ The files will be available in a regular ``$request->files`` files bag::
need to specify ``multiple`` attribute on HTML element and end ``name``
with ``[]``.

.. _downloads:

Downloading files
-----------------

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.

Create a LiveAction that generates the URL for the file download and returns a `RedirectResponse`::

#[LiveAction]
public function initiateDownload(UrlGeneratorInterface $urlGenerator): RedirectResponse
{
$url = $urlGenerator->generate('app_file_download');
return new RedirectResponse($url);
}

.. code-block:: html+twig

<div {{ attributes }} data-turbo="false">
<button
data-action="live#action"
data-live-action-param="initiateDownload"
>
Download
</button>
</div>


.. tip::

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.


.. _forms:

Forms
Expand Down

0 comments on commit 668a257

Please sign in to comment.