如何解决无法自动装配“App\Controller\BlogController::edit()”的参数 $blog:它引用类“App\Entity\Blog”但不存在这样的服务
在这里我试图编辑帖子,但它显示了这个自动装配错误......即使代码中的一切似乎都是正确的......
这是我用来编辑我的 symfony 项目的具有编辑功能的控制器文件......我认为没有任何语法错误......它似乎像别的东西
src/Controller/BlogController.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Annotation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\DependencyInjection;
use App\Repository\BlogRepository;
use App\Entity\Blog;
use App\Form\BlogType;
class BlogController extends AbstractController
{
/**
* @Route("/{id}/edit",name="edit",methods={"GET","POST"})
* @param Request $request
* @param Blog $blog
* @return Response
*/
public function edit(Request $request,Blog $blog,$id): Response
{
$form = $this->createForm(BlogType::class,$blog);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$blog = $entityManager->getRepository(Blog::class)->find($id);
if ($form->isSubmitted()) {
$file = $request->files->get('blog')['featureImage'];
if($file != null){
$uploads_directory = $this->getParameter('uploads_directory');
$filename = $file->getClientOriginalName();
$file->move(
$uploads_directory,$filename
);
$blog->setFeatureImage($filename);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($blog);
$entityManager->flush();
$this->addFlash('success','blog edited successfully!');
return $this->redirectToRoute('blog');
}
return $this->render('blog/edit.html.twig',[
'edit' => $form->createView(),'blog' => $blog,'id' => $blog->getid(),]);
}
}
here is my blog entity file codes......
src/Entity/Blog.php
<?php
namespace App\Entity;
use App\Controller\Admin\BlogController;
use App\Repository\BlogRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Table(name="blogs")
* @UniqueEntity("blogTitle")
* @ORM\Entity(repositoryClass=BlogRepository::class)
*/
class Blog
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Assert\NotBlank(message = "The blog title of category is required.")
* @ORM\Column(type="string",length=255)
*/
private $title;
/**
* @Assert\NotBlank(message = "The short description of category is required.")
* @Assert\Length(min=500)
* @ORM\Column(type="text")
*/
private $shortDescription;
/**
* @Assert\NotBlank(message = "The description of category is required.")
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="string",length=255,nullable=true)
* @Assert\NotBlank(message = "The feature image of category is required.")
*/
private $featureImage;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getShortDescription(): ?string
{
return $this->shortDescription;
}
public function setShortDescription(string $shortDescription): self
{
$this->shortDescription = $shortDescription;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getFeatureImage(): ?string
{
return $this->featureImage;
}
public function setFeatureImage(?string $featureImage): self
{
$this->featureImage = $featureImage;
return $this;
}
}
and here is my template where i used to create edit botton to edit post...
模板:blog/index.html.twig
{% for blog in blog %}
<tr>
<td>{{ blog.id }}</td>
<td>{{ blog.title }}</td>
<a href="{{ path('edit',{'id': blog.id,'title': blog.title}) }}" class="btn btn-outline-info">{{ 'Edit'|trans }}</a>
</tr>
{% else %}
解决方法
由于您使用的是 ParamConverters,但您在控制器方法中同时添加了 $blog
和 $id
,因此犯了一个错误。
就这样做:
/**
* @Route("/{id}/edit",name="edit",methods={"GET","POST"})
*/
public function edit(Request $request,Blog $blog): Response
{
}
您的 Twig 模板也是如此。您只需要使用 id
。您应该删除title
:
{% for blog in blog %}
<tr>
<td>{{ blog.id }}</td>
<td>{{ blog.title }}</td>
<a href="{{ path('edit',{'id': blog.id }) }}" class="btn btn-outline-info">{{ 'Edit'|trans }}</a>
</tr>
{% else %}
额外建议:我删除了 @return
和 @param
文档块。由于您使用的是 return/param 类型,因此这些类型不会增加任何值。
根据我的评论。这就是我会做的,所以你不必注入你的实体。让 Form 类处理它。
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Annotation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\DependencyInjection;
use App\Repository\BlogRepository;
use App\Entity\Blog;
use App\Form\BlogType;
class BlogController extends AbstractController
{
/**
* @Route("/{id}/edit","POST"})
* @param Request $request
* @return Response
*/
public function edit(Request $request,$id): Response
{
$entityManager = $this->getDoctrine()->getManager();
$blog = $entityManager->getRepository(Blog::class)->find($id);
$form = $this->createForm(BlogType::class,$blog);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $request->files->get('blog')['featureImage'];
if($file != null){
$uploads_directory = $this->getParameter('uploads_directory');
$filename = $file->getClientOriginalName();
$file->move(
$uploads_directory,$filename
);
$blog->setFeatureImage($filename);
}
$entityManager->persist($blog);
$entityManager->flush();
$this->addFlash('success','blog edited successfully!');
return $this->redirectToRoute('blog');
}
return $this->render('blog/edit.html.twig',[
'edit' => $form->createView(),'blog' => $blog,'id' => $blog->getid(),]);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。