# Connecting with firebase database ( not JWT )

**URL:** <https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070>\
**Category:** Questions\
**Created:** [April 20, 2020, 10:44am UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070 "2020-04-20T10:44:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldp.edo98](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@ldp.edo98](https://discourse.slimframework.com/u/ldp.edo98)\
**Post date:** [April 20, 2020, 10:44am UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/1 "2020-04-20T10:44:59Z")

</div>

I need help with connecting my firebase database with slim framework. I tried using kreait library for firebase, it works only with default php. I need to use slim routes to access my database. Any help is appreciated. Thank you

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 20, 2020, 7:18pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/2 "2020-04-20T19:18:06Z")

</div>

Hi @ldp.edo98

> I tried using kreait library for firebase, it works only with default php.

There is no built-in database or firebase support in Slim. But it should work if you install the `kreait/firebase-php` component, configure the firebase credentials (json) and then add a container definition for `\Kreait\Firebase\Factory::class`. As soon as you have the Factory instance, you can authenticate your requests to Firebase with a Service Account.

---

<div class="post-metadata">

**Author:** ![ldp.edo98](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@ldp.edo98](https://discourse.slimframework.com/u/ldp.edo98)\
**Post date:** [April 21, 2020, 1:25am UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/3 "2020-04-21T01:25:53Z")

</div>

Can you give me an example of the code? I don’t understand. Configure firebase credentials where to be specific? Container definition for kreait factory class. I already installed kreait library, it works fine when i run it directly from another file of php, but i can’t run it when i call it from routes.

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 21, 2020, 9:42am UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/4 "2020-04-21T09:42:35Z")

</div>

> but i can’t run it when i call it from routes.

You should give more details like a code snippet, an error messages or something like this.

---

<div class="post-metadata">

**Author:** ![ldp.edo98](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@ldp.edo98](https://discourse.slimframework.com/u/ldp.edo98)\
**Post date:** [April 21, 2020, 1:26pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/5 "2020-04-21T13:26:25Z")

</div>

this is my routes.php

```php
<?php

use Slim\App;

use Slim\Http\Request;

use Slim\Http\Response;

use Kreait\Firebase\Factory;

use Kreait\Firebase\ServiceAccount;

use OurOwnNameSpace\testClass as test;

return function (App $app) {

$container = $app->getContainer();

$app->get("/edo", function (Request $request, Response $response){

return test::get(1);

});

};

```

this is my working outside class, this class works when i call it directly from localhost using var\_dump

```php
<?php

use Psr\Http\Message\ResponseInterface as Response;

use Psr\Http\Message\ServerRequestInterface as Request;

use Slim\Factory\AppFactory;

use Kreait\Firebase\Factory;

use Kreait\Firebase\ServiceAccount;

require __DIR__. '/../vendor/autoload.php';

class Users {

protected $database;

protected $dbname='users';

public function __construct(){

$acc=ServiceAccount::fromJsonFile( __DIR__. '\secret\tugasakhir-273202-6ee1f9786c82.json');

$firebase=(new Factory)->withServiceAccount($acc)->create();

$this->database=$firebase->getDatabase();

}

public function get(int $userID = NULL){

if (empty($userID) || !isset($userID)) { return false; }

if ($this->database->getReference($this->dbname)->getSnapshot()->hasChild($userID)){

return $this->database->getReference($this->dbname)->getChild($userID)->getValue();

} else {

return false;

}

}

public function insert(array $data){

if (empty($data) || !isset($data)) { return false; }

foreach ($data as $key => $value){

$this->database->getReference()->getChild($this->dbname)->getChild($key)->set($value);

}

return true;

}

public function delete(int $userID){

if (empty($userID) || !isset($userID)) { return false; }

if ($this->database->getReference($this->dbname)->getSnapshot()->hasChild($userID)){

$this->database->getReference($this->dbname)->getChild($userID)->remove();

return true;

} else {

return false;

}

}

}

//$users=new Users();

// var_dump($users->insert([

// '1'=>"John",

// '2'=>"Leo"

// ]));

//var_dump($users->get(2));

```

this is my testClass which i want to call from routes.php, i try to use function get but it shows error

```php
<?php

namespace OurOwnNameSpace;

class testClass {

protected $database;

protected $dbname='users';

public function __construct() {

$acc=ServiceAccount::fromJsonFile( __DIR__. '\secret\tugasakhir-273202-6ee1f9786c82.json');

$firebase=(new Factory)->withServiceAccount($acc)->create();

$this->database=$firebase->getDatabase();

}

public function get(int $userID = NULL){

if (empty($userID) || !isset($userID)) { return false; }

if ($this->database->getReference($this->dbname)->getSnapshot()->hasChild($userID)){

return $database->getReference($this->dbname)->getChild($userID)->getValue();

} else {

return false;

}

}

public function testing() {

return 'Happy Test';

}

}

```

this is the error that shows when i run my slim app

# Slim Application Error

The application could not run because of the following error:

## Details

**Type:** Error

**Message:** Using $this when not in object context

**File:** C:\xampp\htdocs\Service\_TA\src\class\testClass.php

**Line:** 18

## Trace

```auto
#0 C:\xampp\htdocs\Service_TA\src\routes.php(14): OurOwnNameSpace\testClass::get(1)
#1 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#2 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(40): call_user_func(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\Route.php(281): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#4 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\Route.php(268): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\App.php(503): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#8 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\App.php(392): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#9 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\App.php(297): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#10 C:\xampp\htdocs\Service_TA\public\index.php(46): Slim\App->run()
#11 {main}

```

thank you very much for helping me

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 21, 2020, 4:50pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/6 "2020-04-21T16:50:19Z")

</div>

Sorry, it’s not readable 🙂

You can format source code in this tags:

````
```php
// source code goes here
```

````

> **[adam-p/markdown-here](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code)**
>
> Google Chrome, Firefox, and Thunderbird extension that lets you write email in Markdown and render it before sending. - adam-p/markdown-here

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [April 21, 2020, 5:39pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/7 "2020-04-21T17:39:12Z")

</div>

(I’ve applied the formatting to that post.)

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 21, 2020, 10:25pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/8 "2020-04-21T22:25:53Z")

</div>

> **Message:** Using $this when not in object context

That’s a PHP specific issue.

Here you invoke a non static method like a static method: `return test::get(1);`

But in your `testClass` the method `get($id)` is not static.

Instead you should create an instance of “testClass” first, e.g.

```php
$app->get("/edo", function (Request $request, Response $response) {
    $test = new \OurOwnNameSpace\testClass();
    $result = $test->get(1);

    $response = $response->withHeader('Content-Type', 'application/json');
    $response->getBody()->write((string)json_encode($result));

    return $response;
}

```

---

<div class="post-metadata">

**Author:** ![ldp.edo98](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@ldp.edo98](https://discourse.slimframework.com/u/ldp.edo98)\
**Post date:** [April 22, 2020, 11:23am UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/9 "2020-04-22T11:23:16Z")

</div>

i did what you told me to do. i copied your code to my routes.php. it shows this error. is it error within the library? i think it shouldn’t be OurOwnNameSpace\ServiceAccount. but i don’t know where the problem is

# Slim Application Error

The application could not run because of the following error:

## Details

**Type:** Error

**Message:** Class ‘OurOwnNameSpace\ServiceAccount’ not found

**File:** C:\xampp\htdocs\Service\_TA\src\class\testClass.php

**Line:** 16

## Trace

```auto
#0 C:\xampp\htdocs\Service_TA\src\routes.php(15): OurOwnNameSpace\testClass->__construct()
#1 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#2 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(40): call_user_func(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\Route.php(281): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#4 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\Route.php(268): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\App.php(503): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#8 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\App.php(392): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#9 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\App.php(297): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#10 C:\xampp\htdocs\Service_TA\public\index.php(46): Slim\App->run()
#11 {main}

```

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 22, 2020, 12:31pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/10 "2020-04-22T12:31:07Z")

</div>

> . is it error within the library?

No, Slim works. Your composer autoloader is just not configured properly. You should fix that first. Read about composer and PSR-4. You can also find some explanations for Slim 4 [here](https://odan.github.io/2019/11/05/slim4-tutorial.html).

---

<div class="post-metadata">

**Author:** ![ldp.edo98](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@ldp.edo98](https://discourse.slimframework.com/u/ldp.edo98)\
**Post date:** [April 22, 2020, 2:01pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/11 "2020-04-22T14:01:55Z")

</div>

i already dump my autoload.  
this is my composer.json  
i can’t use " ``` " tag for inserting code  
so i just upload my screenshot. i don’t think

 ![image.png](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/b/b3f02736d8e7b670a7146739794176c9fffe3ef5.png)

i don’t understand at all

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 22, 2020, 2:14pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/12 "2020-04-22T14:14:32Z")

</div>

The “autoload” section is missing in your composer.json file. Separate the default namespaces from the dev namespaces. Add an application specific namespace e.g. `App` that points to `/src`. The src/ path is for all your PHP classes and sub-namespaces under `\App\*`.

Here is an example:

```json
"autoload": {
    "psr-4": {
        "App\\": "src"
    }
},
"autoload-dev": {
    "psr-4": {
        "App\\Test\\": "tests"
    }
}

```

Move the file `testClass.php` to `src/`.  
Rename the file `testClass.php` to `TestClass.php`  
Rename the class `testClass` to `TestClass`  
Change the namespace `OurOwnNameSpace` to `App`.  
Update all references to the new TestClass (e.g. in your routes)  
Run composer update.  
Try it again.

Read more about it: [https://odan.github.io/2019/11/05/slim4-tutorial.html#psr-4-autoloading](https://odan.github.io/2019/11/05/slim4-tutorial.html#psr-4-autoloading)

---

<div class="post-metadata">

**Author:** ![ldp.edo98](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@ldp.edo98](https://discourse.slimframework.com/u/ldp.edo98)\
**Post date:** [April 22, 2020, 2:55pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/13 "2020-04-22T14:55:44Z")

</div>

i followed your tutorial at that link. still shows error. first i followed your email, it shows the same error with before. Then i try to follow at your link, it shows another error but i think it’s quite the same.

# Slim Application Error

The application could not run because of the following error:

## Details

**Type:** RuntimeException

**Message:** Callable App\Src\TestClass does not exist

**File:** C:\xampp\htdocs\Service\_TA\vendor\slim\slim\Slim\CallableResolver.php

**Line:** 99

## Trace

```auto
#0 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\CallableResolver.php(58): Slim\CallableResolver->resolveCallable('App\\Src\\TestCla...', '__invoke')
#1 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\CallableResolverAwareTrait.php(41): Slim\CallableResolver->resolve('App\\Src\\TestCla...')
#2 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\Route.php(276): Slim\Routable->resolveCallable('App\\Src\\TestCla...')
#3 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#4 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\Route.php(268): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\App.php(503): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\App.php(392): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#8 C:\xampp\htdocs\Service_TA\vendor\slim\slim\Slim\App.php(297): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#9 C:\xampp\htdocs\Service_TA\public\index.php(46): Slim\App->run()
#10 {main}

```

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 22, 2020, 3:41pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/14 "2020-04-22T15:41:17Z")

</div>

The src/ directory is the root directory for the App namespace. “Src” must not be part of a namespace. In your case it should be `App\TestClass` (not `App\Src\TestClass`). Please note that it’s just an example.

[https://getcomposer.org/doc/01-basic-usage.md#autoloading](https://getcomposer.org/doc/01-basic-usage.md#autoloading)

---

<div class="post-metadata">

**Author:** ![ldp.edo98](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@ldp.edo98](https://discourse.slimframework.com/u/ldp.edo98)\
**Post date:** [April 22, 2020, 4:46pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/15 "2020-04-22T16:46:03Z")

</div>

App\TestClass doesn’t exist

---

<div class="post-metadata">

**Author:** ![ldp.edo98](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@ldp.edo98](https://discourse.slimframework.com/u/ldp.edo98)\
**Post date:** [April 22, 2020, 4:46pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/16 "2020-04-22T16:46:37Z")

</div>

i tried composer update, composer dump-autoload

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 22, 2020, 6:35pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/17 "2020-04-22T18:35:55Z")

</div>

Then better check out a Slim-skeleton project with a working autoloader:

- [https://github.com/slimphp/Slim-Skeleton](https://github.com/slimphp/Slim-Skeleton)
- [https://github.com/odan/slim4-skeleton](https://github.com/odan/slim4-skeleton)
- [https://github.com/chubbyphp/petstore/tree/slim](https://github.com/chubbyphp/petstore/tree/slim)
- [https://github.com/search?q=slim4+skeleton](https://github.com/search?q=slim4+skeleton)

---

<div class="post-metadata">

**Author:** ![ldp.edo98](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@ldp.edo98](https://discourse.slimframework.com/u/ldp.edo98)\
**Post date:** [April 27, 2020, 1:41pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/18 "2020-04-27T13:41:47Z")

</div>

i downloaded a working slim autoload in github, they use slim skeleton or something. i tried modifying it a little bit like this.  
i created a class in file src named Coba.php  
created a simple function

```php
public function print() { return "Printing" }

then at the routes i tried to call it like this

$app->get('/', function (Request $request, Response $response) {

$iniclass=new App\Coba();

$result = $iniclass->print();

$response = $response->withHeader('Content-Type', 'application/json');

$response->getBody()->write((string)json_encode($result));

return $response;

});

```

it shows this error

```auto
{
    "statusCode": 500,
    "error": {
        "type": "SERVER_ERROR",
        "description": "Class 'Slim\\App\\Coba' not found"
    }
}

```

 ![image.png](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/7/75cfe76e05e45614dfcae2383024256f6dc65d94.png)

here is my directory

is there anything i did wrong?

i tried inserting $result with pure string and it works fine, something like “blablabla”

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 27, 2020, 3:31pm UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/19 "2020-04-27T15:31:28Z")

</div>

> [@ldp.edo98](#):
>
> $iniclass=new App\Coba();

Try: `$iniclass = new \App\Coba();`

PS:

1. The code is not readable:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/8/8c1631192a30ca63e433a548ef9e76b39faa3d46.png)

1. You really should learn the basics of PHP classes, namespaces, use statements, PSR-4 autoloader first. 😉

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [April 28, 2020, 11:57am UTC](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070/20 "2020-04-28T11:57:44Z")

</div>

_(I’ve tried to cleanup the initial post as best I could.)_

[Next page](https://discourse.slimframework.com/t/connecting-with-firebase-database-not-jwt/4070.md?page=2)
