symfony 实体未出现在/API处

wljmcqd8  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(136)

我有一个与Symfony 6的工作项目,我们使用API平台。
我需要使我的实体到一个只有一个端点的API资源,以获取所有我的实体在数据库中的列表。
我的问题是我不明白为什么我的实体不作为API资源出现在127.0.0.1/api和端点不工作太。我试图创建一个新的测试实体与symfony制造商直接作为实体资源,但它不工作太我不明白是什么问题。
我的单位:

<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Repository\BoxTypeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: BoxTypeRepository::class)]
#[ApiResource]
class BoxType
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 45)]
    private ?string $name = null;

    #[ORM\Column(length: 45)]
    private ?string $manufacturer = null;

    #[ORM\OneToMany(mappedBy: 'boxTypeId', targetEntity: Box::class)]
    private Collection $boxes;

    public function __construct()
    {
        $this->boxes = new ArrayCollection();
    }

我的composer.json:

{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "stable",
    "prefer-stable": true,
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "api-platform/core": "^2.7",
        "doctrine/annotations": "^1.0",
        "doctrine/doctrine-bundle": "^2.8",
        "doctrine/doctrine-migrations-bundle": "^3.2",
        "doctrine/orm": "^2.14",
        "easycorp/easyadmin-bundle": "^4.6",
        "fakerphp/faker": "^1.21",
        "nelmio/cors-bundle": "^2.2",
        "phpdocumentor/reflection-docblock": "^5.3",
        "phpstan/phpdoc-parser": "^1.15",
        "sensio/framework-extra-bundle": "^6.1",
        "symfony/asset": "6.0.*",
        "symfony/console": "6.0.*",
        "symfony/doctrine-messenger": "6.0.*",
        "symfony/dotenv": "6.0.*",
        "symfony/expression-language": "6.0.*",
        "symfony/flex": "^2",
        "symfony/form": "6.0.*",
        "symfony/framework-bundle": "6.0.*",
        "symfony/http-client": "6.0.*",
        "symfony/intl": "6.0.*",
        "symfony/mailer": "6.0.*",
        "symfony/mime": "6.0.*",
        "symfony/monolog-bundle": "^3.0",
        "symfony/notifier": "6.0.*",
        "symfony/process": "6.0.*",
        "symfony/property-access": "6.0.*",
        "symfony/property-info": "6.0.*",
        "symfony/runtime": "6.0.*",
        "symfony/security-bundle": "6.0.*",
        "symfony/serializer": "6.0.*",
        "symfony/string": "6.0.*",
        "symfony/translation": "6.0.*",
        "symfony/twig-bundle": "6.0.*",
        "symfony/validator": "6.0.*",
        "symfony/web-link": "6.0.*",
        "symfony/webpack-encore-bundle": "^1.16",
        "symfony/yaml": "6.0.*",
        "twig/extra-bundle": "^2.12|^3.0",
        "twig/twig": "^2.12|^3.0"
    },
    "config": {
        "allow-plugins": {
            "composer/package-versions-deprecated": true,
            "symfony/flex": true,
            "symfony/runtime": true
        },
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*",
        "symfony/polyfill-php73": "*",
        "symfony/polyfill-php74": "*",
        "symfony/polyfill-php80": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "6.0.*"
        }
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4",
        "phpunit/phpunit": "^9.5",
        "symfony/browser-kit": "6.0.*",
        "symfony/css-selector": "6.0.*",
        "symfony/debug-bundle": "6.0.*",
        "symfony/maker-bundle": "^1.48",
        "symfony/phpunit-bridge": "^6.2",
        "symfony/stopwatch": "6.0.*",
        "symfony/web-profiler-bundle": "6.0.*"
    }
}

我试着用制造者制造一个新的实体,并直接将其作为API资源,但它不起作用
我尝试升级API平台,但与我的 composer 发生了一些冲突,我不明白

ny6fqffe

ny6fqffe1#

在您的文件config/api_platform.yaml中检查您的Map是否正确,Map键应该如下所示:

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']

您还可以完全删除Map键,并且只要您的类具有属性#[ApiResource],API平台就会自动公开您的API。
请参阅本文档以获取参考

相关问题