Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法自动连接参数$post of“的”;App\Controller\BlogController::postById();_Php_Symfony - Fatal编程技术网

Php 无法自动连接参数$post of“的”;App\Controller\BlogController::postById();

Php 无法自动连接参数$post of“的”;App\Controller\BlogController::postById();,php,symfony,Php,Symfony,我是symfony的初学者,我接受了symfony 4.2培训,我想拥有一个id为的帖子,但它给了我错误:无法自动连接“App\Controller\BlogController::postById()”的参数$post:它引用类“App\Entity\post”,但不存在此类服务。知道在训练中它很有效 我是symfony的初学者,我接受了symfony 4.2培训,我想拥有一个id为的帖子,但它给了我错误:无法自动连接“App\Controller\BlogController::postBy

我是symfony的初学者,我接受了symfony 4.2培训,我想拥有一个id为的帖子,但它给了我错误:
无法自动连接“App\Controller\BlogController::postById()”的参数$post:它引用类“App\Entity\post”,但不存在此类服务
。知道在训练中它很有效

我是symfony的初学者,我接受了symfony 4.2培训,我想拥有一个id为的帖子,但它给了我错误:
无法自动连接“App\Controller\BlogController::postById()”的参数$post:它引用类“App\Entity\post”,但不存在此类服务
。知道在训练中它很有效

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

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

bin/console doctrine:cache:clear-metadata

您的实体似乎缺少注释。

您能显示您的
config/services.yaml
?如果控制器被标记为,是否也可以检查
bin/console debug:autowiring BlogController
?具体来说,您的控制器必须被标记,并且您需要确保实体未注册为服务,例如
bin/console debug:autowiring Post
不应返回服务。@dbrumann thx for u answer i edit my code i add
services.yaml
,看起来不错。你安装了ORM吗?尝试运行以下命令:
composer require orm
composer require doctrine/orm doctrine/doctrine bundle
,如果您在项目中未使用symfony/flex。@dbrumann我尝试运行
composer require doctrine/orm doctrine/doctrine bundle
命令,它会给我错误,然后我运行
composer require orm
命令,它可以工作,但是错误仍然与前面的注释相同,我真的认为您没有正确安装Symfony 4。特别是,条令实体是使用所谓的Param转换器注入的,该转换器由sensio/framework额外包实现。我在composer.json文件中没有看到它,您说在运行各种命令时不断出现错误。重新启动并安装一个新的SyfFoy 4.4项目:“Simfou-NeX-ValueValue= LTS项目”,将来你应该考虑增加一些细节到你的答案。您还应该知道,对于问题所示的基本情况,不需要额外的注释。这也许是个好主意,但除非您知道这是一个问题,否则注释可能更合适。另外,仅供参考,路由注释实际上并不是原则的一部分,所以这不太可能奏效。