简体   繁体   English

在 Symfony 中配置 HTTP 请求方法?

[英]Configuring HTTP Request Methods in Symfony?

I'm trying to get into Symfony for a project and I (sort of) have trouble figuring out how things work in Symfony.我正在尝试进入 Symfony 进行一个项目,但我(有点)无法弄清楚 Symfony 中的工作原理。 I've done these things a couple of times in Java Spring Boot before but this project needs me doing stuff in PHP and I want to learn too.我之前在 Java Spring Boot 中做过几次这些事情,但这个项目需要我用 PHP 做东西,我也想学习。

What I have basically done is create an Entity, Repository, Service and now I'm working on the controller.我所做的基本上是创建一个实体、存储库、服务,现在我正在处理控制器。

I made the entity, repo and controller using make:entity (or make:controller)我使用 make:entity(或 make:controller)创建了实体、repo 和控制器

The Service is supposed to wrap the repository and further abstract things.该服务应该包装存储库并进一步抽象事物。

My questions:我的问题:

  1. In the Controller I have a constructor.在控制器中,我有一个构造函数。 Is that one actually called?那个真的叫吗? I need it to initialize the Service it is used in我需要它来初始化它所使用的服务

  2. How do I define what HTTP Request method needs to be used?如何定义需要使用的 HTTP 请求方法? I know how to specify routes, but how do I define if it is to be accessed as GET, POST, PUT, DELETE?我知道如何指定路由,但我如何定义它是否以 GET、POST、PUT、DELETE 方式访问? The Symfony doc about controllers does not specify this.关于控制器的 Symfony 文档没有指定这一点。

  3. I have to find this out later so I think I'll ask here: If I want to persist an item through the api, I just pass the objects as json?我稍后必须找到这个,所以我想我会在这里问:如果我想通过 api 保留一个项目,我只是将对象作为 json 传递? (For example if I'm testing with postman) (例如,如果我正在使用邮递员进行测试)

Here's my Entity:这是我的实体:

?php
 
namespace App\Entity;
 
use App\Repository\FahrzeugRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
 
/**
 * @ORM\Entity(repositoryClass=FahrzeugRepository::class)
 */
class Fahrzeug
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;
 
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $fahrgestellnummer;
 
    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $tueren;
 
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $modellbezeichnung;
 
    /**
     * @ORM\ManyToMany(targetEntity=Person::class, mappedBy="faehrt")
     */
    private $gefahren_von;
 
    /**
     * @ORM\ManyToOne(targetEntity=Marke::class, inversedBy="produziert")
     * @ORM\JoinColumn(nullable=false)
     */
    private $stammt_von;
 
    public function __construct()
    {
    $this->gefahren_von = new ArrayCollection();
    }
 
    public function getId(): ?int
    {
    return $this->id;
    }
 
    public function getFahrgestellnummer(): ?string
    {
    return $this->fahrgestellnummer;
    }
 
    public function setFahrgestellnummer(string $fahrgestellnummer): self
    {
    $this->fahrgestellnummer = $fahrgestellnummer;
 
    return $this;
    }
 
    public function getTueren(): ?int
    {
    return $this->tueren;
    }
 
    public function setTueren(?int $tueren): self
    {
    $this->tueren = $tueren;
 
    return $this;
    }
 
    public function getModellbezeichnung(): ?string
    {
    return $this->modellbezeichnung;
    }
 
    public function setModellbezeichnung(string $modellbezeichnung): self
    {
    $this->modellbezeichnung = $modellbezeichnung;
 
    return $this;
    }
 
    /**
     * @return Collection|Person[]
     */
    public function getGefahrenVon(): Collection
    {
    return $this->gefahren_von;
    }
 
    public function addGefahrenVon(Person $gefahrenVon): self
    {
    if (!$this->gefahren_von->contains($gefahrenVon)) {
        $this->gefahren_von[] = $gefahrenVon;
        $gefahrenVon->addFaehrt($this);
    }
 
    return $this;
    }
 
    public function removeGefahrenVon(Person $gefahrenVon): self
    {
    if ($this->gefahren_von->removeElement($gefahrenVon)) {
        $gefahrenVon->removeFaehrt($this);
    }
 
    return $this;
    }
 
    public function getStammtVon(): ?Marke
    {
    return $this->stammt_von;
    }
 
    public function setStammtVon(?Marke $stammt_von): self
    {
    $this->stammt_von = $stammt_von;
 
    return $this;
    }
}

My Repo:我的回购:

<?php

namespace App\Repository;

use App\Entity\Fahrzeug;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method Fahrzeug|null find($id, $lockMode = null, $lockVersion = null)
 * @method Fahrzeug|null findOneBy(array $criteria, array $orderBy = null)
 * @method Fahrzeug[]    findAll()
 * @method Fahrzeug[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class FahrzeugRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
    parent::__construct($registry, Fahrzeug::class);
    }

    // /**
    //  * @return Fahrzeug[] Returns an array of Fahrzeug objects
    //  */
    /*
    public function findByExampleField($value)
    {
    return $this->createQueryBuilder('f')
        ->andWhere('f.exampleField = :val')
        ->setParameter('val', $value)
        ->orderBy('f.id', 'ASC')
        ->setMaxResults(10)
        ->getQuery()
        ->getResult()
    ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Fahrzeug
    {
    return $this->createQueryBuilder('f')
        ->andWhere('f.exampleField = :val')
        ->setParameter('val', $value)
        ->getQuery()
        ->getOneOrNullResult()
    ;
    }
    */
}
    

My Service我的服务

<?php

namespace App\Service;

use App\Entity\Fahrzeug;
use App\Repository\FahrzeugRepository;
use Doctrine\ORM\EntityManagerInterface;

class FahrzeugService {

    private FahrzeugRepository $fahrzeugRepository;

    public function __construct() {
    $this->injectRepository();
    }

    private function injectRepository() {
    $this->fahrzeugRepository = $this->getDoctrine()->getManager()->getRepository(Fahrzeug::class);
    }

    public function findById(int $id): Fahrzeug {
    return $this->fahrzeugRepository->find($id);
    }

    //Returns an array
    public function findAll(): array
    {
    return $this->fahrzeugRepository->findAll();
    }

    public function save(Fahrzeug $fahrzeug): Fahrzeug {
    $this->fahrzeugRepository->persist($fahrzeug);

    //TODO: gucken ob persist reicht oder ob man eine neue instanz erzeugen muss

    $this->fahrzeugRepository->flush();
    return $fahrzeug;
    }

    //TODO UPdate - kann man das auch mittels save machen?

    
    public function delete(Fahrzeug $fahrzeug): Fahrzeug {

    /*TODO: Herausfinden was auf der anderen Seite passiert
    Idealerweise wird auf der anderen Seite das Feld genullt
    */

    $this->fahrzeugRepository->remove($fahrzeug);
    $this->fahrzeugRepository->flush();
    return $fahrzeug;
    }
}

My Controller I'm working on:我正在处理的控制器:

<?php

namespace App\Controller;

use App\Entity\Fahrzeug;
use App\Service\FahrzeugService;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class FahrzeugController extends AbstractController
{
    
    private FahrzeugService $fahrzeugService;

    //TODO wird der Controller initialisiert?
    public function __construct() {
    $this->fahrzeugService = new FahrzeugService();
    }

    #[Route('/fahrzeugIndex', name: 'fahrzeug')]
    public function index(): Response
    {
    return $this->render('fahrzeug/index.html.twig', [
        'controller_name' => 'FahrzeugController',
    ]);
    }

    #[Route('/fahrzeug/{id}', name: 'fahrzeug')]
    public function findById(int $id): Fahrzeug {
    return $this->fahrzeugService->findById($id);
    }

    #[Route('/fahrzeug', name: 'fahrzeug')]
    public function findAll(): array {
    return $this->fahrzeugService->findAll();
    }

    #[Route('/fahrzeugIndex', name: 'fahrzeug')]
    public function save(Fahrzeug $fahrzeug): Fahrzeug {
    return $this->fahrzeugService->save($fahrzeug);
    }

    public function delete(Fahrzeug $fahrzeug): Fahrzeug {
    return $this->fahrzeugService->delete($fahrzeug);
    }
}

Dependency Injection in PHP/Symfony works different than in Java/Spring. PHP/Symfony 中的依赖注入与 Java/Spring 中的工作方式不同。 If you want to inject a dependency, you have to add it to the constructor.如果要注入依赖项,则必须将其添加到构造函数中。 That dependency will be automatically constructed using the dependency-injection-container built in symfony.该依赖项将使用 symfony 中内置的依赖注入容器自动构建。

private FahrzeugService $fahrzeugService;
public function __construct(FahrzeugService $fahrzeugService)
{
   $this->fahrzeugService = $fahrzeugService;
}

You don't have to specify a method which injects the service.您不必指定注入服务的方法。 It is possible to change how your class (Controller/Service/etc.) is being created, but that's not the case here.可以更改您的类(控制器/服务/等)的创建方式,但这里并非如此。

Once that's done, you can call methods in your FahrzeugService using完成后,您可以使用FahrzeugService调用方法

$this->fahrzeugService->[method]()

So in your case, you can use:所以在你的情况下,你可以使用:

<?php

namespace App\Service;

use App\Entity\Fahrzeug;
use App\Repository\FahrzeugRepository;

class FahrzeugService {

    private FahrzeugRepository $fahrzeugRepository;

    public function __construct(FahrzeugRepository $fahrzeugRepository) {
       $this->fahrzeugRepository = $fahrzeugRepository;
    }

    //...
}

You don't have to get the repository from the EntityManager.您不必从 EntityManager 获取存储库。 It is possible, but you don't have to.这是可能的,但您不必这样做。

For your HTTP methods.对于您的 HTTP 方法。 You can specify the method in your annotations.您可以在注释中指定方法。 Since you are already using the new annotations, you can use由于您已经在使用新的注释,您可以使用

#[Route('/fahrzeugIndex', name: 'fahrzeug', methods:['GET', 'POST'])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM