我正在嘗試進入 Symfony 進行一個專案,但我(有點)無法弄清楚 Symfony 中的作業原理。我之前在 Java Spring Boot 中做過幾次這些事情,但是這個專案需要我用 PHP 做東西,我也想學習。
我所做的基本上是創建一個物體、存盤庫、服務,現在我正在處理控制器。
我使用 make:entity(或 make:controller)創建了物體、repo 和控制器
該服務應該包裝存盤庫并進一步抽象事物。
我的問題:
在控制器中,我有一個建構式。那個真的叫嗎?我需要它來初始化它所使用的服務
如何定義需要使用的 HTTP 請求方法?我知道如何指定路由,但我如何定義它是否以 GET、POST、PUT、DELETE 的方式訪問?關于控制器的 Symfony 檔案沒有指定這一點。
我必須稍后找到這個,所以我想我會在這里問:如果我想通過 api 保留一個專案,我只是將物件作為 json 傳遞?(例如,如果我正在使用郵遞員進行測驗)
這是我的物體:
?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;
}
}
我的回購:
<?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()
;
}
*/
}
我的服務
<?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;
}
}
我正在處理的控制器:
<?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);
}
}
uj5u.com熱心網友回復:
PHP/Symfony 中的依賴注入與 Java/Spring 中的作業方式不同。如果要注入依賴項,則必須將其添加到建構式中。該依賴項將使用 symfony 中內置的依賴注入容器自動構建。
private FahrzeugService $fahrzeugService;
public function __construct(FahrzeugService $fahrzeugService)
{
$this->fahrzeugService = $fahrzeugService;
}
您不必指定注入服務的方法。可以更改您的類(控制器/服務/等)的創建方式,但這里并非如此。
完成后,您可以在FahrzeugServiceusing 中呼叫方法
$this->fahrzeugService->[method]()
所以在你的情況下,你可以使用:
<?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;
}
//...
}
您不必從 EntityManager 獲取存盤庫。這是可能的,但您不必這樣做。
對于您的 HTTP 方法。您可以在注釋中指定方法。由于您已經在使用新的注釋,您可以使用
#[Route('/fahrzeugIndex', name: 'fahrzeug', methods:['GET', 'POST'])]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/358343.html
上一篇:在EventSubscriberSymfony3.4上發送回應
下一篇:Symfony\Bridge\Twig\Extension\RoutingExtension::getPath()中的引數錯誤
