闪存消息Flashing Messages

闪存消息用于通知用户关于他/她产生的动作状态,或者简单地为用户显示一此信息。 这类消息可以使用这个组件来生成。

Flash messages are used to notify the user about the state of actions he/she made or simply show information to the users. These kind of messages can be generated using this component.

适配器Adapters

这个组件使用了适配器来定义消息传递给Flasher后的行为:

This component makes use of adapters to define the behavior of the messages after being passed to the Flasher:

AdapterDescriptionAPI
DirectDirectly outputs the messages passed to the flasherPhalcon\Flash\Direct
SessionTemporarily stores the messages in session, then messages can be printed in the next requestPhalcon\Flash\Session

使用Usage

通常闪存消息都是来自服务容器的请求, 如果你正在使用 Phalcon\DI\FactoryDefault, 那么 :doc:`Phalcon\Flash\Direct <../api/Phalcon_Flash_Direct>`将会作为 “flash” 服务自动注册:

Usually the Flash Messaging service is requested from the services container, if you’re using Phalcon\DI\FactoryDefault then Phalcon\Flash\Direct is automatically registered as “flash” service:

  1. <?php
  2. use Phalcon\Flash\Direct as FlashDirect;
  3. //Set up the flash service
  4. $di->set('flash', function() {
  5. return new FlashDirect();
  6. });

这样的话,你便可以在控制器或者视图中通过在必要的片段中注入此服务来使用它:

This way, you can use it in controllers or views by injecting the service in the required scope:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class PostsController extends Controller
  4. {
  5. public function indexAction()
  6. {
  7. }
  8. public function saveAction()
  9. {
  10. $this->flash->success("The post was correctly saved!");
  11. }
  12. }

目前已支持的有四种内置消息类型:

There are four built-in message types supported:

  1. <?php
  2. $this->flash->error("too bad! the form had errors");
  3. $this->flash->success("yes!, everything went very smoothly");
  4. $this->flash->notice("this a very important information");
  5. $this->flash->warning("best check yo self, you're not looking too good.");

你可以用你自己的类型来添加消息:

You can add messages with your own types:

  1. <?php
  2. $this->flash->message("debug", "this is debug message, you don't say");

输出信息Printing Messages

发送给flash服务的消息将会自动格式成html:

Messages sent to the flash service are automatically formatted with html:

  1. <div class="errorMessage">too bad! the form had errors</div>
  2. <div class="successMessage">yes!, everything went very smoothly</div>
  3. <div class="noticeMessage">this a very important information</div>
  4. <div class="warningMessage">best check yo self, you're not looking too good.</div>

正如你看到的,CSS的类将会自动添加到div中。这些类允许你定义消息在浏览器上的图形表现。 此CSS类可以被重写,例如,如果你正在使用Twitter的bootstrap,对应的类可以这样配置:

As you can see, CSS classes are added automatically to the DIVs. These classes allow you to define the graphical presentation of the messages in the browser. The CSS classes can be overridden, for example, if you’re using Twitter bootstrap, classes can be configured as:

  1. <?php
  2. use Phalcon\Flash\Direct as FlashDirect;
  3. //Register the flash service with custom CSS classes
  4. $di->set('flash', function(){
  5. $flash = new FlashDirect(array(
  6. 'error' => 'alert alert-error',
  7. 'success' => 'alert alert-success',
  8. 'notice' => 'alert alert-info',
  9. ));
  10. return $flash;
  11. });

然后消息会是这样输出:

Then the messages would be printed as follows:

  1. <div class="alert alert-error">too bad! the form had errors</div>
  2. <div class="alert alert-success">yes!, everything went very smoothly</div>
  3. <div class="alert alert-info">this a very important information</div>

隐式刷送与会话Implicit Flush vs. Session

依赖于发送消息的适配器,它可以立即产生输出,也可以先临时将消息存放于会话中随后再显示。 你何时应该使用哪个?这通常依赖于你在发送消息后重定向的类型。例如, 如果你用了“转发”则不需要将消息存放于会话中,但如果你用的是一个HTTP重定向,那么则需要存放于会话中:

Depending on the adapter used to send the messages, it could be producing output directly, or be temporarily storing the messages in session to be shown later. When should you use each? That usually depends on the type of redirection you do after sending the messages. For example, if you make a “forward” is not necessary to store the messages in session, but if you do a HTTP redirect then, they need to be stored in session:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class ContactController extends Controller
  4. {
  5. public function indexAction()
  6. {
  7. }
  8. public function saveAction()
  9. {
  10. //store the post
  11. //Using direct flash
  12. $this->flash->success("Your information was stored correctly!");
  13. //Forward to the index action
  14. return $this->dispatcher->forward(array("action" => "index"));
  15. }
  16. }

或者使用一个HTTP重定向:

Or using a HTTP redirection:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class ContactController extends Controller
  4. {
  5. public function indexAction()
  6. {
  7. }
  8. public function saveAction()
  9. {
  10. //store the post
  11. //Using session flash
  12. $this->flashSession->success("Your information was stored correctly!");
  13. //Make a full HTTP redirection
  14. return $this->response->redirect("contact/index");
  15. }
  16. }

在这种情况下,你需要手动在交互的视图上打印消息:

In this case you need to manually print the messages in the corresponding view:

  1. <!-- app/views/contact/index.phtml -->
  2. <p><?php $this->flashSession->output() ?></p>

“flashSession”属性是先前在依赖注入容器中设置的闪存。 为了能成功使用flashSession消息者,你需要先启动 session 。

The attribute ‘flashSession’ is how the flash was previously set into the dependency injection container. You need to start the session first to successfully use the flashSession messenger.