多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 闪存消息 Flash消息用于向用户通知他/她所做的操作的状态,或者仅向用户显示信息。可以使用此组件生成这些类型的消息。 ## 适配器 此组件使用适配器在传递给Flasher后定义消息的行为: | 适配器 | 描述 | API | | :-----: | :----------------------------------------------------: | :-------------------: | | Direct | 直接输出传递给闪存器的消息 | Phalcon\\Flash\\Direct | | Session | 暂时将消息存储在会话中,然后可以在下一个请求中打印消息 | Phalcon\\Flash\\Session | ## 使用 通常,从服务容器请求Flash消息传递服务。如果您正在使用 `Phalcon\\Di\\FactoryDefault`,则 `Phalcon\\Flash\\Direct` 会自动注册为flash 服务, `Phalcon\\Flash\\Session` 会自动注册为 `flashSession` 服务。您也可以手动注册它: ```php <?php use Phalcon\\Flash\\Direct as FlashDirect; use Phalcon\\Flash\\Session as FlashSession; // 设置闪存服务 $di->set( 'flash', function () { return new FlashDirect(); } ); // Set up the flash session service $di->set( 'flashSession', function () { return new FlashSession(); } ); ``` 这样,您可以在控制器或视图中使用它: ```php <?php use Phalcon\\Mvc\\Controller; class PostsController extends Controller { public function indexAction() { } public function saveAction() { $this\->flash\->success('The post was correctly saved!'); } } ``` 支持四种内置消息类型: ```php <?php $this->flash->error('too bad! the form had errors'); $this->flash->success('yes!, everything went very smoothly'); $this->flash->notice('this a very important information'); $this->flash->warning("best check yo self, you're not looking too good."); ``` 您还可以使用 `message()` 方法添加具有您自己类型的消息: ```php <?php $this->flash->message('debug', "this is debug message, you don't say"); ``` ## 打印消息 发送到闪存服务的消息将自动使用HTML格式化: ```php too bad! the form had errors yes!, everything went very smoothly this a very important information best check yo self, you're not looking too good. ``` 如您所见,CSS类会自动添加到 `` 中。这些类允许您在浏览器中定义消息的图形表示。可以覆盖CSS类,例如,如果您使用的是Twitter Bootstrap,则可以将类配置为: ```php <?php use Phalcon\\Flash\\Direct as FlashDirect; // Register the flash service with custom CSS classes $di->set( 'flash', function () { $flash = new FlashDirect( [ 'error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning', ] ); return $flash; } ); ``` 然后消息将打印如下: ```php too bad! the form had errors yes!, everything went very smoothly this a very important information best check yo self, you're not looking too good. ``` ## 隐式刷新与Session 根据用于发送消息的适配器,它可以直接生成输出,或者暂时将消息存储在会话中以便稍后显示。你应该什么时候使用?这通常取决于您在发送邮件后执行的重定向类型。例如,如果您没有必要将转发存储在会话中,但是如果您执行HTTP重定向,则需要将它们存储在会话中: ```php <?php use Phalcon\\Mvc\\Controller; class ContactController extends Controller { public function indexAction() { } public function saveAction() { // Store the post // Using direct flash $this->flash->success('Your information was stored correctly!'); // Forward to the index action return $this->dispatcher->forward([ 'action' => 'index']); } } ``` 或者使用HTTP重定向: ```php <?php use Phalcon\\Mvc\\Controller; class ContactController extends Controller { public function indexAction() {} public function saveAction(){ // Store the post // Using session flash $this->flashSession->success('Your information was stored correctly!'); // Make a full HTTP redirection return $this->response->redirect('contact/index'); } } ``` 在这种情况下,您需要在相应的视图中手动打印消息: ```php flashSession->output() ?> ``` 属性 flashSession 是先前将flash设置到依赖项注入容器中的方式。您需要首先启动session才能成功使用flashSession消息。