我正在Symfony做一个小项目,以获得这个框架的一些经验。 目前我只是在制作自己的工具时使用教程,这是一个用于制作简历的GUI。
在我的控制器中,我调用$this->createFormBuilder($cvBase) ,这是一个“未定义的属性”通知,我觉得这很奇怪,因为我正在调用一个方法。
有谁知道是什么原因造成的?
<?php // My php code namespace NuiCart\CvToolBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use NuiCart\CvToolBundle\Entity\CvBase; use Symfony\Component\HttpFoundation\Request; class DefaultController extends Controller{ public function formAction(Request $request){ $cvBase = new CvBase(); $form = new $this->createFormBuilder($cvBase);通知:
注意:未定义的属性:/var/www/devel/cv/public_html/src/NuiCart/CvToolBundle/Controller/DefaultController.php中的NuiCart \ CvToolBundle \ Controller \ DefaultController :: $ createFormBuilder
第19行500内部服务器错误 - ContextErrorException
I am doing a small project in Symfony to gain some experience with this framework. At the moment I am just following tutorials while making my own tool which is a GUI to make my resume.
In my controller I am calling $this->createFormBuilder($cvBase) which is throwing an "Undefined property" notice which I think is strange because I am calling a method.
Does anyone know what is causing this?
<?php // My php code namespace NuiCart\CvToolBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use NuiCart\CvToolBundle\Entity\CvBase; use Symfony\Component\HttpFoundation\Request; class DefaultController extends Controller{ public function formAction(Request $request){ $cvBase = new CvBase(); $form = new $this->createFormBuilder($cvBase);The notice:
Notice: Undefined property: NuiCart\CvToolBundle\Controller\DefaultController::$createFormBuilder in /var/www/devel/cv/public_html/src/NuiCart/CvToolBundle/Controller/DefaultController.php
line 19 500 Internal Server Error - ContextErrorException
最满意答案
createFormBuilder() 已经返回一个新创建的FormBuilder对象 ,因此您不需要创建新的Object。 通过尝试使用new ,它试图创建一个新的DefaultController并访问一个私有的Controller内部函数(或类似的东西 - 不要因为这个问题而起诉我的正确性,因为它只是尝试解释为什么使用new $this是错误的。)
您的解决方案是删除$this->createFormBuilder() :
$form = $this->createFormBuilder($cvBase);createFormBuilder() already returns a newly created FormBuilder object and you therefore don't need to create a new Object. By attempting to use new, it is trying to create a new DefaultController and access a function that is private to the internals of the Controller (or something like that - don't sue me for correctness on this one as it is just an attempt to explain why using new $this is wrong.)
Your solution is to remove the new in $this->createFormBuilder():
$form = $this->createFormBuilder($cvBase);更多推荐
Controller,Symfony,DefaultController,$cvBase,电脑培训,计算机培训,IT培训"/> <
发布评论