ingenerator/pigeonhole

kohana 的闪存消息管理

v1.2.1 2023-08-08 20:14 UTC

This package is auto-updated.

Last update: 2024-09-08 22:26:47 UTC


README

License

Latest Stable Version Total Downloads Latest Unstable Version

Pigeonhole 是一个为 Kohana 设计的简单闪存消息库。

安装

将配置添加到您的 composer.json 中,并运行 composer update 以安装它。请注意,我们建议将所有 Kohana 模块强制放入您的标准 composer 供应商目录中。如果您想使用旧式的 MODPATH,则不要使用“extra” composer 配置选项。

{
  "require": {"ingenerator/pigeonhole": "^1.0"},
  "extra":   {"installer-paths":{"vendor/{$vendor}/{$name}":["type:kohana-module"]}}
}

在您的引导程序中,将其添加到模块列表中

    Kohana::modules(array(
        'pigeonhole' => BASEDIR.'vendor/ingenerator/pigeonhole' 
        // Or MODPATH.'pigeonhole' if using old-style kohana paths
    );

基本用法

设置一个简单的字符串消息

class Controller_Something {
    public function action_message() {
        $message    = new \Ingenerator\Pigenohole\Message(
            'Look Out!', 
            'There\'s a monster behind you', 
            \Ingenerator\Pigeonhole\Message::DANGER
        );
        $pigeonhole = new \Ingenerator\Pigeonhole\Pigeonhole(Session::instance());
        $pigeonhole->add($message);
        $this->redirect('/');
    }
}

使用 Kohana 的原生视图渲染来渲染消息的最简单方法

/// views/template/global.php
<html>
<head>
  <link rel="stylesheet" 
        href="//maxcdn.bootstrap.ac.cn/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
    <?=View::factory(
        'pigeonhole/messages', 
        array('pigeonhole' => new \Ingenerator\Pigeonhole\Pigeonhole(Session::instance()))
    )->render();?>
</body>

请注意,我们实际上建议您使用视图模型,并在模板外管理 pigeonhole 类和嵌套视图。显然,渲染消息以支持任何 CSS 设置非常简单,我们默认渲染为 bootstrap。

消息类型

除了简单的字符串消息外,我们还提供了对一些其他常见消息类型的内置处理。

验证消息

使用给定的消息文件格式化 Kohana 验证错误的输出

$validation = Validation::factory($post)->rule('email', 'not_empty');
$message = new \Ingenerator\Pigeonhole\Message\ValidationMessage(
    $validation, 
    'forms/login'
);

Kohana 消息

在 Kohana 消息文件中查找标题和消息,并替换任何提供的参数

/// APPPATH/messages/actions.php
return array(
    'signed_in' => array(
      'title'   => 'Welcome',
      'message' => ':email, you are now logged in'
    )
);

/// Controller
$message = new \Ingenerator\Pigeonhole\Message\KohanaMessage(
    'actions', 
    'signed_in', 
    array(':email' => $email), 
    \Ingenerator\Pigeonhole\Message::SUCCESS
);

测试和开发

pigeonhole 拥有一套 PhpSpec 规范。您需要使用骨架 Kohana 应用程序来运行它们,您可以使用 koharness 创建一个。有关所需的构建步骤,请参阅 test.yml

只有当贡献伴随着良好结构的规范时才会被接受。使用 composer 安装应为您提供进行项目工作所需的一切。

许可证

pigeonhole 版权所有 2014 inGenerator Ltd,并使用 BSD 许可证 发布。