1

I'm new in Symfony, I try to use Dependency injection to get User in a service (i think)

services.yaml :

App\Service\Test\RESTAuthenticatedService:
         calls:
             - method: getTrigramme
               arguments:
                   - '@security.token_storage'

In my RESTAuthenticatedService.php :

namespace App\Service\Test;
....
class RESTAuthenticatedService extends AbstractController {
protected $session;
private $user;
....
public function getTrigramme(){
     $user = $this->token_storage->getToken()->getUser();
ERROR : 
Notice: Undefined property: App\Service\Test\PrestataireService::$token_storage

Can you help me please ?


Ok, first thanks everyone now I try what you said and i have this error :

Too few arguments to function App\Service\Test\ClientService::__construct(), 0 passed in D:\www\Interface_SAT\src\Controller\RecherchePrestataires.php on line 60 and exactly 2 expected

In my Controller RecherchePrestataires.php I have :

.....
public function rechercher(Request $request) {
....
    $recherchePresta = new PrestataireService();

In the file class PrestataireService I just have :

class PrestataireService extends ClientService { 

In ClientService :

    use Symfony\Component\HttpFoundation\Session\SessionInterface;
    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class ClientService extends RESTAuthenticatedService
{
    public $user;

    public function __construct(SessionInterface $session, TokenStorageInterface $tokenStorage)
    {
        parent::__construct($session, $tokenStorage);
        $this->setSession($session);
    }

And in RESTAuthenticatedService : I've done :

public function __construct(SessionInterface $session, TokenStorageInterface $tokenStorage)
    {
        $this->token_storage = $tokenStorage;

Sorry , but i try so many things.

  • You don't say what the problem with this code is. Make it clear what the expectations are, and what results you get. –  Feb 05 '19 at 14:57
  • Either `@logger` is a `TokenStorage` or `TokenStorage` is a `@logger`, but there is something weird going on there. – Loek Feb 05 '19 at 14:59
  • Sorry it's tokenStorage – Jean-Philippe Jurnet Feb 05 '19 at 15:02
  • 1
    This [question here](https://stackoverflow.com/questions/36870272/how-to-get-the-current-logged-user-in-a-service) has various working examples. In particular look at the fairly new Security helper component. Symfony\Component\Security\Core\Security. Do not try to get the user in the constructor as it may not have set when the service is created. Instead, store the injected Security or TokenStorage object then get the user when you actually need it for something. – Cerad Feb 05 '19 at 15:04
  • I have a hard time following your code. Going from the back, your controller should not call `new PrestataireService`, but get the service either injected into the constructor or your action (at least when you use the default config). In your services.yaml you assign an artificial service id `monservice`. Unless you want to have multiple services with the same class you should use the full class name (with namespace) as service id instead. – dbrumann Feb 05 '19 at 15:11
  • Use the edit button under your question to update it with new code. Comments don't format well. There are quite a few things wrong with your code. Maybe take a step back and try making a simple service and injecting it just to get an idea of how this all works. It can be quite a learning curve. – Cerad Feb 05 '19 at 15:21
  • Ok thank you i did it , can you help me ? – Jean-Philippe Jurnet Feb 05 '19 at 15:30
  • 1
    Not really. Too many details are wrong or missing. You have RESTAuthenticatedService extending from AbstractController which is almost certainly wrong. You are trying to call getTrigramme from your service definition which is wrong. All I can do is to repeat my earlier suggestion which is to start with a simple service. Get it working and understand why it works. Then add to it. The service container documentation (as well as dependency injection) might seem overwhelming at first but you you just need to keep plugging away at it. – Cerad Feb 05 '19 at 16:22
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Mike Doe Feb 05 '19 at 19:44

1 Answers1

2

This seems that you didn't created the constructor in the class.

You have defined at your services.yaml that your class has a dependency, but you haven't done anything with that dependcy. You need to create the constructor and add the dependency as a parameter, and assign to a local variable.

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class RESTAuthenticatedService extends AbstractController

    /**
     * @var TokenStorageInterface
     */
    private $token_storage;

    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $this->token_storage = $tokenStorage;
    }
}

Then you will be able to access $this->token_storage.

Edit: Change your services.yaml to inject the depenency in the constructor.

App\Service\Test\RESTAuthenticatedService:
     class: App\Service\Test\RESTAuthenticatedService
     arguments:
        - '@security.token_storage'
David Rojo
  • 2,337
  • 1
  • 22
  • 44
  • You can skip the class attribute – Alexander Dimitrov Feb 06 '19 at 20:11
  • Then you will have to inject also PrestataireService in your controller, or retrieve it via $this->get(PrestataireService::class), or if you want to create it with new you will have to provide the dependency yourself. The best option is to inject it as a depency as it is the purpose of depency injection, to clearify the depencies of your components. Review also if you are autowiring and autoconfiguring your services https://symfony.com/doc/current/service_container.html#the-autowire-option – David Rojo Feb 07 '19 at 10:03
  • Same results new yml : `App\Service\Test\RESTAuthenticatedService:` `class: App\Service\Test\RESTAuthenticatedService` `arguments:` `- '@session'` `- '@security.token_storage'` App\Service\Test\ClientService: class: App\Service\Test\ClientService arguments: - '@session' - '@security.token_storage' `App\Service\Test\PrestataireService:` `class: App\Service\Test\PrestataireService` `arguments:` `- '@session'` `- '@security.token_storage'` – Jean-Philippe Jurnet Feb 07 '19 at 10:31