Skip to content

Commit bfd59f7

Browse files
committed
create a laravel convert into pwa app
1 parent bff39d6 commit bfd59f7

File tree

8 files changed

+236
-2
lines changed

8 files changed

+236
-2
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ RajTechnologies\Tools\ToolServiceProvider::class,
1818
- Clear Cache
1919
- Routes List
2020
- HTTP Status Code List
21+
- Laravel App Convert To PWA App
2122

2223
## URL Routes
2324

@@ -28,6 +29,36 @@ RajTechnologies\Tools\ToolServiceProvider::class,
2829
| Routes List | routeslist | All Routes List |
2930
| API Routes List | routeslist?only=api | Only API Routes List |
3031

32+
## PWA App
33+
- Open your `config/app.php` and add the following to the `providers` array:
34+
```bash
35+
'LaravelPwa' => \RajTechnologies\Tools\LaravelPwa::class,
36+
```
37+
## Publish the Assets For PWA App
38+
39+
Run the following command to publish config file,
40+
41+
php artisan laravel-pwa:publish
42+
## Configure PWA
43+
44+
Add following code in root blade file in header section.
45+
```php
46+
<!-- PWA -->
47+
<meta name="theme-color" content="#6777ef"/>
48+
<link rel="apple-touch-icon" href="{{ asset('logo.PNG') }}">
49+
<link rel="manifest" href="{{ asset('/manifest.json') }}">
50+
```
51+
Add following code in root blade file in before close the body.
52+
```js
53+
<script src="{{ asset('/sw.js') }}"></script>
54+
<script>
55+
if (!navigator.serviceWorker.controller) {
56+
navigator.serviceWorker.register("/sw.js").then(function (reg) {
57+
console.log("Service worker has been registered for scope: " + reg.scope);
58+
});
59+
}
60+
</script>
61+
```
3162
## Contributing
3263

3364
- [Bhargav Raviya](https://github.com/bhargavraviya)

composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
"name": "rajtechnologies/laravel-tools",
33
"description": "All Type of Base Tools to Helping Development",
44
"type": "library",
5+
"keywords": [
6+
"laravel-clear-cache",
7+
"laravel-routes-view-list",
8+
"laravel-http-status-code-list",
9+
"laravel-pwa",
10+
"pwa-laravel",
11+
"laravel",
12+
"php"
13+
],
514
"license": "MIT",
615
"authors": [
716
{

src/LaravelPwa.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace RajTechnologies\Tools;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class LaravelPwa extends Facade
8+
{
9+
/**
10+
* Get the binding in the IoC container.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'laravel-pwa';
17+
}
18+
}

src/ToolServiceProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace RajTechnologies\Tools;
44

55
use Illuminate\Support\ServiceProvider;
6-
6+
use RajTechnologies\Tools\commands\PWAPublish;
77
class ToolServiceProvider extends ServiceProvider
88
{
99
public function boot(){
@@ -12,7 +12,14 @@ public function boot(){
1212
$this->loadViewsFrom(__DIR__.'/../resources/views', 'Tool');
1313
}
1414
public function register(){
15-
15+
// PWA Starter Kit Start
16+
$this->app->singleton('laravel-pwa:publish', function ($app) {
17+
return new PWAPublish();
18+
});
19+
$this->commands([
20+
'laravel-pwa:publish',
21+
]);
22+
// PWA Starter Kit End
1623
}
1724
}
1825

src/commands/PWAPublish.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace RajTechnologies\Tools\commands;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Illuminate\Console\Command;
7+
8+
class PWAPublish extends Command
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'laravel-pwa:publish';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Publish Service Worker|Offline HTMl|manifest file for PWA application.';
23+
24+
public $composer;
25+
26+
/**
27+
* Create a new command instance.
28+
*/
29+
public function __construct()
30+
{
31+
parent::__construct();
32+
33+
$this->composer = app()['composer'];
34+
}
35+
36+
public function handle()
37+
{
38+
$publicDir = public_path();
39+
40+
$manifestTemplate = file_get_contents(__DIR__.'/../stubs/pwa_app/manifest.stub');
41+
$this->createFile($publicDir. DIRECTORY_SEPARATOR, 'manifest.json', $manifestTemplate);
42+
$this->info('manifest.json file is published.');
43+
44+
$offlineHtmlTemplate = file_get_contents(__DIR__.'/../stubs/pwa_app/offline.stub');
45+
$this->createFile($publicDir. DIRECTORY_SEPARATOR, 'offline.html', $offlineHtmlTemplate);
46+
$this->info('offline.html file is published.');
47+
48+
$swTemplate = file_get_contents(__DIR__.'/../stubs/pwa_app/sw.stub');
49+
$this->createFile($publicDir. DIRECTORY_SEPARATOR, 'sw.js', $swTemplate);
50+
$this->info('sw.js (Service Worker) file is published.');
51+
52+
$this->info('Generating autoload files');
53+
$this->composer->dumpOptimized();
54+
55+
$this->info('Greeting!.. Enjoy PWA site...');
56+
}
57+
58+
public static function createFile($path, $fileName, $contents)
59+
{
60+
if (!file_exists($path)) {
61+
mkdir($path, 0755, true);
62+
}
63+
64+
$path = $path.$fileName;
65+
66+
file_put_contents($path, $contents);
67+
}
68+
}

src/stubs/pwa_app/manifest.stub

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "Laravel PWA APP",
3+
"short_name": "PWA",
4+
"start_url": "/index.php",
5+
"background_color": "#6777ef",
6+
"description": "Laravel PWA APP",
7+
"display": "fullscreen",
8+
"theme_color": "#6777ef",
9+
"icons": [
10+
{
11+
"src": "logo.PNG",
12+
"sizes": "512x512",
13+
"type": "image/png",
14+
"purpose": "any maskable"
15+
}
16+
]
17+
}

src/stubs/pwa_app/offline.stub

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="title" content="InfyBonus">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
9+
<style>
10+
.error_body {
11+
margin: 0;
12+
padding: 0;
13+
box-sizing: border-box;
14+
height: 100vh;
15+
width: 100%;
16+
background: #f9f9f9;
17+
}
18+
19+
.error_container .error_heading {
20+
color: transparent;
21+
font-size: 160px;
22+
margin: 0;
23+
font-weight: 900;
24+
letter-spacing: 20px;
25+
background-size: 100% 100%;
26+
background: linear-gradient(90deg, #6b719b 38%, #9da3cc 53%, #979dce 65%);
27+
-webkit-background-clip: text;
28+
-moz-background-clip: text;
29+
-ms-background-clip: text;
30+
}
31+
32+
@media (max-width: 540px) {
33+
.error_container .error_heading {
34+
font-size: 120px;
35+
letter-spacing: 10px;
36+
}
37+
}
38+
39+
.error_container .error_btn {
40+
background-color: #6e749e !important;
41+
border: none;
42+
outline: none;
43+
}
44+
45+
.error_container .error_btn:focus {
46+
box-shadow: none !important;
47+
}
48+
49+
.error_container .error_message,
50+
.error_container .error_paragraph {
51+
color: #787878;
52+
}
53+
</style>
54+
</head>
55+
<body>
56+
<div class="error_body">
57+
<div
58+
class="container error_container d-flex justify-content-center align-items-center flex-column w-100 h-100 p-5 text-center">
59+
<h2 class="error_message text-center mb-3">Boo! You don't have an internet connection</h2>
60+
<p class="error_paragraph text-center mb-5">
61+
Please check your network connection and try again.
62+
</p>
63+
<a href="/" class="btn btn-primary error_btn">Back to Home Page</a>
64+
</div>
65+
</div>
66+
</body>
67+
</html>

src/stubs/pwa_app/sw.stub

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "Laravel PWA APP",
3+
"short_name": "PWA",
4+
"start_url": "/index.php",
5+
"background_color": "#6777ef",
6+
"description": "Laravel PWA APP",
7+
"display": "fullscreen",
8+
"theme_color": "#6777ef",
9+
"icons": [
10+
{
11+
"src": "logo.PNG",
12+
"sizes": "512x512",
13+
"type": "image/png",
14+
"purpose": "any maskable"
15+
}
16+
]
17+
}

0 commit comments

Comments
 (0)