问题描述
伙计们! 我使用本地网络服务器 (localhost) 开发了一个网页。我使用 PHP、Symfony、Twig 和 Doctrine。 当我完成我可以去的页面时,如果我按下主页上的“新文章”按钮,我遇到了这个问题(新文章页面将在我的主页中创建一个新文章,因此我尝试使用表单构建器创建一个表单,我认为 createFormbuilder 方法中的那个是错误的):
传递给 Symfony\Component\Form\FormBuilder::add() 的参数 2 必须是 string 或 null 类型,给定数组,在 C:\xampp\htdocs\symphart\src\ 中调用控制器\文章控制器.PHP
怎么了?有人知道吗?
namespace App\Controller;
use App\Entity\Article;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Components\Form\Extension\Core\Type\TextType;
use Symfony\Components\Form\Extension\Core\Type\TextareaType;
use Symfony\Components\Form\Extension\Core\Type\SubmitType;
class ArticleController extends AbstractController {
/**
* @Route ("/",name = "article_list")
* @Method ({"GET"})
*/
public function index ()
{
$articles = $this->getDoctrine()->getRepository(Article ::class)->findAll();
return $this->render('Articles/index.html.twig',array ('articles' => $articles));
}
/**
* @Route ("/article/new",name = "new_article")
* @Method ({"GET","POST"})
*/
public function new (Request $request)
{
$article = new Article ();
$form = $this->createFormBuilder($article)
->add ('title',TextType::class,array ('attr' => array ('class' => 'form-control')))
->add('body'. TextareaType::class,array ('required' => false,'attr' => array ('class' => 'form-control' )))
->add ('save',SubmitType::class,array ('label' => 'Create ','attr' => array ('class' => 'btn btn-primary mt-3')))
->getForm();
return $this->render ('articles/new.html.twig',array ('form' => $form->createView()));
}
/**
* @Route ("/article/{id}",name = "article_show")
*/
public function show ($id)
{
$article = $this->getDoctrine()->getRepository(Article::class)->find($id);
return $this->render ('articles/show.html.twig',array ('article' => $article));
}
///**
//* @Route ("/article/save")
//*/
//public function save ()
//{
// $entityManager = $this->getDoctrine()->getManager();
// $article = new Article();
// $article->setTitle("Article Three");
// $article->setBody("This is body for Article Three");
// $entityManager->persist($article);
// $entityManager->flush();
// return new Response ('Saves an article with the id of '. $article->getId());
//}
}
?>
解决方法
->add('body'
后面有一个点;)