我是Symfony的新手,正在尝试学习基础知识。我最近看到了这个问题,我想学习路由的工作方式。所以我Controller1.php
从问题中复制了并将其更改为UserController.php
:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class UsersController extends AbstractController
{
/**
* @Route("/listOf/Users", methods={"GET"})
* @param Request $request
* @return JsonResponse
*/
public function list(Request $request)
{
if (empty($request->headers->get('api-key'))) {
return new JsonResponse(['error' => 'Please provide an API_key'], 401);
}
if ($request->headers->get('api-key') !== $_ENV['API_KEY']) {
return new JsonResponse(['error' => 'Invalid API key'], 401);
}
return new JsonResponse($this->getDoctrine()->getRepository('App\Entity\User')->findAll());
}
}
如OP所言,确实可以正常工作并返回以下列表(使用Sequel Pro手动添加的数据)列表:
[
{
"id": 14,
"name": "user1 Name"
},
{
"id": 226,
"name": "user2 Name"
},
{
"id": 383,
"name": "user3 Name"
}
]
因此,我的下一步是学习如何调整此用户列表,以返回具有给定的特定用户id
。因此,我遵循了有关路由的Symfony官方文档。所以我将代码更改为以下内容:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class UsersController extends AbstractController
{
/**
* @Route("/listOf/Users/{IdUser}", requirements={"IdUser"="\d+"}, methods={"GET"})
* @param Request $request
* @param int $IdUser
* @return JsonResponse
*/
public function list(Request $request, int $IdUser)
{
if (empty($request->headers->get('api-key'))) {
return new JsonResponse(['error' => 'Please provide an API_key'], 401);
}
if ($request->headers->get('api-key') !== $_ENV['API_KEY']) {
return new JsonResponse(['error' => 'Invalid API key'], 401);
}
return new JsonResponse($this->getDoctrine()->getRepository('App\Entity\User\{IdUser}')->findAll());
}
}
并尝试请求ID为14的用户数据,但这无效,并产生以下错误:
类App \ Entity \ User {IdUser}不存在(500内部服务器错误)
为了能够做我想做的事情,我还需要做些什么改变?
这是我的User.php
实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements \JsonSerializable
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function jsonSerialize()
{
return get_object_vars($this);
}
}
UserRepository.php
除了自动生成的代码外,我什么也没有。
编辑:我的第一个请求的工作形式是:http://domainName.local:80/listOf/Users
我的第二个请求是:http://domainName.local:80/listOf/Users/14
如前所述,这就是为什么它不起作用以及如何使其起作用。
让我们检查一下代码的打击:
$this->getDoctrine()->getRepository('App\Entity\User\{IdUser}')->findAll();
基本上,您是在说:学说,给我一个负责App\Entity\User\{IdUser}
从字面上处理实体的存储库,并且没有这样的实体类。您真正想要的是仓库App\Entity\User
。
传递给该getRepository()
方法的字符串必须始终是实体的全限定类名-句点。为了确保您在这里没有任何错字,使用实体的类常量非常有帮助,如下所示
$repo = $this->getDoctrine()->getRepository(App\Entity\User::class);
有了存储库后,您可以调用其不同的方法,如教义文档中所示:https://www.doctrine-project.org/api/orm/latest/Doctrine/ORM/EntityRepository.html对于您而言,$IdUser
您要映射到id
用户类的db列/实体属性的变量。既然您知道您恰好希望这个ID为14的用户,那么您要做的就是告诉存储库查找一个看起来像这样的用户。
// here's the example for your specific case
$user = $repo->findOneBy(['id' => $IdUser]);
// another example could be e.g. to search a user by their email address
$user = $repo->findOneBy(['email' => $email]);
// you can also pass multiple conditions to find*By methods
$user = $repo->findOneBy([
'first_name' => $firstName,
'last_name' => $lastName,
]);
希望这比混淆=>更有用
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句