“ Controller ..CategoryController具有必需的构造函数参数,并且在容器中不存在。您是否忘了定义这样的服务?”,

时间:2019-07-02 16:02:17

标签: php symfony symfony4 api-platform.com

我在使用Symfony4和API平台的自定义控制器时遇到麻烦。

  

{“ @context”:“ / api / contexts / Error”,“ @type”:“ hydra:Error”,
  “ hydra:title”:“发生错误”,“ hydra:description”:“控制器   \“ \ App \ Controller \ CategoryController \”具有必需的构造函数   参数,并且在容器中不存在。你忘了   定义这样的服务?“,”跟踪“:[       {         “ namespace”:“”,         “ short_class”:“”,         “ class”:“”,         “ type”:“”,         “ function”:“”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ http-kernel \ Controller \ ContainerControllerResolver.php”,         “行”:62,         “参数”:[]       },       {         “ namespace”:“ Symfony \ Component \ HttpKernel \ Controller”,         “ short_class”:“ ContainerControllerResolver”,         “ class”:“ Symfony \ Component \ HttpKernel \ Controller \ ContainerControllerResolver”,         “ type”:“->”,         “ function”:“ instantiateController”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ framework-bundle \ Controller \ ControllerResolver.php”,         “线”:54         “参数”:[           [             “串”,             “ \ App \ Controller \ CategoryController”           ]         ]       },       {         “ namespace”:“ Symfony \ Bundle \ FrameworkBundle \ Controller”,         “ short_class”:“ ControllerResolver”,         “ class”:“ Symfony \ Bundle \ FrameworkBundle \ Controller \ ControllerResolver”,         “ type”:“->”,         “ function”:“ instantiateController”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ http-kernel \ Controller \ ControllerResolver.php”,         “行”:110,         “参数”:[           [             “串”,             “ \ App \ Controller \ CategoryController”           ]         ]       },       {         “ namespace”:“ Symfony \ Component \ HttpKernel \ Controller”,         “ short_class”:“ ControllerResolver”,         “ class”:“ Symfony \ Component \ HttpKernel \ Controller \ ControllerResolver”,         “ type”:“->”,         “ function”:“ createController”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ http-kernel \ Controller \ ContainerControllerResolver.php”,         “行”:42         “参数”:[           [             “串”,             “ \ App \ Controller \ CategoryController :: show”           ]         ]       },       {         “ namespace”:“ Symfony \ Component \ HttpKernel \ Controller”,         “ short_class”:“ ContainerControllerResolver”,         “ class”:“ Symfony \ Component \ HttpKernel \ Controller \ ContainerControllerResolver”,         “ type”:“->”,         “ function”:“ createController”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ framework-bundle \ Controller \ ControllerResolver.php”,         “线”:46,         “参数”:[           [             “串”,             “ \ App \ Controller \ CategoryController :: show”           ]         ]       },       {         “ namespace”:“ Symfony \ Bundle \ FrameworkBundle \ Controller”,         “ short_class”:“ ControllerResolver”,         “ class”:“ Symfony \ Bundle \ FrameworkBundle \ Controller \ ControllerResolver”,         “ type”:“->”,         “ function”:“ createController”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ http-kernel \ Controller \ ControllerResolver.php”,         “行”:85,         “参数”:[           [             “串”,             “ \ App \ Controller \ CategoryController :: show”           ]         ]       },       {         “ namespace”:“ Symfony \ Component \ HttpKernel \ Controller”,         “ short_class”:“ ControllerResolver”,         “ class”:“ Symfony \ Component \ HttpKernel \ Controller \ ControllerResolver”,         “ type”:“->”,         “ function”:“ getController”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ http-kernel \ Controller \ TraceableControllerResolver.php”,         “线”:38,         “参数”:[           [             “宾语”,             “ Symfony \ Component \ HttpFoundation \ Request”           ]         ]       },       {         “ namespace”:“ Symfony \ Component \ HttpKernel \ Controller”,         “ short_class”:“ TraceableControllerResolver”,         “ class”:“ Symfony \ Component \ HttpKernel \ Controller \ TraceableControllerResolver”,         “ type”:“->”,         “ function”:“ getController”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ http-kernel \ HttpKernel.php”,         “行”:134,         “参数”:[           [             “宾语”,             “ Symfony \ Component \ HttpFoundation \ Request”           ]         ]       },       {         “ namespace”:“ Symfony \ Component \ HttpKernel”,         “ short_class”:“ HttpKernel”,         “ class”:“ Symfony \ Component \ HttpKernel \ HttpKernel”,         “ type”:“->”,         “ function”:“ handleRaw”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ http-kernel \ HttpKernel.php”,         “行”:68,         “参数”:[           [             “宾语”,             “ Symfony \ Component \ HttpFoundation \ Request”           ],           [             “整数”,             1个           ]         ]       },       {         “ namespace”:“ Symfony \ Component \ HttpKernel”,         “ short_class”:“ HttpKernel”,         “ class”:“ Symfony \ Component \ HttpKernel \ HttpKernel”,         “ type”:“->”,         “ function”:“ handle”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ http-kernel \ Kernel.php”,         “行”:198,         “参数”:[           [             “宾语”,             “ Symfony \ Component \ HttpFoundation \ Request”           ],           [             “整数”,             1个           ],           [             “布尔值”,             真正           ]         ]       },       {         “ namespace”:“ Symfony \ Component \ HttpKernel”,         “ short_class”:“内核”,         “ class”:“ Symfony \ Component \ HttpKernel \ Kernel”,         “ type”:“->”,         “ function”:“ handle”,         “ file”:“ C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ public \ index.php”,         “行”:25,         “参数”:[           [             “宾语”,             “ Symfony \ Component \ HttpFoundation \ Request”           ]         ]       }]}

<?php

namespace App\Controller;

use App\Entity\User;
use App\Service\CategoryService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class CategoryController extends AbstractController
{
    /**
     * @var CategoryService
     */
    private $categoryService;

    /**
     * @param CategoryService $categoryService
     */
    public function __construct(CategoryService $categoryService)
    {
        $this->categoryService = $categoryService;
    }

    public function show($slug)
    {
        return $slug;
    }
}

我尝试将控制器和服务添加到services.yaml

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
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,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    category_service:
        class: App\Service\CategoryService
        public: true

    App\Controller\CategoryController:
        arguments: ['@category_service']


0 个答案:

没有答案