Skip to content

Commit 99f0fd8

Browse files
committed
added tip #336
1 parent 06e8f2d commit 99f0fd8

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Currently, there are over 300 tips categorized as follows:
1616
- 🔄 [Routing & Request Tips](./tips/routing.md) (22 tips)
1717
- 📊 [Laravel Collections Tips](./tips/collections.md) (17 tips)
1818
-[Validation Tips](./tips/validation.md) (19 tips)
19-
- 🌐 [API & HTTP Client Tips](./tips/api-and-http-client.md) (13 tips)
19+
- 🌐 [API & HTTP Client Tips](./tips/api-and-http-client.md) (14 tips)
2020
- 💡 [Random Cool Tips](./tips/others.md) (16 tips)
2121
- 🖼️ [View Tips](./tips/views.md) (10 tips)
2222
- 📬 [Queues & Job Tips](./tips/queues-and-jobs.md) (10 tips)

tips/api-and-http-client.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Global Middleware for HTTP Client](#laravel-tip--global-middleware-for-http-client-️)
1414
- [Convert Responses to Exceptions](#laravel-tip--convert-responses-to-exceptions-️)
1515
- [HTTP Response Status Helpers](#laravel-tip--http-response-status-helpers-️)
16+
- [Real-Time Download Progress](#laravel-tip--real-time-download-progress-️)
1617

1718
## Laravel Tip 💡: The "withToken()" method ([⬆️](#api--the-http-client-tips-cd-))
1819

@@ -309,3 +310,27 @@ $response->unprocessableContent(); // status code 422
309310
$response->serverError(); // status code >= 500
310311
$response->clientError(); // status code >= 400 && <500
311312
```
313+
314+
## Laravel Tip 💡: Real-Time Download Progress ([⬆️](#api--the-http-client-tips-cd-))
315+
316+
![Laravel](https://img.shields.io/badge/Laravel-%3E%3D7-FF2D20?style=for-the-badge&logo=laravel&logoColor=white)
317+
318+
If you ever need to download a file in your Laravel app, consider using Guzzle's "progress" option. It gives you real-time updates on the download, which you can broadcast to your UI, display in the console, or handle however you like 🚀
319+
320+
```php
321+
<?php
322+
323+
use Illuminate\Support\Facades\Http;
324+
325+
Http::withToken('an-api-token')
326+
->get('https://api.example.com/plugins/some-theme')
327+
->timeout(30)
328+
->withOptions([
329+
'sink' => storage_path('plugins/some-theme.zip'),
330+
'progress' => function ($downloadTotal, $downloadedBytes) {
331+
// You can emit an event to the frontend for real-time updates,
332+
event(new PluginDownloadProgress($downloadedBytes, $downloadTotal));
333+
// Or update a progress bar in a console command, etc..
334+
}
335+
]);
336+
```

0 commit comments

Comments
 (0)