TKK_E32230908/app/Providers/AppServiceProvider.php

54 lines
1.5 KiB
PHP

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Models\IotMode;
use App\Observers\IotModeObserver;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Request; // <-- Tambahkan ini untuk deteksi host
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
/**
* 1. DINAMIS URL & HTTPS (SOLUSI DUA URL: LOCAL & NGROK)
* Logika ini mendeteksi jika aplikasi dibuka via Ngrok, maka otomatis
* menggunakan HTTPS dan URL Ngrok. Jika dibuka via Localhost, tetap HTTP.
*/
$host = Request::header('host');
if (str_contains($host, 'ngrok-free.dev')) {
// Jika akses lewat Ngrok
URL::forceScheme('https');
URL::forceRootUrl("https://" . $host);
} else {
// Jika akses lewat Localhost / IP 127.0.0.1
URL::forceScheme('http');
// Jika Anda menggunakan port khusus (misal 8000), ini akan otomatis mengikutinya
URL::forceRootUrl("http://" . $host);
}
/**
* 2. REGISTRASI OBSERVER IOT MODE
* Memastikan perubahan status pada tabel IotMode terpantau untuk sinkronisasi hardware
*/
if (class_exists(IotMode::class)) {
IotMode::observe(IotModeObserver::class);
}
}
}