2016-11-24 95 views
3

我有两个捆绑:
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'); 
    } 
+0

能你发布你的路由配置文件?看起来'DefaultController'对你的代码没有附加价值,或者你对每个Bundle有不同的路由配置。 – inaliahgle

+0

我已编辑帖子 –

+0

我更新了答案! – inaliahgle

回答

1

首先,你应该有不同的路线后缀,因为你用的两束相同的一个(\) ,女巫是个坏主意。您可以为offerBundle路由配置(\offer)添加后缀。

另外,在路由中。yml,你没有加载offerBundle路由配置文件。

而且,通过看着你的控制器,没有什么可以保存的。 indexAction显示页面并保存一个空对象new Offer()

你有POST请求区分(当你提交的数据)和GET请求(如果你想显示的形式)

你的代码应该是这样的:

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($request->isMethod('POST') && $form->isValid()) { 
     $entity_manager->persist($offer); 
     $entity_manager->flush(); 
     } 

     return $this->render('DellexisOfferBundle:Default:index.html.twig', array(
      'form' => $form->createView() 
    )); 
    } 


} 
+0

我尝试了你说的话,但没有任何改变。但是使用$ form-> isValid(),如果表单中没有数据,它就不会保存任何数据,因此GET和POST方法之间已经有了验证否?我在控制器中放置了转储,以查看offer变量是否具有POST的值,但该变量仍具有空值。所以我认为它没有检测到POST方法 –

+0

随着我的OfferController请求的转储和DefaultController(Main Bundle)的转储,POST方法在DefaultController上,OfferController停留在GET方法上。 –

+0

@ThomasClermont是否适合你? – inaliahgle

相关问题