英文:
Entity doesnt appear at /api
问题
I have a work project with Symfony 6 and we use Api platform.
I need to make my entity an api resource with only an endpoint for getting the list of all my entities in the database.
My problem is that I don't understand why my entity doesn't appear as an api resource at 127.0.0.1/api, and the endpoint doesn't work either. I tried to create a new test entity with the Symfony maker to make it directly an entity resource, but it didn't work either. I don't understand what the problem is.
My entity:
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();
}
My 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.*"
}
}
I tried to create a new entity with the maker and make it directly an api resource, but it doesn't work.
I also tried to upgrade Api platform, but there are some conflicts with my composer that I don't understand.
英文:
I have a work project with Symfony 6 and we use Api platform.
I need to make my entity to an api resource with only an endpoint for get the list of all my entity in db.
My problem is i dont understand for what my entity doesnt appear as api resource at the 127.0.0.1/api and the endpoint dont work too. I try to create a new test entity with the symfony maker for make it directly as entity resource but it dont work too i dont understand what is the problem.
My entity :
<?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();
}
My 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.*"
}
}
I try to make a new entity with the maker and make it directly as an api resource but its not work
I try to upgrade Api platform and there is some conflict with my composer that i dont understand
答案1
得分: 1
在你的文件 config/api_platform.yaml
中检查你的映射是否正确,映射键应该如下所示:
api_platform:
mapping:
paths: ['%kernel.project_dir%/src/Entity']
你也可以完全删除映射键,只要你的类上有 #[ApiResource]
属性,API 平台将自动公开你的 API。
参考这个文档。
英文:
In your file config/api_platform.yaml
check that your mapping is correct, the mapping key should look like this:
api_platform:
mapping:
paths: ['%kernel.project_dir%/src/Entity']
You can also completely remove the mapping key and api platform will expose your API's automatically as long as you have the attribute #[ApiResource]
on your class.
See this documentation for reference
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论