简体   繁体   English

无法自动装配“App\Controller\BlogController::postById()”的参数 $post

[英]Cannot autowire argument $post of "App\Controller\BlogController::postById()"

I am a beginner in symfony, I followed a symfony 4.2 training, I want to have a post with id but it gives me error: Cannot autowire argument $post of "App\Controller\BlogController::postById()": it references class "App\Entity\Post" but no such service exists .我是 symfony 的初学者,我遵循了 symfony 4.2 培训,我想发布一个带有 id 的帖子,但它给了我错误: Cannot autowire argument $post of "App\Controller\BlogController::postById()": it references class "App\Entity\Post" but no such service exists knowing that in training it works well知道在训练中效果很好

I am a beginner in symfony, I followed a symfony 4.2 training, I want to have a post with id but it gives me error: Cannot autowire argument $post of "App\Controller\BlogController::postById()": it references class "App\Entity\Post" but no such service exists .我是 symfony 的初学者,我遵循了 symfony 4.2 培训,我想发布一个带有 id 的帖子,但它给了我错误: Cannot autowire argument $post of "App\Controller\BlogController::postById()": it references class "App\Entity\Post" but no such service exists knowing that in training it works well知道在训练中效果很好

BlogController.php BlogController.php

namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

use Symfony\Component\HttpFoundation\Request;
use App\Entity\Post;


/**
    * @Route("/post/{id}", requirements={ "id" : "\d+" }, name="get_one_post_by_id")
    * 
    */
    public function postById(Post $post){
        return $this->json($post);
    }

config/services.yaml配置/服务.yaml

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,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']

composer.json作曲家.json

"require": {
        "php": "^7.1.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "1.11.99.1",
        "doctrine/annotations": "^1.11",
        "doctrine/doctrine-bundle": "^1.11",
        "doctrine/doctrine-migrations-bundle": "^3.0",
        "doctrine/orm": "^2.7",
        "phpdocumentor/reflection-docblock": "^5.2",
        "symfony/console": "4.2.*",
        "symfony/dotenv": "4.2.*",
        "symfony/flex": "^1.3.1",
        "symfony/framework-bundle": "4.2.*",
        "symfony/property-access": "4.2.*",
        "symfony/property-info": "4.2.*",
        "symfony/proxy-manager-bridge": "4.2.*",
        "symfony/serializer": "4.2.*",
        "symfony/yaml": "4.2.*"
    },

It look like missing annotation for your entity.看起来您的实体缺少注释。

You need "sensio/framework-extra-bundle" installed.您需要安装“sensio/framework-extra-bundle”。 run:跑:

composer require sensio/framework-extra-bundle

and check config/bundles.php have Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle并检查 config/bundles.php 有 Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle

Try to clear an annotation cache by command尝试通过命令清除注释缓存

bin/console doctrine:cache:clear-metadata

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 symfony 错误:无法自动装配“App\\Controller\\ArticleController::create()”的参数 $request? - How to fix symfony error: Cannot autowire argument $request of "App\Controller\ArticleController::create()"? Symfony:无法自动装配“Controller:method()”的参数 $injectedVar:它引用 class“App\Entity\Entity”,但不存在此类服务 - Symfony: Cannot autowire argument $injectedVar of "Controller:method()": it references class "App\Entity\Entity" but no such service exists 无法自动装配“App\Controller\HomeController”参数 $:它引用 class“Symfony\Component\Form\SubmitButton”但不存在此类服务 - Cannot autowire argument $of "App\Controller\HomeController": it references class "Symfony\Component\Form\SubmitButton" but no such service exists 无法自动装配参数 $request - Cannot autowire argument $request 无法自动装配服务“App\\Service\\MatchCarAdService”:方法的参数“$templating” - Cannot autowire service "App\Service\MatchCarAdService": argument "$templating" of method 无法使用 @ParamConverter 声明自动装配实体参数 - Cannot autowire entity argument with @ParamConverter declaration Symfony 5 无法为动态数据库连接自动装配参数 $Driver - Symfony 5 Cannot autowire argument $Driver for a dynamic database connection 无法自动装配服务:参数引用 class 但不存在此类服务 - Cannot autowire service: Argument references class but no such service exists 无法以 symfony 形式自动装配服务“App\Form\QcmType”: - Cannot autowire service in a symfony form "App\Form\QcmType": 如何修复 Sylius 中的“无法自动装配服务:参数引用 class 但不存在此类服务”? - How to fix "Cannot autowire service: Argument references class but no such service exists" in Sylius?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM