From d0756fd7746354eb333f220906d6831a58a17e9b Mon Sep 17 00:00:00 2001 From: romain Date: Mon, 20 Jan 2025 08:59:25 +0100 Subject: [PATCH 1/7] Add very simple download files doc --- src/LiveComponent/doc/index.rst | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/LiveComponent/doc/index.rst b/src/LiveComponent/doc/index.rst index 4c7b5fc242..dedd08e072 100644 --- a/src/LiveComponent/doc/index.rst +++ b/src/LiveComponent/doc/index.rst @@ -1326,6 +1326,51 @@ 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`:: + + use Symfony\Component\HttpFoundation\RedirectResponse; + use Symfony\Component\Routing\Generator\UrlGeneratorInterface; + use Symfony\UX\LiveComponent\Attribute\LiveAction; + + // ... + + class MyDownloadButton + { + // ... + + #[LiveAction] + public function initiateDownload(UrlGeneratorInterface $urlGenerator): RedirectResponse + { + $url = $urlGenerator->generate('app_file_download'); + return new RedirectResponse($url); + } + } + +.. code-block:: html+twig + +
+ +
+ +When Turbo is enabled, it will intercept the redirect and initiate a second request for the download URL. Adding `data-turbo="false"` ensures that the download URL is called only once. + +.. note:: + + Native support for file downloads in Live Components is under development. For more details, refer to the related `pull request #2483 `_. + + .. _forms: Forms From 105fa04d5c6e08d908330c0ff41350fc5cb6fdbc Mon Sep 17 00:00:00 2001 From: romain Date: Tue, 21 Jan 2025 07:48:09 +0100 Subject: [PATCH 2/7] fix rst formating --- src/LiveComponent/doc/index.rst | 46 ++++++++++++++++----------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/LiveComponent/doc/index.rst b/src/LiveComponent/doc/index.rst index dedd08e072..3b06dc3ddb 100644 --- a/src/LiveComponent/doc/index.rst +++ b/src/LiveComponent/doc/index.rst @@ -1329,40 +1329,40 @@ The files will be available in a regular ``$request->files`` files bag:: .. _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`:: - use Symfony\Component\HttpFoundation\RedirectResponse; - use Symfony\Component\Routing\Generator\UrlGeneratorInterface; - use Symfony\UX\LiveComponent\Attribute\LiveAction; + use Symfony\Component\HttpFoundation\RedirectResponse; + use Symfony\Component\Routing\Generator\UrlGeneratorInterface; + use Symfony\UX\LiveComponent\Attribute\LiveAction; - // ... + // ... - class MyDownloadButton - { - // ... + class MyDownloadButton + { + // ... - #[LiveAction] - public function initiateDownload(UrlGeneratorInterface $urlGenerator): RedirectResponse - { - $url = $urlGenerator->generate('app_file_download'); - return new RedirectResponse($url); - } - } + #[LiveAction] + public function initiateDownload(UrlGeneratorInterface $urlGenerator): RedirectResponse + { + $url = $urlGenerator->generate('app_file_download'); + return new RedirectResponse($url); + } + } .. code-block:: html+twig -
- -
+
+ +
When Turbo is enabled, it will intercept the redirect and initiate a second request for the download URL. Adding `data-turbo="false"` ensures that the download URL is called only once. From 546ef1ebe0e37c6b4900eb989d570169369676b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=C3=A9fyx?= <113043125+zefyx@users.noreply.github.com> Date: Wed, 22 Jan 2025 08:06:40 +0100 Subject: [PATCH 3/7] fix "Download" text indentation Co-authored-by: Hugo Alliaume --- src/LiveComponent/doc/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LiveComponent/doc/index.rst b/src/LiveComponent/doc/index.rst index 3b06dc3ddb..a3e11639e7 100644 --- a/src/LiveComponent/doc/index.rst +++ b/src/LiveComponent/doc/index.rst @@ -1360,7 +1360,7 @@ Create a LiveAction that generates the URL for the file download and returns a ` data-action="live#action" data-live-action-param="initiateDownload" > - Download + Download From b4b3a535295ad24b8a2ce8731a9e832b22e6f91c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=C3=A9fyx?= <113043125+zefyx@users.noreply.github.com> Date: Wed, 22 Jan 2025 08:10:00 +0100 Subject: [PATCH 4/7] Improve tip about Turbo behavior MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Simon André --- src/LiveComponent/doc/index.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/LiveComponent/doc/index.rst b/src/LiveComponent/doc/index.rst index a3e11639e7..3f3cd89108 100644 --- a/src/LiveComponent/doc/index.rst +++ b/src/LiveComponent/doc/index.rst @@ -1364,7 +1364,10 @@ Create a LiveAction that generates the URL for the file download and returns a ` -When Turbo is enabled, it will intercept the redirect and initiate a second request for the download URL. Adding `data-turbo="false"` ensures that the download URL is called only once. + +.. 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. .. note:: From 99d3d21ef6d3191d2c3139a3743b4d36e4bd7102 Mon Sep 17 00:00:00 2001 From: romain Date: Wed, 22 Jan 2025 08:16:02 +0100 Subject: [PATCH 5/7] keep only the necessary code to illustrate the LiveAction --- src/LiveComponent/doc/index.rst | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/LiveComponent/doc/index.rst b/src/LiveComponent/doc/index.rst index 3f3cd89108..cca442c9ee 100644 --- a/src/LiveComponent/doc/index.rst +++ b/src/LiveComponent/doc/index.rst @@ -1335,23 +1335,12 @@ Currently, Live Components do not natively support returning file responses dire Create a LiveAction that generates the URL for the file download and returns a `RedirectResponse`:: - use Symfony\Component\HttpFoundation\RedirectResponse; - use Symfony\Component\Routing\Generator\UrlGeneratorInterface; - use Symfony\UX\LiveComponent\Attribute\LiveAction; - - // ... - - class MyDownloadButton - { - // ... - #[LiveAction] public function initiateDownload(UrlGeneratorInterface $urlGenerator): RedirectResponse { $url = $urlGenerator->generate('app_file_download'); return new RedirectResponse($url); } - } .. code-block:: html+twig @@ -3802,7 +3791,7 @@ uses Symfony's test client to render and make requests to your components:: // authenticate a user ($user is instance of UserInterface) $testComponent->actingAs($user); - // set the '_locale' route parameter (if the component route is localized) + // set the '_locale' route parameter (if the component route is localized) $testComponent->setRouteLocale('fr'); // customize the test client From 9a361b7a1d72d9970365a32c593781046693e68a Mon Sep 17 00:00:00 2001 From: romain Date: Thu, 23 Jan 2025 12:44:11 +0100 Subject: [PATCH 6/7] restore unrelated end of line formatting introduced with 99d3d21ef6d3191d2c3139a3743b4d36e4bd7102 --- src/LiveComponent/doc/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LiveComponent/doc/index.rst b/src/LiveComponent/doc/index.rst index cca442c9ee..6b7a9f8fad 100644 --- a/src/LiveComponent/doc/index.rst +++ b/src/LiveComponent/doc/index.rst @@ -3791,7 +3791,7 @@ uses Symfony's test client to render and make requests to your components:: // authenticate a user ($user is instance of UserInterface) $testComponent->actingAs($user); - // set the '_locale' route parameter (if the component route is localized) + // set the '_locale' route parameter (if the component route is localized) $testComponent->setRouteLocale('fr'); // customize the test client From e525ee33f29c42fe5ddba820c007a861e5be59c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=C3=A9fyx?= <113043125+zefyx@users.noreply.github.com> Date: Fri, 24 Jan 2025 09:35:55 +0100 Subject: [PATCH 7/7] remove note about ongoing development MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Simon André --- src/LiveComponent/doc/index.rst | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/LiveComponent/doc/index.rst b/src/LiveComponent/doc/index.rst index 6b7a9f8fad..b6ec0b237f 100644 --- a/src/LiveComponent/doc/index.rst +++ b/src/LiveComponent/doc/index.rst @@ -1358,10 +1358,6 @@ Create a LiveAction that generates the URL for the file download and returns a ` 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. -.. note:: - - Native support for file downloads in Live Components is under development. For more details, refer to the related `pull request #2483 `_. - .. _forms: