newton-labs / alertify-bundle
Symfony AlertifyBundle
Requires
- friendsofsymfony/jsrouting-bundle: 2.0.*@dev
Requires (Dev)
- symfony/class-loader: ~2.1
README
Alertify Bundle
它的目的是什么?
此包允许您轻松地协调警报和其他通知。在配置中声明(或直接使用默认配置)并使用以下库(或您自己的)发送警报
安装
首先,通过 composer 依赖它
composer.phar require troopers/alertify-bundle:dev-master
在 AppKernel.php 中添加它
public function registerBundles() {
$bundles = array(
[...]
new Troopers\AlertifyBundle\TroopersAlertifyBundle(),
[...]
要包含您选择的库,您有以下两种方法
自动使用 https://github.com/Troopers/AsseticInjectorBundle (推荐)
只需在常规 assetic 块中添加资源标签的名称(例如 alertify-toastr)。
例如,此代码...
{% javascripts injector="alertify-toastr" '@MyAcmeDemoBundle/Resources/public/js/loremipsumdolorsitamet.js' %} <script type="text/javascript" src="{{ asset_url }}"></script> {% endjavascripts %}
以相同的方式为样式表(如有必要)和每个可用的库(例如 alertify-notie
,alertify-codrops-notification
...)。
手动
如果您想手动执行此操作,您将不得不阅读 Resources/config/assetic_injector.json 文件并将其手动添加
{% javascripts '@TroopersAlertifyBundle/Resources/public/toastr/js/toastr.js' %} <script type="text/javascript" src="{{ asset_url }}"></script> {% endjavascripts %}
并且以相同的方式为样式表。
配置?
要定义警报的默认配置,您可以在您的 config.yml 中添加以下行
troopers_alertify: contexts: front: engine: "toastr" \#Could be noty, modal, toastr or your own layout: "top-right" \#Is relative according to the selected engine translationDomain: "messages" \#Where do you want to store the translation strings admin: engine: "myOwnSystem" layout: "bottomRight" translationDomain: "messages" options: background: rgb(99, 176, 205) anyOptionName: anyValue
默认情况下,即使您没有声明任何上下文,Alertify 也会设置默认值。您可以通过以下方式轻松覆盖这些设置
troopers_alertify: default: context: app \#default: front engine: noty \#default: toastr layout: bottomLeft \#default: top-right translationDomain: messages \#default: flash contexts: ...
如何?
使用很简单,只需遵循以下步骤
在您的 twig 布局末尾添加此块
{% block alertify %}
{{ app.session|alertify|raw }}
{% endblock %}
现在,在任何地方,您都可以将您的警报放入 flash 会话中并享受。
$this->get('session')->getFlashBag()->add('success', 'ok');
$this->get('session')->getFlashBag()->add('warning', array('body' => 'ok');
$this->get('session')->getFlashBag()->add('warning', array('body' => 'ok', 'context' => 'front');
您可以使用 troopers_alertifybundle.helper.alertifyhelper
服务来简化警报创建
$this->container->get('troopers_alertifybundle.helper.alertifyhelper')->congrat('TEST');
$this->container->get('troopers_alertifybundle.helper.alertifyhelper')->warn('TEST');
$this->container->get('troopers_alertifybundle.helper.alertifyhelper')->inform('TEST');
$this->container->get('troopers_alertifybundle.helper.alertifyhelper')->scold('TEST');
您还可以使用 AlertifyControllerTrait 在控制器中拥有简单的方法
use Troopers\AlertifyBundle\Controller\AlertifyControllerTrait; class MyController extends ... { use AlertifyControllerTrait; /* You can use then all methods : * $this->alert("hello", $type = 'success'); * $this->congrat("Congratulation"); * $this->warn("Warning !"); * $this->inform("Did you know ?"); * $this->scold("What's wrong with you !"); */ }
如果您在应用程序中有两个上下文(例如前后端),我强烈建议您在每个方面覆盖控制器中的这些函数以传递自动上下文,如下所示
class BaseFrontController { /** * congrat user through flashbag : all happened successfully * Will automatically inject context * @param string $content */ public function congrat($content) { $content = array('body' => $content, 'context' => 'front'); $this->get('av.shortcuts')->congrat($content); } }
使用自定义库显示警报
TroopersAlertify 附带了一些库以简化使用,但您也可以使用自定义库(欢迎提交拉取请求,您的库可能对社区感兴趣)。您只需遵循以下步骤
troopers_alertify: contexts: front: engine: "myOwnSystem" layout: "bottomRight" \#it's up to your library translationDomain: "messages"
然后只需覆盖 app/Resources/TroopersAlertifyBundle/views/Modal/myOwnSystem.html.twig 并添加警报初始化。
选项
模态框
要调用模态框,请使用名为 'modal' 的闪存
$this->get('session')->getFlashBag()->set("success", array('engine' => 'modal', 'title' => "Wow", 'button_class' => "btn btn-primary btn-large", "body"=> "<div>Some info</div>"));
如您所见,您可以传递一些参数以自定义模态框,可用的是
title:
(html) string
button-class:
you con specify classes to customize your button
body:
html string
hasHeader:
boolean (default = true)
hasFooter:
boolean (default = true)
deleteIcon:
string : icon-class (for example: "fa fa-times")
id:
string : (default: "alertify-modal")
回调类型
您可以调用的一种最终类型的警报,回调类型允许您在项目中调用任何操作,这对于在警报中放置动态内容非常棒。要工作,被调用的操作必须渲染一个视图。这对于在模态框中包含表单非常有用。
$this->get('session')
->getFlashBag()
->set('callback', array(
'engine' => 'modal',
'title' => 'Wow',
'action' => 'AcmeBundle:Default:hello',
'button_class' => 'btn btn-primary btn-large',
'body' => '<p>Yeah that's crazy !</p>'
)
);
此类型非常简单易用,只需调用回调警报,并在选项中定义 "type" 为您想要的最终警报,"action" 为您想要调用的操作,以及您选择的警报的特定选项。
Ajax 模式
我们告诉您将 alertify 过滤器添加到您的布局中。这很棒,但如果您想在应用程序中使用 Ajax 呢?
实际上,这个库并不是专门为它设计的,但你可以简单地添加这部分代码来触发新Ajax内容的警告。
{% if app.request.isXmlHttpRequest %}
{{ app.session|alertify|raw }}
{% endif %}
确认模态框
在链接点击或表单提交后,我们有时希望提示用户确认他是否理解了自己所做的事情。你可以通过以下文档以简单的方式实现:(https://github.com/Troopers/TroopersAlertifyBundle/blob/master/README_Confirm.md)