给大家介绍下 flash, flash 还是比较不错的。于是记录下来。
首先还是在我的 baseController 中建立一个方法
/** * @author gf * * flash提示 * * @param $level * @param $message */ protected function setFlashMessage($level, $message) { $this->get('session')->getFlashBag()->add($level, $message); } 然后去控制器调用该方法, /** * Creates a new Article entity. * */ public function newAction(Request $request) { $this->addFlash('success','操作成功'); $article = new Article(); $form = $this->createForm('Lot\CoreBundle\Form\ArticleType', $article); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($article); $em->flush(); $goto = $this->generateUrl('admin_article_show', array('id' => $article->getId())); return $this->createMessageResponse(0,'添加成功!', $goto); } return $this->render('LotCoreBundle:Admin/Article:new.html.twig', array( 'article' => $article, 'form' => $form->createView(), )); }
然后去创建一个 macro 标签文件,关于标签解释(http://twig.sensiolabs.org/doc/tags/macro....)
建立 macro_message.html.twig 标签
{% macro flash_messages() %} {% for type, flashMessages in app.session.flashbag.all() %} {% for flashMessage in flashMessages %} <div class="alert alert-{{ type }} alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> </button> <strong>提示:</strong> {{ flashMessage|raw }}<span id="jumpTo"></span> </div> {% endfor %} {% endfor %} {% endmacro %}
然后导入文件
{% import '@LotCore/Common/macro_message.html.twig' as lot_macro %}
然后在需要的页面去进行调用
{{ lot_macro.flash_messages() }}
然后出来的效果为
© 著作权归作者所有
举报
发表评论
0/200