I created this controller
<?php
namespace App\Controller;
use App\Interface\GetDataServiceInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/api')]
class ApiController
{
private GetDataServiceInterface $getDataService;
public function __construct(GetDataServiceInterface $getDataService)
{
$this->getDataService = $getDataService;
}
#[Route('/products', name: 'products', methods: ['GET'])]
public function products(): Response
{
return new Response(
$this->getDataService->getData()
);
}
}
then I setted the autowiring for GetDataServiceInterface
on services.yml
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
App\Service\GetJsonDataService: ~
App\Interface\GetDataServiceInterface: '@App\Services\GetJsonDataService'
this is the interface
<?php
namespace App\Interface;
interface GetDataServiceInterface
{
public function getData():string;
}
and the service
<?php
namespace App\Service;
use App\Interface\GetDataServiceInterface;
class GetJsonDataService implements GetDataServiceInterface
{
public function getData():string
{
return getcwd();
}
}
but now I get this error when I try to make a request
The controller for URI "/api/products" is not callable: Controller "App\Controller\ApiController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?
I'm not sure what else I have to set
CodePudding user response:
Your controller does not extends the AbstractController
so you have to manually tag it as controller.service_arguements
in services.yaml