php - 从继承的 Controller 调用表单

标签 php forms symfony

我有两个包:
MainBundle:谁拥有我网站的不同部分。
SecondBundle 带有编码不同优惠的形式。(优惠 bundle )

我的问题是我想在我的 MainBundle 的主页分支中调用我的报价表单 Controller 。

我调用表单没有问题,但我的表单数据没有保存在我的数据库中。另一方面,如果我将我的表单放入我的主 Controller ,我没有问题,它会正确保存所有数据。

表格 Twig 代码:

{{ 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 %}

我的报价 Controller (表单 Controller ):

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()
        ));
    }
}

我的主 Controller :

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(Main Bundle)的转储,POST 方法在 DefaultController 上,OfferController 留在 GET 方法上。

编辑 2:解决方案:

我终于找到了解决方案。由于第二个 Controller 没有与第一个 Controller 相同的请求。我想将请求转发给第二个 Controller 。 symfony 的魔力想要它起作用!

所以如果有人有同样的问题,就这样转发请求:

public function indexAction(Request $request)
    {
        $this->forward('DellexisOfferBundle:Offer:index', array(
            'request'  => $request
        ));

        return $this->render('DellexisMainBundle:Home:index.html.twig');
    }

最佳答案

首先,您应该有不同的路由后缀,因为您在两个 Bundle 中使用相同的路由后缀 (\),这是个坏主意。您可以将后缀添加到 offerBundle 路由配置 (\offer)。

此外,在 routing.yml 中,您没有加载 offerBundle 路由配置文件。

而且,通过查看您的 Controller ,没有什么可保存的。 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()
      ));
  }


}

关于php - 从继承的 Controller 调用表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40780659/

相关文章:

php - Symfony 和 Doctrine 元数据缓存

php - Symfony2 : Duplicate definition of column 'id' on entity in a field or discriminator column mapping

javascript ajax php 调用返回另外 3 个字符

php - 使用 php 在 postgresql 中截断带约束的表

java - 如何动态调整列表类型变量的输入?

javascript - 使用 Javascript 触发 IE9 文件输入

java - php 服务器 - java 客户端 - post 和 echo

PHP:用度,分和秒格式化纬度和经度

jquery - 使用 jquery 更改占位符文本字体和颜色

php - 作为 Action 输入的后置参数