kreait / firebase-bundle

A Symfony Bundle for the Firebase PHP Admin SDK
https://github.com/kreait/firebase-php
MIT License
136 stars 26 forks source link

Service not found #9

Closed seddighi78 closed 5 years ago

seddighi78 commented 6 years ago

The problem

When i calling the service kreait_firebase or kreait_firebase.project_name i get:

You have requested a non-existent service "kreait_firebase".

Environment

kreait/firebase-bundle 1.1.1 kreait/firebase-php 4.16.0 kreait/firebase-tokens 1.7.1 kreait/gcp-metadata 1.0.1 php 7.2.7 symfony 4.1.4

Code to reproduce issue

/**
     * @Route("/test")
     */
    public function test()
    {
        $firebase = $this->get('kreait_firebase');
        $title = 'test title';
        $body = 'test body';
        $token = 'my_foo_token';
        $notification = Notification::create($title, $body);
        $message = CloudMessage::withTarget('token', $token);
        $message->withNotification($notification);
        dd($firebase->getMessaging()->send($message));
    }
seddighi78 commented 6 years ago

ping @jeromegamez

jeromegamez commented 6 years ago

I already got the notification when you created the issue, no need to ping me one minute after :).

This looks as if the Firebase bundle is not configured for your test environment. Please make sure that it either is configured separately, or that the test config imports the config file in which the bundle is configured.

seddighi78 commented 6 years ago

Oh I'm so sorry - but I'm not in test env my app env is on dev

jeromegamez commented 6 years ago

Ah, I misinterpreted the environment because of the method name, my bad!

Hm... you could try debugging the service container and/or the configuration with the respective Symfony commands.

Besides that, I have no clue without seeing the whole project (but don‘t show it to me 😅)

Have you seen https://github.com/jeromegamez/firebase-php-examples ? You could compare your project/settings to this and see if you spot some differences.

seddighi78 commented 6 years ago

Yes I see the example but it's different from the library documents my configuration file is:

# %kernel.project_dir%/config/packages/firebase/firebase.yaml
kreait_firebase:
    projects:
        default:
            credentials: '%kernel.project_dir%/config/packages/firebase/service_account_key.json'
            public: true
            alias: 'default'
            default: true
jeromegamez commented 6 years ago

Based on the configuration you should be able to get kreait_firebase.default from the container or (cleaner) inject Kreait\Firebase as a method dependency.

If this doesn’t work, please debug your container with bin/console debug:container

seddighi78 commented 6 years ago

When I run the command for debugging and search for firebase i get this result:

$ bin/console debug:container firebase

 Select one of the following services to display its information:
  [0] Kreait\Firebase\Factory
  [1] Kreait\Firebase\Symfony\Bundle\DependencyInjection\Factory\ProjectFactory
 > 
jeromegamez commented 6 years ago

And if you try $this->get('default');, does that work?

seddighi78 commented 6 years ago

Still not :(

nunocodex commented 5 years ago

The issue come from the controller extend class, if you use the deprecated Controller it's work, but with the latest SF4 we need to extend the AbstractController and with this not working.

Reference to fix on latest SF (put it in the repo doc): https://github.com/symfony/symfony-docs/issues/9926

Fix it with:

namespace App\Controller;

use Kreait\Firebase; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**

Now if you extend the new AbstractController it's work, remember to change the key related to your services name's.

jeromegamez commented 5 years ago

As this issue has become stale, I'm closing it now, but please feel free to reopen it.

Emad-Hamza commented 4 years ago

Still can't inject Kreait\Firebase. Using @nunocodex's proposal returns: "The service "App\Controller\AppController" has a dependency on a non-existent service "Kreait\Firebase"."