"Controller ..CategoryController имеет обязательные аргументы конструктора и не существует в контейнере. Вы забыли определить такой сервис?", - PullRequest
0 голосов
/ 02 июля 2019

У меня проблемы с пользовательским контроллером с Symfony4 и платформой API.

{"@context": "/ api / contexts / Error", "@type": "hydra:Ошибка ",
" hydra: title ":" Произошла ошибка "," hydra: description ":" Controller \ "\ App \ Controller \ CategoryController \" имеет обязательные аргументы конструктора и не существует в контейнере.забыли определить такой сервис? "," trace ": [{" namespace ":" "," short_class ":" "," class ":" "," type ":" "," function ":" ","file": "C: \ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ http-kernel \ Controller \ ContainerControllerResolver.php", "line": 62, "args": []}, {"namespace": "Symfony \ Component \ HttpKernel \ Controller", "short_class": "ContainerControllerResolver", "class": "Symfony \ Component \ HttpKernel \ Controller \ ContainerControllerResolver", "тип": "->", "function": "instantiateController", "file": "C: \ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ vendor \ symfony \ framework-bundle \ Controller \ ControllerResolver.php "," line ": 54," args ": [[" string "," \ 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 "," line ": 110," args ": [[" string "," \ 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 "," line ": 42," args ": [["string", "\ App \ Controller \ CategoryController :: show"]]}, {"пространство имен": "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 "," line ": 46," args ": [[" string "," \ 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 "," line ": 85, "args": [["string", "\ App \ Controller \ CategoryController :: show"]]}, {"пространство имен": "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", "line": 38,"args": [["object", "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 "," line ": 134," args ": [[" object "," 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 "," line ": 68," args ": [[" object "," Symfony \ Component \ HttpFoundation \ Request ""], [" integer ", 1]]}, {" пространство имен ":" 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", "line":198, "args": [["object", "Symfony \ Component \ HttpFoundation \ Request"], ["integer", 1], ["boolean", true]]}, {"namespace": "Symfony \ Component\ HttpKernel "," short_class ":" Kernel "," class ":" Symfony \ Component \ HttpKernel \ Kernel "," type ":" -> "," function ":" handle "," file ":" C:\ Users \ Jonny \ Desktop \ Workspace \ php \ documentarywire \ public \ index.php "," line ": 25," args ": [[" object "," 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']


Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...