我有两个捆绑:
MainBundle:谁有我的网站的不同部分。
SecondBundle与形式编码不同的优惠(此优惠套装)调用一个遗产控制器的形式
我的问题是,我想打电话给我的报价形式控制器我MainBundle我的主页树枝。
我没有问题来调用窗体,但我的窗体的数据不保存在我的数据库中。另一方面,如果我把我的表格放在我的主控制器中,我没有问题,它可以正确保存所有的数据。
形式树枝代码:
{{ form_start(form,{'attr': {'id': 'FootForm'}}) }}
<div class="field-warper">
{{ form_label(form.titre, "Titre") }}
{{ form_widget(form.titre,{'attr': {'placeholder': 'Titre'}}) }}
</div>
<div class="field-warper">
{{ form_label(form.contenu, "Contenu") }}
{{ form_widget(form.contenu,{'attr': {'placeholder': 'Contenu'}}) }}
</div>
<div class="field-warper">
{{ form_label(form.price, "Price") }}
{{ form_widget(form.price,{'attr': {'placeholder': 'Price'}}) }}
</div>
<div class="field-warper submit-warper">
{{ form_widget(form.send,{'attr':{'class':'submit'}}) }}
</div>
{{ form_end(form) }}
我的主页树枝:
{% extends "DellexisMainBundle:Common:base.html.twig" %}
{% block body %}
<div>
<p>i'm the Body</p>
{% block formu %}
{{ render(controller('DellexisOfferBundle:Offer:index')) }}
{% endblock %}
</div>
{% endblock %}
我的报价控制器(表单控件):
class OfferController extends Controller
{
public function indexAction(Request $request)
{
$entity_manager = $this->getDoctrine()->getManager();
$offer=new Offer();
$form = $this->get('form.factory')->createBuilder('form', $offer)
->add('titre','text')
->add('contenu','textarea')
->add('price','text')
->add('send', 'submit')
->getForm();
// on joint la requete Post à notre classe
$form->handleRequest($request);
if($form->isValid()) {
$entity_manager->persist($offer);
$entity_manager->flush();
}
return $this->render('DellexisOfferBundle:Default:index.html.twig', array(
'form' => $form->createView()
));
}
}
我的主控制器:
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
return $this->render('DellexisMainBundle:Home:index.html.twig');
}
}
的routing.yml
dellexis_offer:
resource: "@DellexisOfferBundle/Resources/config/routing.xml"
prefix: /offer
dellexis_main:
resource: "@DellexisMainBundle/Resources/config/routing.xml"
prefix: /
OfferBundle routing.xml
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="dellexis_offer_homepage" path="/offer">
<default key="_controller">DellexisOfferBundle:Default:index</default>
</route>
</routes>
MainBundle的routing.yml
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="dellexis_main_homepage" path="/">
<default key="_controller">DellexisMainBundle:Default:index</default>
</route>
</routes>
编辑:
随着我OfferController的要求,我DefaultController(主束)的转储的转储,POST方法是在DefaultController,在OfferController停留在一个GET方法。
编辑2:解决的办法:
我finaly找到解决方案。由于第二个控制器没有与第一个控制器相同的请求。我想这是为了向第二个控制器提出请求。而symfony的魔法就是要这样做!
因此,如果有人有同样的问题,只是转发这样的请求:
public function indexAction(Request $request)
{
$this->forward('DellexisOfferBundle:Offer:index', array(
'request' => $request
));
return $this->render('DellexisMainBundle:Home:index.html.twig');
}
能你发布你的路由配置文件?看起来'DefaultController'对你的代码没有附加价值,或者你对每个Bundle有不同的路由配置。 – inaliahgle
我已编辑帖子 –
我更新了答案! – inaliahgle