PHP code example of adrorocker / php-firebase

1. Go to this page and download the library: Download adrorocker/php-firebase library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adrorocker / php-firebase example snippets




use PhpFirebase\Firebase;

// Base endpoint
$base = 'https://hey-123.firebaseio.com/somesubendpoint';

// Auth token
$token = 'a1b2c3d4e5f6g7h8i9';

$firebase = new Firebase($base,$token);

// Unique ID
$id = (new \DateTime())->getTimestamp();

// Set the data (body of the request).
$data = ['key' => 'value']; // The data could be even just a string

// Make a PUT request, the response is return
$put = $firebase->put('/logs/'.$id, $data);

// Make a GET request, the response is return, 
// you will have all the logs in the $get variable 
$get = $firebase->get('/logs');

// app/Model/User/User.php


namespace App\Model\User;

use PhpFirebase\Entities\Entity;

class User extends Entity
{
    protected $id;

    public $firstName;

    public $lastName;
}


// app/Model/User/UserRepository.php


namespace App\Model\User;

use PhpFirebase\Entities\Repository\Repository;

class UserRepository extends Repository
{
    public function __construct()
    {
        // Base endpoint
        $base = 'https://hey-123.firebaseio.com/somesubendpoint';
        // Auth token
        $token = 'a1b2c3d4e5f6g7h8i9';

        $this->class = User::class;

        parent::__construct($base, $token, '/users');
    }
}




$repo =  new UserRepository();
// Create user
$user = new User([
    'id' => 1,
    'firstName' => 'Adro',
    'lastName' => 'Rocker',
]);
$user = $repo->store($user); // $user will be an instance of App\Model\User

// Update user
// You can get or assign values to an entity property using a method named as the property name.
$user->lastName('Rocks'); // setting $lastName to be 'Rocks'.
$lastName = $user->lastName(); // getting $lastName, $lastName has the value 'Rocks'.
$user = $repo->store($user);

// Find user
$user = $repo->find(1); // $user will be an instance of App\Model\User


composer