文档
Welcome! 安装(Installation) 示例列表(List of examples) 依赖注入与服务定位器(Dependency Injection/Service Location) MVC 架构(The MVC Architecture) 使用控制器(Using Controllers) 使用模型(Working with Models) 模型元数据(Models Meta-Data) 事务管理(Model Transactions) Phalcon 查询语言(Phalcon Query Language (PHQL)) 缓存对象关系映射(Caching in the ORM) 对象文档映射 ODM (Object-Document Mapper) 使用视图(Using Views) 视图助手(View Helpers) 资源文件管理(Assets Management) Volt 模版引擎(Volt: Template Engine) MVC 应用(MVC Applications) 路由(Routing) 调度控制器(Dispatching Controllers) 微应用(Micro Applications) 使用命名空间(Working with Namespaces) 事件管理器(Events Manager) 请求环境 (Request Environment) 返回响应(Returning Responses) Cookie 管理(Cookies Management) 生成 URL 和 路径(Generating URLs and Paths) 闪存消息(Flashing Messages) 使用 Session 存储数据(Storing data in Session) 过滤与清理(Filtering and Sanitizing) 上下文编码(Contextual Escaping) 验证(Validation) 表单(Forms) 读取配置(Reading Configurations) 分页(Pagination) 使用缓存提高性能(Improving Performance with Cache) 安全(Security) 加密/解密( Encryption/Decryption ) 访问控制列表 ACL(Access Control Lists ACL) 多语言支持(Multi-lingual Support) 通用类加载器 ( Universal Class Loader ) 日志记录(Logging) 注释解析器(Annotations Parser) 命令行应用(Command Line Applications) 队列(Queueing) 数据库抽象层(Database Abstraction Layer) 国际化(Internationalization) 数据库迁移(Database Migrations) 调试应用程序(Debugging Applications) Phalcon 开发工具(Phalcon Developer Tools) 提高性能:下一步该做什么?(Increasing Performance: What's next?) 单元测试(Unit testing) 授权(License)
教程

发布于 2015-08-21 15:14:10 | 460 次阅读 | 评论: 0 | 来源: 网络整理

闪存消息(Flashing Messages)

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

适配器(Adapters)

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

适配器 描述 API
Direct 直接输出传递给flasher的消息 PhalconFlashDirect
Session 将消息临时存放于会话中,以便消息可以在后面的请求中打印出来 PhalconFlashSession

使用(Usage)

通常闪存消息都是来自服务容器的请求, 如果你正在使用 PhalconDIFactoryDefault , 那么 PhalconFlashDirect 将会作为 “flash” 服务自动注册:

<?php

use PhalconFlashDirect as FlashDirect;

// 建立flash服务
$di->set('flash', function () {
    return new FlashDirect();
});

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

<?php

use PhalconMvcController;

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

<?php

use PhalconFlashDirect as FlashDirect;

// 利用自定义的CSS类来注册flash服务
$di->set('flash', function () {
    $flash = new FlashDirect(
        array(
            '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 PhalconMvcController;

class ContactController extends Controller
{
    public function indexAction()
    {

    }

    public function saveAction()
    {
        // 存储POST

        // 使用直接闪存
        $this->flash->success("Your information was stored correctly!");

        // 转发到index动作
        return $this->dispatcher->forward(
            array(
                "action" => "index"
            )
        );
    }
}

或者使用一个HTTP重定向:

<?php

use PhalconMvcController;

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

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务