![](/img/trans.png)
[英]How to fix symfony error: Cannot autowire argument $request of "App\Controller\ArticleController::create()"?
[英]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.