我创建了这个可能有用的简单FlashBagTrait:
<?php
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
trait FlashBagTrait
{
/**
* @return FlashBagInterface
*/
public function getFlashBag() {
return $this['session']->getFlashBag();
}
}
只需将它添加到您的 Application 课程中,它就会让事情变得更加简单!
$app->getFlashBag()->add('message',array('type'=>"danger",'content'=>"You shouldn't be here"));
{% if app.flashbag.peek('message') %}
<div class="row">
{% for flash in app.flashbag.get('message') %}
<div class="bs-callout bs-callout-{{ flash.type }}">
<p>{{ flash.content }}</p>
</div>
{% endfor %}
</div>
{% endif %}
它的主要优点是类型提示可以在 PhpStorm 中工作。
您也可以将其添加为服务提供者,
$app['flashbag'] = $app->share(function (Application $app) {
return $app['session']->getFlashBag();
});
这使得在 PHP 中使用起来更方便(但你失去了类型提示):
$app['flashbag']->add('message',array('type'=>"danger",'content'=>"You shouldn't be here"));