我创建了这个控制器
<?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()
);
}
}
Run Code Online (Sandbox Code Playgroud)
GetDataServiceInterface然后我在 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.
# …Run Code Online (Sandbox Code Playgroud)