dymantic/secretary

基本管理部分的短信和通知系统。它处理联系表单。

v0.2.0 2018-12-05 04:10 UTC

This package is auto-updated.

Last update: 2024-09-05 19:06:57 UTC


README

简单处理联系表单和类似功能的方法。至少,它可以让你免于再次处理那个该死的联系表单。只需将消息传递给秘书,它将相应地处理,无论是发送电子邮件、Slack消息等。每个消息都会在数据库中保存记录。默认情况下包含电子邮件和Slack消息,但很容易添加自己的。

安装和设置

步骤 1: 使用 composer 需求

composer require dymantic/secretary

Laravel 应自动发现 ServiceProvider 和 Facade。如果您不使用自动发现,您可以自己将其添加到您的应用配置中。

//in config/app.php
...
'providers' => [
    //...
    Dymantic\Secretary\SecretaryServiceProvider::class,
    //...
];

...

'aliases' => [
    //...
    'Secretary' => Dymantic\Secretary\Facades\Secretary::class,
];

步骤 2: 发布配置文件

php artisan vendor:publish --provider="Dymantic\Secretary\SecretaryServiceProvider"

步骤 3: 运行迁移

php artisan migrate

步骤 4: 在 config/secretary.php 中相应地设置您的配置。以下是一个示例

<?php

return [
  'sends_email_to' => 'hello@example.test',
  'slack_endpoint' => 'https://a-totoally-fake-slack-webhook.test/FAKE',
  'slack_recipient' => '#site_messages',
  'notification_channels' => ['mail', 'slack']
];

步骤 5: 使用它!

使用方法

一般的流程是创建一个新的实现 Dymantic\Secretary\SecretaryMessage 的消息,然后将其传递给秘书的 receive 方法。此包包含 Dymantic\Secretary\ContactMessage,对于大多数情况应该很好。

//ContactFormController.php // or whatever controller you use

public function handleContactForm(\Dymantic\Secretary\Secretary $secretary) {
    //validate your request

    //new up the message
    $message = new \Dymantic\Secretary\ContactMessage([
        'name' => request('name'),
        'email' => request('email'),
        'message_body' => request('message_body')
    ]);

    $secretary->receive($message);

    //you are done now
}

上面的是最常见的用例,因此此包包含一个表单请求以简化此过程。

//ContactFormController.php // or whatever controller you use

public function handleContactForm(\Dymantic\Secretary\Secretary $secretary, \Dymantic\Secretary\ContactForm $form) {
    $secretary->receive($form->contactMessage());
    //you are done now
}

上述代码将处理名称、电子邮件地址和消息正文的基礎验证。

额外的消息数据

除了消息的名称、电子邮件和消息正文字段外,还有一个消息_notes 字段,其中包含可以包含在消息中的额外数据。您可以在创建新消息时将它们作为键为 'message_notes' 的关联数组传递,或者如果您使用 ContactForm 表单请求对象,则可以传递字段。

//creating a message manually
$message = new \Dymantic\Secretary\ContactMessage([
        'name' => request('name'),
        'email' => request('email'),
        'message_body' => request('message_body'),
        'message_notes' => [
            'phone' => request('phone'),
            'company' => request('company')
        ]
    ]);

//or if using the form request object, just pass the fields you would like to take from the request as an array to the contactMessage method
$form->createMessage(['phone', 'company']);

** 注意:** 您负责验证额外字段。

数据库消息

您的秘书收到的每个消息都会保存在数据库中。模型是 Dymantic\Secretary\Message,它是一个用于此类用途的 eloquent 模型,因此您可以随意删除、编辑等。该模型包括一个 archive 方法来存档消息,以及一个 reinstate 方法,这是存档的相反。

秘书本身有一些用于检索消息的便利方法。

//get all messages
$secretary->getMessages();

//get archived messages
$secretary->getArchivedMessages();

//get messages from the last week (does NOT include archived messages)
$secretary->lastWeeksMessages();

//get messages from the last month (does NOT include archived messages)
$secretary->lastMonthsMessages();