Controller中的saveAction()从未调用过(symfony2)

时间:2016-04-20 15:39:09

标签: php symfony

我是symfony2的初学者。 (让我们从那开始)

我有一个必须将数据发送到数据库的表单。但是每次我点击提交按钮时,页面都会重新加载(我知道原因),并且不会重定向到应该使用的页面。 (该页面调用自己发送数据,但随后需要重定向)

我看到的是我的控制器中的saveAction()方法永远不会被调用,如标题中所述。我无法理解为什么。

这是我的控制器:

class ReunionsController extends Controller
{
protected $reunionsService;

/**
 * @Template("IntranetBundle:Reunions:index.html.twig")
 */
public function indexAction($page)
{
    $this->reunionsService = $this->get("intranet.reunions_service");

    if(!$page) {
        $page = 1;
    }

    $pagination = $this->reunionsService->getReunionsByPagination($page);

    // parameters to template
    return array('pagination' => $pagination);
}

/**
 * @Template()
 */
public function createAction(){

    $this->reunionsService = $this->get("intranet.reunions_service");
    $reunion = $this->reunionsService->defaultReunion();

    $form = $this->createForm(new ReunionsType(), $reunion);

    return array(
        'form' => $form->createView(),
    );
}

/**
 * @Template("IntranetBundle:Reunions:create.html.twig")
 */
public function saveAction()
{
    $request = $this->get('request');
    $this->reunionsService = $this->get("intranet.reunions_service");

    $reunion = $this->reunionsService->defaultReunions();

    $form = $this->createForm(new ReunionsType(), $reunion);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $this->reunionsService->saveReunion($reunion);
        $this->get('session')->getFlashBag()->add('notice', 'Reunion ajoutée avec succès');
        return $this->redirect($this->generateUrl('users_reunions'));
    }
    return array(
        'form' => $form->createView(),
    );
}

/**
 * @Template()
 */
public function editAction($id)
{
    $request = $this->get('request');
    if (is_null($id)) {
        $postData = $request->get('reunions');
        $id = $postData['id'];
    }

    $this->reunionssService = $this->get("intranet.reunions_service");
    //on récupere le restaurant via son id
    $reunion = $this->reunionsService->getReunions($id);
    $form = $this->createForm(new ReunionsType(), $reunion);

    return  array('form' => $form->createView(), 'id' => $id, 'date' => $reunion->getReunionDate(), 'CR' => $reunion->getReunionCR());
}

/**
 * @Template("IntranetBundle:Restaurants:edit.html.twig")
 */
public function updateAction($id)
{
    $request = $this->get('request');
    if (is_null($id)) {
        $postData = $request->get('reunions');
        $id = $postData['id'];
    }

    $this->reunionsService = $this->get("intranet.reunions_service");
    $reunion = $this->reunionsService->getReunions($id);

    $form = $this->createForm(new ReunionsType(), $reunion);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $this->reunionsService->updateReunions($reunion);
        $this->get('session')->getFlashBag()->add('notice', 'Objet mis à jour');
    }
    else
        $this->get('session')->getFlashBag()->add('noticeError', 'L\'objet n\'a pu être mis à jour');
    return  array('form' => $form->createView(), 'id' => $id, 'date' => $reunion->getReunionDate(), 'CR' => $reunion->getReunionCR());
}

}

执行保存的服务部分(也应该被调用):

public function saveReunion($reunion)
 {
     $this->getEntityManager()->persist($reunion);
     $this->getEntityManager()->flush();
 }

正在进行提交的表单部分:

<form action="{{ path('users_reunions_save') }}" onsubmit="return testAll()" method="post" {{ form_enctype(form) }}>
            {% include "IntranetBundle:Reunions:formulaire.html.twig" %}
            </fieldset>
            <ul class="sf_list_actions">
                <li class="sf_list_action_list">
                    <a href="{{ path("users_reunions") }}">Retour à la liste</a>
                </li>
                <li class="sf_list_action_save">
                    <input type="submit" value="Enregistrer"/>
                </li>
            </ul>
        </form>

告诉我你是否需要别的东西来帮助我解决这个问题。我在互联网上找不到任何关于那个......

提前致谢

编辑:这可能(肯定)是一个路由问题。 当我这样写:

users_reunions_create:
pattern:  /users/reunions/create
defaults: { _controller: IntranetBundle:Reunions:create }

users_reunions_save:
pattern:  /users/reunions/create
defaults: { _controller: IntranetBundle:Reunions:save }

这是我的问题, 但是当我写这篇文章时:

users_reunions_create:
    pattern:  /users/reunions/create
    defaults: { _controller: IntranetBundle:Reunions:save }

users_reunions_save:
    pattern:  /users/reunions/create
    defaults: { _controller: IntranetBundle:Reunions:save }

有效。但是我想要通过第二条路径,那就是&#34;保存&#34;

1 个答案:

答案 0 :(得分:0)

确定。这完全是路由。我需要写一下:

users_reunions_create:
pattern:  /users/reunions/create
defaults: { _controller: IntranetBundle:Reunions:create }

users_reunions_save:
pattern:  /users/reunions/save
defaults: { _controller: IntranetBundle:Reunions:save }

但我真的不知道为什么,因为网站上的其他一些页面都是用我之前尝试做的路线写的......无论如何。完成。