PHPackages                             devinet/firebase - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Admin Panels](/categories/admin)
4. /
5. devinet/firebase

ActiveLibrary[Admin Panels](/categories/admin)

devinet/firebase
================

Firebase integration for CodeIgniter 4

1.6.0(2y ago)0183MITPHPPHP ^7.4 || ^8.0

Since Dec 14Pushed 2y agoCompare

[ Source](https://github.com/llKoull/codeigniter4-firebase)[ Packagist](https://packagist.org/packages/devinet/firebase)[ Docs](https://github.com/llKoull/codeigniter4-firebase)[ RSS](/packages/devinet-firebase/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependencies (6)Versions (34)Used By (0)

Devinet\\Firebase
=================

[](#devinetfirebase)

Firebase integration for CodeIgniter 4

[![](https://github.com/tattersoftware/codeigniter4-firebase/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-firebase/actions/workflows/phpunit.yml)[![](https://github.com/tattersoftware/codeigniter4-firebase/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-firebase/actions/workflows/phpstan.yml)[![](https://github.com/tattersoftware/codeigniter4-firebase/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-firebase/actions/workflows/deptrac.yml)[![Coverage Status](https://camo.githubusercontent.com/1d3e822402f8fd4259b358a1febcd02c04cfe448d00a4888b7348aff479a8b25/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f746174746572736f6674776172652f636f646569676e69746572342d66697265626173652f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/tattersoftware/codeigniter4-firebase?branch=develop)

Quick Start
-----------

[](#quick-start)

1. Install with Composer: `> composer require devinet/firebase`
2. Edit **.env** and add the path to your Firebase credentials: `GOOGLE_APPLICATION_CREDENTIALS = ../credentials/keyfile.json`
3. Access components via the service: `$authentication = service('firebase')->auth;`
4. Use the Firestore `Collection` and `Entity` to model your data: `$widget = collection(WidgetCollection::class)->get($widgetId);`

Description
-----------

[](#description)

This is a CodeIgniter 4 integration of `Kreait\Firebase`, the "Unofficial Firebase Admin SDK for PHP":

- [Documentation](https://firebase-php.readthedocs.io/)
- [GitHub repository](https://github.com/kreait/firebase-php)

It provides a convenience service and custom Firestore classes to use your Firebase project within CodeIgniter 4. Please pay attention to the requirements of the underlying services:

- [kreait\\firebase-php](https://firebase-php.readthedocs.io/en/stable/overview.html#requirements)
- [google\\cloud-firestore](https://firebase-php.readthedocs.io/en/stable/cloud-firestore.html)

Notably, you must have the [gRPC PHP extension](https://github.com/grpc/grpc/tree/master/src/php)and the credentials file to a [Service Account](https://firebase.google.com/docs/admin/setup#add_firebase_to_your_app)with the *Project -&gt; Editor* or *Project -&gt; Owner* role.

Installation
------------

[](#installation)

Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities and always be up-to-date:

```
composer require devinet/firebase
```

Or, install manually by downloading the source files and adding the directory to **app/Config/Autoload.php**.

> Note: As of February 5, 2022 this library fully supports PHP 8.1, however Google's Protobuf has an incompatibility (hopefully fixed soon: [protocolbuffers/protobuf#9293](https://github.com/protocolbuffers/protobuf/issues/9293)).

Credentials
-----------

[](#credentials)

You must provide a key file with your application's service account credentials. The standard way to do this is to add **keyfile.json** to your project and edit **.env** to its path (relative to **public/**):

```
GOOGLE_APPLICATION_CREDENTIALS = ../keyfile.json

```

> *WARNING* Make sure you exclude the key file from any repository updates!

To generate a key file from your Firebase project:

1. Firebase Project Home
2. Project Settings (gear)
3. Service Accounts
4. Firebase Admin SDK
5. Generate new private key

For more info on acquiring credentials see the [Firestore Quick Start Guide](https://firebase.google.com/docs/firestore/quickstart)

For more information on credential specification see the [SDK setup docs](https://firebase-php.readthedocs.io/en/stable/setup.html)

Usage
-----

[](#usage)

Load the Firebase service:

```
$firebase = service('firebase');
```

The service will handle creating and caching each component as you need them. Access components by their name:

```
$storage = $firebase->storage;
$bucket  = $storage->getBucket('my-bucket');
```

You can also use the service to access all the functions of `Kreait\Firebase\Factory`directly, for example if you wanted a separate component instance:

```
$shareClient = $firebase->auth;
$altClient   = $firebase->createAuth();
```

See the [SDK docs](https://firebase-php.readthedocs.io/en/stable/index.html) for a list of supported components. Available at the time of this writing:

- Auth
- Database
- Firestore
- Messaging
- RemoteConfig
- Storage
- *Caller*

Caller
------

[](#caller)

While not yet officially supported by the Firebase SDK, this library includes a component for Firebase callable functions. A simple example shows all its features:

```
// Get the component
$caller = service('firebase')->caller;

// Set the UID of the user making the call
$caller->setUid($user->uid);

// Make the call
$data     = ['customerId' => 7, 'charge' => 3.50];
$response = $caller->call('https://us-central1-myproject.cloudfunctions.net/addCharge', $data);

if ($response === null) {
    echo implode(' ', $caller->getErrors());
}
else {
    print_r($response);
}
```

Firestore
---------

[](#firestore)

This library provides access to the Firestore database directly via `FirestoreClient`. Use the helper function for direct access to a shared instance of the client:

```
helper('firestore');

$document = firestore()->collection('users')->document('lovelace');
$document->set([
    'first' => 'Ada',
    'last' => 'Lovelace',
    'born' => 1815
]);

printf('Added data to the "lovelace" document in the users collection.');
```

Since the SDK and Google classes already represent a full database implementation there is no need for a framework database layer. You can interact directly with the Firestore classes according to [Google's User Guide](https://googleapis.github.io/google-cloud-php/#/docs/cloud-firestore/latest).

### Collection

[](#collection)

The `Collection` class is inspired by the framework's `Model` and handles most of the pre- and post-processing that developers are used to. All you need to supply is the name of the collection and the Entity type to use:

```
