jralph/notification

1.1.1 2014-10-09 14:30 UTC

This package is auto-updated.

Last update: 2024-09-08 10:21:03 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

为 Laravel 4 提供简单易用的闪存通知设置。

扩展

默认情况下,通知由 Illuminate Session Store 处理,但可以很容易地通过实现提供的 Jralph\Notification\Contracts\Store 合约并使用 IoC 容器进行绑定来替换。完成之后,现有代码将继续正常工作。例如,您可以轻松地将 Session Store 替换为您自己的存储形式来存储闪存消息。

用法

Laravel

要在 Laravel 中使用通知,可以将服务提供者和别名添加到您的 app/config/app.php 文件中。

<?php
...
    'providers' => array(
        ...
        'Jralph\Notification\NotificationServiceProvider',
        ...
    ),
    ...
    'aliases' => array(
        ...
        'Notification' => 'Jralph\Notification\Facades\Notification'
        ...
    ),
...
?>

完成此操作后,您可以使用外观类如下。

<?php
    Notification::put('key', 'value');
?>

有关类提供的方法的更多信息,请参阅以下内容。

方法

PUT

put 方法将键 => 值消息闪存到会话中。

<?php
    Notification::put('key', 'value');
?>

GET

get 方法通过键从会话中检索值。

<?php
    Notification::get('key'); // If using the above put somewhere, this will return (string) 'value'.
?>

HAS

has 方法检查通知会话存储中是否存在键。

<?php
    Notification::has('key',); // If using the above put somewhere, this will return true.
?>

TAGS

tags 方法可以与 put 方法结合使用,以标记特定通知。

<?php
    Notification::tags(['tag1', 'tag2'])->put('key', 'value');
?>

TAG

tag 方法用于检索与特定标记相关联的通知数组。

<?php
    Notification::tag('tag1'); // Returns an array of all notifications tagged as `tag1`.
?>

示例用法

在一个处理登录的控制器中。

<?php

class AuthController extends BaseController {

    public function postLogin()
    {
        // Validate Form
        if ($validation->fails())
        {
            Notification::tags(['error'])->put('validation_failed', 'The form validation has failed.'); // Note you could pass an array as the value.
            return Redirect::back();
        }

        // Process User Login
    }

}
?>

在一个显示登录表单的视图中。

@foreach (Notification::tag('error') as $key => $notification)
    <div class="notification">
        <strong>{{ $key }}</strong> {{ $notification }}
    </div>
@endforeach

您也可以忽略标签,采用更简单的方法来显示验证失败的错误。

@if (Notification::has('validation_failed'))
    <div class="notification">
        <strong>Validation Failed</strong> {{ Notification::get('validation_failed') }}
    </div>
@endif

即将推出的功能

  • 添加一个不会被覆盖的默认标签。
    • 或者 添加 Notification::all() 方法来检索所有消息。