PHP code example of rawand201 / filament-spatie-lighthouse
1. Go to this page and download the library: Download rawand201/filament-spatie-lighthouse library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rawand201 / filament-spatie-lighthouse example snippets
use FilamentSpatieLighthouse\FilamentSpatieLighthousePlugin;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(FilamentSpatieLighthousePlugin::make());
}
FilamentSpatieLighthousePlugin::make()
->navigationGroup('Tools') // set or override the navigation group
->navigationLabel('Lighthouse') // set the nav item label
->navigationIcon('heroicon-o-bolt') // set the nav item icon
->navigationSort(5) // set the sort order
->authorize(fn (): bool => auth()->user()->can('view lighthouse'));
// Where to store audit metadata (scores, timestamps, etc.)
// Options: 'database' (default) | 'cache'
'result_store' => env('LIGHTHOUSE_RESULT_STORE', 'database'),
// Options: 'database' (default) | 'filesystem'
'raw_results_driver' => env('LIGHTHOUSE_RAW_RESULTS_DRIVER', 'database'),
// Laravel disk to use when driver is 'filesystem'
'raw_results_disk' => env('LIGHTHOUSE_RAW_RESULTS_DISK', 'local'),
// Directory within the disk
'raw_results_path' => env('LIGHTHOUSE_RAW_RESULTS_PATH', 'lighthouse-results'),
'database' => [
// Use a specific DB connection (null = app default)
'connection' => env('LIGHTHOUSE_DB_CONNECTION', null),
],
// TTL in seconds when using the 'cache' result store
'cache_ttl' => env('LIGHTHOUSE_CACHE_TTL', 86400), // 24 hours
// Lighthouse audit timeout in seconds
// When running synchronously, PHP's max_execution_time must be higher than this value
'default_timeout' => env('LIGHTHOUSE_TIMEOUT', 180),
// Auto-prune records older than N days (database store only, uses MassPrunable)
// Run: php artisan model:prune --model=FilamentSpatieLighthouse\\Models\\LighthouseAuditResult
'keep_history_for_days' => env('LIGHTHOUSE_KEEP_HISTORY_DAYS', 30),
use FilamentSpatieLighthouse\Events\AuditEndedEvent;
use FilamentSpatieLighthouse\Events\AuditFailedEvent;
use FilamentSpatieLighthouse\Events\AuditStartingEvent;
// In EventServiceProvider or using #[AsEventListener]:
Event::listen(AuditEndedEvent::class, function (AuditEndedEvent $event) {
// $event->url — the audited URL
// $event->scores — ['performance' => 87, 'accessibility' => 100, ...]
// $event->userId — nullable string, set when audit was triggered by a user
});
Event::listen(AuditFailedEvent::class, function (AuditFailedEvent $event) {
// $event->url
// $event->exception
// $event->userId
});
Event::listen(AuditStartingEvent::class, function (AuditStartingEvent $event) {
// $event->url
// $event->categories — array of category strings
// $event->formFactor — 'desktop' | 'mobile'
// $event->userId
});
use FilamentSpatieLighthouse\ResultStores\ResultStore;
use FilamentSpatieLighthouse\ResultStores\StoredAuditResults\StoredAuditResults;
use Spatie\Lighthouse\LighthouseResult;
class RedisResultStore implements ResultStore
{
public function save(string $url, LighthouseResult $result): void
{
// persist $result->rawResults(), $result->scores(), etc.
}
public function latestResults(?string $url = null): ?StoredAuditResults
{
// return a StoredAuditResults instance or null
return new StoredAuditResults(
url: $url,
finishedAt: $finishedAt,
rawResults: $rawResults,
scores: $scores,
);
}
public function getHistory(?string $url = null, int $limit = 10): array
{
// return an array of result arrays
return [];
}
}
use FilamentSpatieLighthouse\ResultStores\ResultStore;
$this->app->singleton(ResultStore::class, RedisResultStore::class);
new StoredAuditResults(
url: string,
finishedAt: ?Carbon,
rawResults: array, // full Lighthouse JSON, keyed by audit id
scores: array, // ['performance' => 87, 'accessibility' => 100, ...]
rawResultPath: ?string, // filesystem path, or null if stored inline
);
namespace App\Filament\Pages;
use FilamentSpatieLighthouse\Pages\LighthouseResults as BaseLighthouseResults;
class LighthouseResults extends BaseLighthouseResults
{
// Override table columns, actions, header widgets, etc.
}