symfony 创建一个 flash 提示框

今天闲来无事,于是看了看symfony的session。发现里面有个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">&times;</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() }}

然后出来的效果为

symfony 创建一个 flash 提示框

是不是很炫啊!哈哈哈

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!