50 lines
2.1 KiB
PHP
50 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
// AppServiceProvider — Service Provider principale dell'applicazione
|
|
//
|
|
// Cos'è un Service Provider?
|
|
// È una classe che viene caricata all'avvio di Laravel. Serve per:
|
|
// - Registrare binding nel Service Container (IoC container)
|
|
// - Eseguire codice di inizializzazione
|
|
// - Condividere dati con tutte le view
|
|
//
|
|
// register(): invocato PRIMA del boot. Registra binding nel container.
|
|
// boot(): invocato DOPO tutti i register. Qui tutto è disponibile.
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
use App\Services\SettingService;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
public function register(): void
|
|
{
|
|
// Registra SettingService come Singleton:
|
|
// una sola istanza per tutta la durata della richiesta.
|
|
// Questo evita di creare più istanze (e più connessioni Redis)
|
|
$this->app->singleton(SettingService::class, fn () => new SettingService());
|
|
}
|
|
|
|
public function boot(): void
|
|
{
|
|
// Condivide le impostazioni di base con TUTTE le view Blade.
|
|
// Così nelle view puoi usare $appSettings senza doverta passare
|
|
// esplicitamente da ogni controller.
|
|
View::composer('*', function ($view) {
|
|
/** @var SettingService $settings */
|
|
$settings = app(SettingService::class);
|
|
|
|
$view->with('appSettings', [
|
|
'company_name' => $settings->get('company_name'),
|
|
'currency_symbol' => $settings->get('currency_symbol'),
|
|
'theme_color' => $settings->get('theme_color'),
|
|
'support_email' => $settings->get('support_email'),
|
|
]);
|
|
});
|
|
}
|
|
}
|