繁体   English   中英

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

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

我是 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 知道在训练中效果很好

我是 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 知道在训练中效果很好

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);
    }

配置/服务.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']

作曲家.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.*"
    },

看起来您的实体缺少注释。

您需要安装“sensio/framework-extra-bundle”。 跑:

composer require sensio/framework-extra-bundle

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

尝试通过命令清除注释缓存

bin/console doctrine:cache:clear-metadata

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM