-1

Hello i'm trying to use dependency injection outside of controller but i always have an error message that say : Too few arguments to function (...), 0 passed in (...) and exactly 1 expected.

Here is my code :

<?php

namespace App\Response;

use Psr\Log\LoggerInterface;

class TestResponse
{
  public function __construct(private LoggerInterface $logger)
  {}
}

Normally with autowiring it sould work but here no ... I don't understand why

i've try to inject dependency as parameter like in controllers but this not working The autowiring is enabled I think is natively possible like in controllers, without adding new package to do that

Help me

Overlock
  • 21
  • 2

1 Answers1

1

I found the solution ! i post here to help on the same problem

Before my controller is like that : This block trigger the error i said before

  #[Route('/test', name: 'test', methods: ['GET'])]
  public function test(): JsonResponse
  {
    $response = new TestResponse();
    return new JsonResponse([]);
  }

I forget how to implement service : This is the good way how to implement this

  #[Route('/test', name: 'test', methods: ['GET'])]
  public function test(TestResponse $response): JsonResponse
  {
    // $response->...
    return new JsonResponse([]);
  }
Overlock
  • 21
  • 2