【发布时间】:2021-01-21 14:17:50
【问题描述】:
大家好! 我用我的本地网络服务器(localhost)开发了一个网页。我使用 PHP、Symfony、Twig 和 Doctrine。 当我完成页面时,如果我按下主页上的“新文章”按钮并且我遇到了这个问题(新文章页面将在我的主页上创建一个新文章,因此我尝试使用表单构建器创建一个表单,我认为createFormbuilder方法中的那个是错误的):
传递给 Symfony\Component\Form\FormBuilder::add() 的参数 2 必须是字符串或 null 类型,给定数组,在 C:\xampp\htdocs\symphart\src\ 中调用Controller\ArticleController.php
怎么了?有人知道吗?
ArticleController 代码:
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());
//}
}
?>
【问题讨论】:
标签: php forms symfony controller formbuilder