小编TaK*_*kEr的帖子

Symfony 2.4 app.session.flashbag.all()返回空值

我在使用flashbag消息时遇到了麻烦.我的情况很简单:

我的代码

  1. 使用表单编辑页面:

    # src/Namespace/MyBundle/Resources/views/Edit/form.html.twig
    <form action="{{ path('form_url_save', {'id': id }) }}" method="POST">
        {{ form_widget(form) }}
    </form>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过控制器将数据表单保存在数据库中:

    # src/Namespace/MyBundle/Controller/EntityController.php
    public function saveAction(Request $request, Entity $entity = null) {
        try {
            if (!$entity) {
                $entity = new Entity();
            }
    
            $form = $this->createForm(new EntityType(), $entity);
    
            if ($request->getMethod() == 'POST') {
                $form->submit($request);
    
                if ($form->isValid()) {
                    // Entity manager
                    $em = $this->getDoctrine()->getManager();
                    // Persist data
                    $em->persist($form->getData());
                    // Saving process
                    $em->flush();
                    // Add flashbag message
                    $this->get('session')->getFlashBag()->add('success', 'The backup was done successfully'));
                } else { …
    Run Code Online (Sandbox Code Playgroud)

php session symfony twig

7
推荐指数
2
解决办法
5070
查看次数

标签 统计

php ×1

session ×1

symfony ×1

twig ×1