0

I am trying to connect my localhost apps to firebase.

The firebase controller works fine with the code below:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Kreait\Firebase;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use Kreait\Firebase\Database;


class FirebaseController extends Controller
{
    protected $database;

    public function __construct()
    {
        $this->database = app('firebase.database');
    }

    public function index()
    {     
        $factory = (new Factory)
            ->withServiceAccount(__DIR__ . env('GOOGLE_APPLICATION_CREDENTIALS'))
            ->withDatabaseUri(env('DATABASE_URI'));


        $database = $factory->createDatabase();

        $newPost = $database
            ->getReference('blog/posts')
            ->push([
                'title' => 'Post title',
                'body' => 'This should probably be longer.'
            ]);


        echo "<pre>";
        print_r($newPost->getvalue());
    }
}

but when I change the environment variable in .env from:

FIREBASE_CREDENTIALS=FirebaseKey.json
GOOGLE_APPLICATION_CREDENTIALS=/FirebaseKey.json

to

FIREBASE_CREDENTIALS=/FirebaseKey.json
GOOGLE_APPLICATION_CREDENTIALS=/FirebaseKey.json

by only added a foward slash at FIREBASE_CREDENTIALS then, suddenly page crack. Error:

Kreait\Firebase\Exception\InvalidArgumentException
Invalid service account: /FirebaseKey.json can not be read:
SplFileObject::__construct(/FirebaseKey.json): Failed to open stream: No such file or directory

I am actually not using the FIREBASE_CREDENTIALS variable in anywhere else, or I shouldn't declaring this variable in .env file?

And also, when I replace the GOOGLE_APPLICATION_CREDENTIALS with FIREBASE_CREDENTIALS and change the value of FIREBASE_CREDENTIALS and GOOGLE_APPLICATION_CREDENTIALS in .env file as show in the below:

FIREBASE_CREDENTIALS=/FirebaseKey.json
GOOGLE_APPLICATION_CREDENTIALS=/FirebaseKey.json

it will display the same error.

Kreait\Firebase\Exception\InvalidArgumentException
Invalid service account: /FirebaseKey.json can not be read: 
SplFileObject::__construct(/FirebaseKey.json): Failed to open stream: No such file or directory

So the question is why using FIREBASE_CREDENTIALS variable, then it cannot read the .json file but when using GOOGLE_APPLICATION_CREDENTIALS everything is fine?

Here is the screenshot of the directory.

Denusklo
  • 35
  • 5
  • Did you try using absolute directories? Also, you should define env info within the config files and use the config helper in Laravel elsewhere. https://stackoverflow.com/questions/40026893/what-is-difference-between-use-envapp-env-configapp-env-or-appenviron – Tim Jul 27 '21 at 06:07
  • Are you trying to access two separate Firebase projects in one application or why are you re-instantiating the factory in the index action? With the Laravel package this should never be necessary. Have a look at https://github.com/kreait/laravel-firebase/issues/96#issuecomment-886030038, this might help. Also https://github.com/kreait/laravel-firebase#usage – jeromegamez Jul 27 '21 at 09:45
  • Also, unless the credentials file is indeed located in the root directory of your filesystem, try removing the leading slash in the file path – jeromegamez Jul 27 '21 at 11:51
  • @jeromegamez Can you provide the resource on how to avoid using re-instantiating because I am quite new to oop php... – Denusklo Jul 29 '21 at 03:19
  • @Denusklo https://firebase-php.readthedocs.io/en/5.x/realtime-database.html#initializing-the-realtime-database-component (the "With Dependency Injection" section). Also, if you'd like, and have more questions, feel free to join the Discord Server linked in the readme of the project - everything is easier than the comments section on Stack Overflow – jeromegamez Jul 31 '21 at 09:16

0 Answers0