最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Laravel 11 + Firebase Firestore: Service Account File Not Readable & Server Stops Responding - Stack Overflow

programmeradmin2浏览0评论

i'm working on a Laravel 11 and the PHP 8.2.12 project on Windows 11, using kreait/firebase-php to connect to Firestore. I have a strange issue where calling a specific route that interacts with Firestore causes the Laravel server to completely stop responding. However, all other routes work fine, including Firebase Realtime Database and Firebase Authentication. and im an owner in firestore

What Works

  • ✅ Firebase Authentication works.
  • ✅ Firebase credentials file is accessible.
  • ✅ Other routes unrelated to Firestore work fine.

What Fails

  • ❌ Calling Firestore (/test route) kills the server.

  • ❌ No errors appear in laravel.log.

  • ❌ The browser shows:

    127.0.0.1 refused to connect (ERR_CONNECTION_REFUSED)

Route Definitions (web.php)

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FirebaseController;
use Kreait\Firebase\Factory;

Route::get('/', function () {
    return view('welcome');
});

// Works fine
Route::get('/index', [FirebaseController::class, 'index']);

// This route causes the issue
Route::get('/test', function () {
    try {
        $firebase = (new Factory)->withServiceAccount(base_path(env('FIREBASE_CREDENTIALS')));
        $db = $firebase->createFirestore()->database();
        $collection = $db->collection('Volunteers');
        $documents = $collection->documents();

        if ($documents->isEmpty()) {
            return 'No documents found';
        }

        foreach ($documents as $document) {
            print_r($document->data());
        }
    } catch (\Exception $e) {
        return 'Error: ' . $e->getMessage();
    }
});

// Checks if Firebase credentials file is accessible (Works fine)
Route::get('/check-firebase-key', function () {
    $path = base_path('storage/firebase/firebase_credentials.json');

    if (!file_exists($path)) {
        return response()->json(['error' => 'Firebase credentials file not found!'], 404);
    }

    $content = json_decode(file_get_contents($path), true);
    return response()->json($content);
});

// Checks Firebase Authentication (Works fine)
Route::get('/test-firebase-auth', function () {
    $path = base_path('storage/firebase/firebase_credentials.json');

    if (!file_exists($path)) {
        return response()->json(['error' => 'Firebase credentials file not found!'], 404);
    }

    try {
        $factory = (new Factory())->withServiceAccount($path);
        $auth = $factory->createAuth();
        return response()->json(['message' => 'Firebase Authentication is working!']);
    } catch (AuthException | FirebaseException $e) {
        return response()->json(['error' => $e->getMessage()], 500);
    }
});

Checked the .env file path Tried using the full absolute path:

FIREBASE_CREDENTIALS=C:\Users\asus-pc\newPro\backend\storage\firebase\firebase_credentials.json

The issue still persists.

Used base_path() with withServiceAccount() $firebase = (new Factory)->withServiceAccount(base_path(env('FIREBASE_CREDENTIALS'))); No success.

Verified File Permissions Ensured that the firebase_credentials.json file is readable and has the correct permissions.

Cleared Laravel Cache and Restarted Server:

php artisan config:clear
php artisan cache:clear
php artisan serve

Did not solve the issue.

Confirmed kreait/firebase-php Installation:

composer require kreait/firebase-php
composer update kreait/firebase-php

Everything is up to date. Checked if PHP has grpc enabled (required for Firebase Firestore) and grpc is enabled.

Core Issue: When accessing the /test route, the Laravel server stops responding completely. All other routes work fine. It seems like Laravel cannot correctly read the firebase_credentials.json file, but it also crashes the entire server instead of just throwing an error. What could be causing this issue, and how can I fix it?

发布评论

评论列表(0)

  1. 暂无评论