# 閃存消息(Flashing Messages)
閃存消息用于通知用戶關于他/她產生的動作狀態,或者簡單地為用戶顯示一此信息。 這類消息可以使用這個組件來生成。
## 適配器(Adapters)
這個組件使用了適配器來定義消息傳遞給Flasher后的行為:
| 適配器 | 描述 | API |
| --- | --- | --- |
| Direct | 直接輸出傳遞給flasher的消息 | [Phalcon\\Flash\\Direct](http://docs.iphalcon.cn/api/Phalcon_Flash_Direct.html) |
| Session | 將消息臨時存放于會話中,以便消息可以在后面的請求中打印出來 | [Phalcon\\Flash\\Session](http://docs.iphalcon.cn/api/Phalcon_Flash_Session.html) |
## 使用(Usage)
通常閃存消息都是來自服務容器的請求. 如果你正在使用[Phalcon\\Di\\FactoryDefault](http://docs.iphalcon.cn/api/Phalcon_Di_FactoryDefault.html), 那么[Phalcon\\Flash\\Direct](http://docs.iphalcon.cn/api/Phalcon_Flash_Direct.html)將會作為 “flash” 服務自動注冊 和[Phalcon\\Flash\\Session](http://docs.iphalcon.cn/api/Phalcon_Flash_Session.html)將會作為 “flashSession” 服務自動注冊. You can also manually register it:
~~~
<?php
use Phalcon\Flash\Direct as FlashDirect;
use Phalcon\Flash\Session as FlashSession;
// 建立flash服務
$di->set(
"flash",
function () {
return new FlashDirect();
}
);
// 建立flashSession服務
$di->set(
"flashSession",
function () {
return new FlashSession();
}
);
~~~
這樣的話,你便可以在控制器或者視圖中通過在必要的片段中注入此服務來使用它:
~~~
<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
public function indexAction()
{
}
public function saveAction()
{
$this->flash->success("The post was correctly saved!");
}
}
~~~
目前已支持的有四種內置消息類型:
~~~
<?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.");
~~~
你可以用你自己的類型來添加消息:
~~~
<?php
$this->flash->message("debug", "this is debug message, you don't say");
~~~
## 輸出信息(Printing Messages)
發送給flash服務的消息將會自動格式成html:
~~~
<div class="errorMessage">too bad! the form had errors</div>
<div class="successMessage">yes!, everything went very smoothly</div>
<div class="noticeMessage">this a very important information</div>
<div class="warningMessage">best check yo self, you're not looking too good.</div>
~~~
正如你看到的,CSS的類將會自動添加到:code:[`](http://docs.iphalcon.cn/reference/flash.html#id1)`中。這些類允許你定義消息在瀏覽器上的圖形表現。 此CSS類可以被重寫,例如,如果你正在使用Twitter的Bootstrap,對應的類可以這樣配置:
~~~
<?php
use Phalcon\Flash\Direct as FlashDirect;
// 利用自定義的CSS類來注冊flash服務
$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;
}
);
~~~
然后消息會是這樣輸出:
~~~
<div class="alert alert-danger">too bad! the form had errors</div>
<div class="alert alert-success">yes!, everything went very smoothly</div>
<div class="alert alert-info">this a very important information</div>
<div class="alert alert-warning">best check yo self, you're not looking too good.</div>
~~~
## 絕對刷送與會話(Implicit Flush vs. Session)
依賴于發送消息的適配器,它可以立即產生輸出,也可以先臨時將消息存放于會話中隨后再顯示。 你何時應該使用哪個?這通常依賴于你在發送消息后重定向的類型。例如, 如果你用了“轉發”則不需要將消息存放于會話中,但如果你用的是一個HTTP重定向,那么則需要存放于會話中:
~~~
<?php
use Phalcon\Mvc\Controller;
class ContactController extends Controller
{
public function indexAction()
{
}
public function saveAction()
{
// 存儲POST
// 使用直接閃存
$this->flash->success("Your information was stored correctly!");
// 轉發到index動作
return $this->dispatcher->forward(
[
"action" => "index"
]
);
}
}
~~~
或者使用一個HTTP重定向:
~~~
<?php
use Phalcon\Mvc\Controller;
class ContactController extends Controller
{
public function indexAction()
{
}
public function saveAction()
{
// 存儲POST
// 使用會話閃存
$this->flashSession->success("Your information was stored correctly!");
// 返回一個完整的HTTP重定向
return $this->response->redirect("contact/index");
}
}
~~~
在這種情況下,你需要手動在交互的視圖上打印消息:
~~~
<!-- app/views/contact/index.phtml -->
<p><?php $this->flashSession->output() ?></p>
~~~
“flashSession”屬性是先前在依賴注入容器中設置的閃存。 為了能成功使用flashSession消息者,你需要先啟動[session](http://docs.iphalcon.cn/reference/session.html)。
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl