cvo-technologies/cakephp-notifier

CvoTechnologies/Notifier 插件用于 CakePHP

安装: 128

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 1

开放问题: 0

类型:cakephp-plugin

1.0.0 2016-08-08 11:52 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:20:01 UTC


README

Software License Build Status Coverage Status Total Downloads Latest Stable Version

用法

配置通知传输

将以下部分添加到您的应用配置文件 app.php 中。

    'NotificationTransport' => [
        'email' => [
            'className' => 'CvoTechnologies/Notifier.Email',
            'profile' => 'default',
        ],
        'irc' => [
            'className' => 'Irc',
            'channel' => '#cvo-technlogies'
        ],
        'twitter' => [
            'className' => 'CvoTechnologies/Twitter.Twitter',
        ],
        'example' => [
            'className' => 'Example',
            'someOption' => true
        ]
    ],

创建通知器

namespace App\Notifier;

use CvoTechnologies\Notifier\Notifier;

class UserNotifier extends Notifier
{
    public function welcome($user)
    {
        $this
            ->to([
                'irc' => $user->irc_nickname,
                'twitter' => $user->twitter_nickname
            ])
            ->subject(sprintf('Welcome %s', $user->name))
            ->template('welcome_message') // By default template with same name as method name is used.
            ->viewVars([
                'user' => $user
            ])
            ->transports([
                'irc',
                'twitter'
            ]);
    }
}

创建通知模板

Template/Notification/transport-type 中创建一个模板文件。这将被用作通知的模板。

例如:Template/Notification/irc/welcome.ctp

Welcome <?= $user->name; ?> to our website!

使用它

使用通知器非常简单。以下是一个示例,展示如何在控制器中使用它

namespace App\Controller;

use CvoTechnologies\Notifier\NotifierAwareTrait;

class UsersController extends AppController
{
    use NotifierAwareTrait;

    public function register()
    {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->data())
            if ($this->Users->save($user)) {
                $this->getNotifier('User')->send('welcome', [$user]);
            }
        }
        $this->set('user', $user);
    }
}

创建传输

传输用于与特定服务通信。

它可以接受从应用配置中的 NotificationTransport 部分传递的配置选项。

<?php

namespace App\Notifier\Transport;

use CvoTechnologies\Notifier\AbstractTransport;

class ExampleTransport extends AbstractTransport
{
    const TYPE = 'example';

    /**
     * Send notification.
     *
     * @param \CvoTechnologies\Notifier\Notification $notification Notification instance.
     * @return array
     */
    public function send(Notification $notification)
    {
        // Send notificaiton
        $result = NotificationSendingService::send($notification->message(static::TYPE));

        return (array)$result;
    }
}