简体   繁体   中英

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 . 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 . knowing that in training it works well

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

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

"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. run:

composer require sensio/framework-extra-bundle

and check config/bundles.php have Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle

Try to clear an annotation cache by command

bin/console doctrine:cache:clear-metadata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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