目前正在开发一个在 Symfony 6 下运行的新 Web 项目。不幸的是,我刚刚发现了这个错误:
Variable "ajouterNouveauProjet" does not exist.
顺便说一句,我找不到问题!这是我的控制器代码:
<?php
namespace App\Controller;
use App\Entity\Projet;
use App\Form\ProjetFormType;
use Doctrine\ORM\EntityManagerInterface;
use phpDocumentor\Reflection\Types\This;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class LandingPageController extends AbstractController
{
#[Route('/landingPage', name: 'app_landing_page')]
public function index(): Response
{
return $this->render('landing_page/index.html.twig', [
'controller_name' => 'LandingPageController',
]);
}
#[Route('/projet', name: 'projet')]
public function projet(Request $request, EntityManagerInterface $em, ): Response
{
//On créé un nouveau produit
$projet = new Projet();
$addProjetForm = $this->createForm(ProjetFormType::class, $projet);
return $this->render('landing_page/projet.html.twig', [
'ajouterNouveauProjet' => $addProjetForm->createView(),
]);
}
}
在这里你可以找到我的查看代码:
{% extends 'base.html.twig' %}
{% block title %}Projet{% endblock %}
{% block body %}
{% include "landing_page/_menuDashboard.html.twig" %}
<form class=" box " method="post">
<h1 class="title is-1">Mes projets</h1>
{{ form_start(ajouterNouveauProjet) }}
<div class="columns">
<div class="column">
{{ form_row(ajouterNouveauProjet.raisonSociale,
{
label: 'Raison Sociale',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.activite,
{
label: 'Activité',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.responsable,
{
label: 'Responsable',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.formeJuridique,
{
label: 'Forme Juridique',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.typeJuridique,
{
label: 'Type juridique',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.mail,
{
label: 'Adresse E-mail',
attr: {class: 'input form-control'}
})
}}
</div>
<div class="column">
{{ form_row(ajouterNouveauProjet.capital,
{
label: 'Capital',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.adresse,
{
label: 'Adresse',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.codePostal,
{
label: 'Code Postal',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.ville,
{
label: 'Ville',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.telephone,
{
label: 'Téléphone',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.siret,
{
label: 'SIRET',
attr: {class: 'input form-control'}
})
}}
{{ form_row(ajouterNouveauProjet.naf,
{
label: 'NAF',
attr: {class: 'input form-control'}
})
}}
</div>
</div>
<button type="submit" class="button is-info">Valider</button>
{{ form_end(ajouterNouveauProjet) }}
</form>
{% endblock %}
有人知道为什么我会出现此错误吗?一切似乎都很好!
祝大家有美好的一天!
我尝试重命名我的变量并移动我的文件夹根目录。
尝试删除控制器中的
->createView()
。
如果您使用的是 symfony 6.3+
render()
就可以了。否则使用 renderForm()
在 Symfony 6.3 中,
render()
功能已更改。
render()
方法调用$form->createView()
将表单转换为表单视图实例。
Symfony 6.3+
public function projet(Request $request, EntityManagerInterface $em, ): Response
{
//On créé un nouveau produit
$projet = new Projet();
$addProjetForm = $this->createForm(ProjetFormType::class, $projet);
return $this->render('landing_page/projet.html.twig', [
'ajouterNouveauProjet' => $addProjetForm,
]);
}
symfony <= 6.2
#[Route('/projet', name: 'projet')]
public function projet(Request $request, EntityManagerInterface $em, ): Response
{
//On créé un nouveau produit
$projet = new Projet();
$addProjetForm = $this->createForm(ProjetFormType::class, $projet);
return $this->renderForm('landing_page/projet.html.twig', [
'ajouterNouveauProjet' => $addProjetForm,
]);
}