我是一名初学者,我正在尝试按照教程在 Symfony 7 下创建博客,不幸的是,当我想使用他的地址(slug)显示文章页面时,我发现自己陷入了错误,我得到:
控制器“App\Controller\ArticleController::show”需要无法解析的“$article”参数。无法自动装配“App\Controller\ArticleController::show()”的参数 $article:它需要“App\Entity\Article”的实例,但此类型已在“config/services.yaml”中排除。
当我将 Article 实体注入到 ArticleController 的 show 函数中时,出现了问题。
<?php
namespace App\Controller;
use App\Entity\Article;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ArticleController extends AbstractController
{
#[Route('/article/{slug}', name: 'article_show')]
public function show(Article $article): Response
{
return $this->render('article/show.html.twig', [
'article' => $article,
]);
}
}
当我使用主页上的链接时,显示的路线是正确的(带有标头)。
知道如何解决这个问题吗? (一定很简单但我不明白)
我尝试检查我的代码,尤其是实体:
<?php
namespace App\Entity;
use App\Model\TimestampedInterface;
use App\Repository\ArticleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ArticleRepository::class)]
class Article implements TimestampedInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(length: 255)]
private ?string $slug = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $content = null;
#[ORM\Column(length: 100, nullable: true)]
private ?string $featuredText = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $createdAt = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $updatedAt = null;
/**
* @var Collection<int, Category>
*/
#[ORM\ManyToMany(targetEntity: Category::class, mappedBy: 'articles')]
private Collection $categories;
/**
* @var Collection<int, Comment>
*/
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'article', orphanRemoval: true)]
private Collection $comments;
#[ORM\ManyToOne]
private ?Media $featuredImage = null;
public function __construct()
{
$this->categories = new ArrayCollection();
$this->comments = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): static
{
$this->title = $title;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): static
{
$this->slug = $slug;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): static
{
$this->content = $content;
return $this;
}
public function getFeaturedText(): ?string
{
return $this->featuredText;
}
public function setFeaturedText(?string $featuredText): static
{
$this->featuredText = $featuredText;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection<int, Category>
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(Category $category): static
{
if (!$this->categories->contains($category)) {
$this->categories->add($category);
$category->addArticle($this);
}
return $this;
}
public function removeCategory(Category $category): static
{
if ($this->categories->removeElement($category)) {
$category->removeArticle($this);
}
return $this;
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setArticle($this);
}
return $this;
}
public function removeComment(Comment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getArticle() === $this) {
$comment->setArticle(null);
}
}
return $this;
}
public function getFeaturedImage(): ?Media
{
return $this->featuredImage;
}
public function setFeaturedImage(?Media $featuredImage): static
{
$this->featuredImage = $featuredImage;
return $this;
}
}
你明白了,因此目标是显示文章页面:
{% extends 'base.html.twig' %}
{% block title %}{{ article.title }}{% endblock %}
{% block body %}
<div class="container">
<h1>{{ article.title }}</h1>
<hr>
{{ article.content|raw }}
</div>
{% endblock %}
非常感谢,
您可以使用 MapEntity 属性来解决您的问题:
<?php
namespace App\Controller;
use App\Entity\Article;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ArticleController extends AbstractController
{
#[Route('/article/{slug}', name: 'article_show')]
public function show(#[MapEntity(mapping:['slug'=>'slug'])]Article $article): Response
{
return $this->render('article/show.html.twig', [
'article' => $article,
]);
}
}
mapEntity 属性将允许 Symfony 找到带有你的路由参数中的 slug 的实体。