发送消息(Flashing Messages) ================================== 发送消息被用于向用户发送请求状态或用户行为等。使用此组件可以生成消息。 适配器(Adapters) --------------------------- 此组件使用适配器来决定使用哪种行为向用户发送消息: +---------+-----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------+ | Adapter | Description | API | +=========+===============================================================================================+============================================================================+ | Direct | Directly outputs the messages passed to the flasher | :doc:`Phalcon\\Flash\\Direct <../api/Phalcon_Flash_Direct>` | +---------+-----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------+ | Session | Temporarily stores the messages in session, then messages can be printed in the next request | :doc:`Phalcon\\Flash\\Session <../api/Phalcon_Flash_Session>` | +---------+-----------------------------------------------------------------------------------------------+----------------------------------------------------------------------------+ 使用方法(Usage) --------------------------- 通常情况下,消息发送这个服务被注册到服务容器中,如果你使用 :doc:`Phalcon\\DI\\FactoryDefault <../api/Phalcon_DI_FactoryDefault>`,那么默认将自动注册 "flash"服务为类型 :doc:`Phalcon\\Flash\\Direct <../api/Phalcon_Flash_Direct>`: .. code-block:: php set('flash', function() { return new \Phalcon\Flash\Direct(); }); 通过这种方式,你可以直接在控制器或视图文件中访问此服务: .. code-block:: php flash->success("The post was correctly saved!"); } } 内置支持的消息类型有: .. code-block:: php 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."); 你也可以增加你自己的消息类型: .. code-block:: php flash->message("debug", "this is debug message, you don't say"); 输出消息 ----------------- 消息发送到客户端会自动转化为HTML: .. code-block:: html
flashSession->output() ?>
属性 'flashSession' 是之前注册'flash'服务到容器中时产生的。