kreait/firebase-php
Unofficial Firebase Admin SDK for PHP. Manage authentication, users, custom tokens, and verify ID tokens; send Cloud Messaging notifications; work with Realtime Database, Cloud Storage, and Remote Config. Built on Google APIs with Laravel-friendly support.
composer require kreait/firebase-php. Ensure you’re using PHP 8.0+ and have ext-json enabled.config/services.php, add:
'firebase' => [
'credential' => env('FIREBASE_CREDENTIAL', storage_path('app/firebase-admin.json')),
'database_uri' => env('FIREBASE_DATABASE_URL', 'https://your-project-id.firebaseio.com'),
'storage_bucket' => env('FIREBASE_STORAGE_BUCKET', 'your-project-id.appspot.com'),
],
Or use Laravel’s config()->set() dynamically for flexibility.$firebase = (new Factory)->withServiceAccount(config('services.firebase.credential'));
$auth = $firebase->getAuth();
$user = $auth->getUserByEmail('user@example.com');
Start with auth or FCM—they’re the most common day-to-day needs.Firebase instance in a service provider (e.g., FirebaseServiceProvider) for consistent access:
$this->app->singleton(\Kreait\Firebase\Factory::class, function () {
return (new \Kreait\Firebase\Factory)
->withServiceAccount(config('services.firebase.credential'))
->withDatabaseUri(config('services.firebase.database_uri'));
});
FirestoreUserRepository—to decouple business logic from SDK calls. Use DocumentReference and CollectionReference for type-safe queries.SendFirebaseNotification) for reliability. Leverage topic messaging for broadcast ($messaging->sendToTopic('news', $message)) and token-based for targeted sends.FIREBASE_EMULATOR_HOST, FIREBASE_DATABASE_EMULATOR_HOST) and override Factory initialization in testing environment to run integration tests without cost.$auth->verifyIdToken($token) in API middleware to validate Firebase ID tokens and hydrate the user into the request.storage_path() or environment-specific paths; rotate credentials if compromised.Google\Type\DateTime objects—always convert to native PHP DateTimeImmutable with ->toDateTime() before storage or serialization to avoid JSON encoding surprises.Kreait\Firebase\Exception\InvalidArgumentException, Unavailable, NotFound) to handle partial failures—especially during batch operations (e.g., bulk FCM sends where some tokens may be invalid).$factory = (new Factory)->withHttpOptions(['retries' => 3]);
finfo_open() and proper file MIME detection. Use StorageObject::upload($filePath, ['predefinedAcl' => 'publicRead']) for simplicity—but avoid public ACLs in production.\Kreait\Firebase\Auth)—not low-level HTTP calls. Use Factory::withServiceAccount() to inject test credentials (e.g., mock service accounts) for fast, isolated tests.How can I help you explore Laravel packages today?