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.