小编a_d*_*v85的帖子

无法从容器中获取控制器,因为它是私有的。您是否忘记用“controller.service_arguments”标记服务?

我创建了这个控制器

<?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)

php symfony symfony6

5
推荐指数
1
解决办法
4453
查看次数

标签 统计

php ×1

symfony ×1

symfony6 ×1