Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service not found #9

Closed
seddighi78 opened this issue Sep 5, 2018 · 12 comments
Closed

Service not found #9

seddighi78 opened this issue Sep 5, 2018 · 12 comments

Comments

@seddighi78
Copy link

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
Copy link
Author

ping @jeromegamez

@jeromegamez
Copy link
Member

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
Copy link
Author

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

@jeromegamez
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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

@seddighi78
Copy link
Author

Still not :(

@nunocodex
Copy link

nunocodex commented Mar 17, 2019

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): symfony/symfony-docs#9926

Fix it with:

  • create AppController inside Controller:
<?php

namespace App\Controller;

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

/**
 * Class AppController
 * @package App\Controller
 */
class AppController extends AbstractController
{
    /**
     * @return array
     */
    public static function getSubscribedServices(): array
    {
        return array_merge(parent::getSubscribedServices(), [
            'kreait_firebase.default' => Firebase::class,
            'firebase' => Firebase::class,
        ]);
    }
}

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

@jeromegamez
Copy link
Member

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

@Emad-Hamza
Copy link

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"."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants