Skip to content

Commit 3388e19

Browse files
committed
feat:update the PWA logo dynamically
1 parent ef88826 commit 3388e19

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

README.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,53 @@ Just before the closing `</body>` tag in your main layout file, add:
110110
</body>
111111
</html>
112112
```
113-
114113
These directives will automatically generate the necessary tags and JavaScript for the PWA.
115114

115+
116+
## Uploading a Logo via PWA 🌟
117+
To update the PWA logo dynamically, follow these steps:
118+
Your Laravel PWA is now configured to update the logo dynamically! 🚀
119+
120+
#### 1. **Create a Controller Method**
121+
122+
**Input Key Name:** `logo`
123+
Make sure the image is in PNG format, at least 512x512 pixels, and does not exceed 1024 KB in size.
124+
125+
```html
126+
<input type="file" name="logo" accept=".png">
127+
```
128+
129+
```php
130+
namespace App\Http\Controllers;
131+
132+
use EragLaravelPwa\Core\PWA;
133+
use Illuminate\Http\Request;
134+
use Illuminate\Routing\Controller;
135+
136+
class SettingsController extends Controller
137+
{
138+
public function uploadLogo(Request $request)
139+
{
140+
$response = PWA::processLogo($request);
141+
142+
if ($response['status']) {
143+
return redirect()->back()->with('success', $response['message']);
144+
}
145+
146+
return redirect()->back()->withErrors($response['errors'] ?? ['Something went wrong.']);
147+
}
148+
}
149+
```
150+
151+
```php
152+
array:2 [▼ // EragLaravelPwa/src/Core/PWA.php:19
153+
"_token" => "iKbZh21VsYZMpNd9TN12Ul5SoysQzkMXlQkhB5Ub"
154+
"logo" => Illuminate\Http\UploadedFile{#1426 ▶}]
155+
```
156+
157+
Once uploaded, the new logo will be available at `http://yourdomain.com/logo.png`.
158+
159+
116160
## Screenshots 📸
117161

118162
<img width="1470" alt="Screenshot 2024-09-19 at 10 11 01 PM" src="https://github.com/user-attachments/assets/27c08862-0557-4fbd-bd8f-90b9d05f67b3">

src/Core/PWA.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace EragLaravelPwa\Core;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Validation\ValidationException;
7+
8+
class PWA
9+
{
10+
/**
11+
* Process the uploaded logo.
12+
*/
13+
public static function processLogo(Request $request): array
14+
{
15+
try {
16+
$request->validate([
17+
'logo' => 'required|image|mimes:png|dimensions:min_width=512,min_height=512|max:1024',
18+
]);
19+
20+
$destinationPath = public_path('logo.png');
21+
22+
if (file_exists($destinationPath)) {
23+
unlink($destinationPath);
24+
}
25+
26+
$request->file('logo')->move(public_path(), 'logo.png');
27+
28+
return [
29+
'status' => true,
30+
'message' => 'Logo updated successfully!',
31+
'path' => asset('logo.png'),
32+
];
33+
34+
} catch (ValidationException $e) {
35+
return [
36+
'status' => false,
37+
'errors' => $e->errors(),
38+
];
39+
40+
} catch (\Exception $e) {
41+
return [
42+
'status' => false,
43+
'error' => 'Something went wrong. Please try again.',
44+
];
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)