php 基于yii2框架编写插件并提交到composer

本文介绍了如何在Yii2框架中编写自定义PHP插件,包括Mail.php和Message.php的实现,并详细说明了如何将插件提交到GitHub以及配置composer,以实现在代码更新时自动同步到项目。
摘要由CSDN通过智能技术生成
在yii2框架中基于swiftmailer和redis编写自定义异步发送邮件的插件,并提交到composer


1、在vendor中创建目录结构如下:
lvfk 此文件夹下可以由多个插件
└── mailerqueue 插件名称
    └── src 存放插件源码
        ├── Mailer.php 继承\yii\swiftmailer\Mailer,自定参数并正真发送Redis中存储的邮件
        └── Message.php 继承\yii\swiftmailer\Message,改写原有发送邮件功能为存储到Redis列表中

2、编写Mail.php 和 Message.php

Message.php
<?php
/**
 * Message.php.
 * User: lvfk
 * Date: 2018/2/3 0003
 * Time: 16:40
 * Desc:
 */

namespace lvfk\mailerqueue;


use yii\base\InvalidConfigException;

class Message extends \yii\swiftmailer\Message
{
    /**
     * 存储邮件到redis队列中
     * @return mixed
     * @throws InvalidConfigException
     */
    public function queue(){
        //检测redis
        $redis = \Yii::$app->redis;
        if(empty($redis)){
            throw new InvalidConfigException('redis not found in config');
        }

        //检测mailer
        $mailer = \Yii::$app->mailer;
        if(empty($redis) || !$redis->select($mailer->db) || empty($mailer->key)){
            throw new InvalidConfigException('mailer not found in config');
        }
        if(empty($mailer->key)){
            throw new InvalidConfigException('parameter key not found in mailer config');
        }
        if(!$redis->select($mailer->db)){
            throw new InvalidConfigException('parameter db not found in mailer config');
        }


        //发送
        $message = [];
        $message['from'] = array_keys($this->from);
        $message['to'] = array_keys($this->getTo());
        $message['cc'] = !empty($this->getCc())?array_keys($this->getCc()):[];
        $message['bcc'] = !empty($this->getBcc())?array_keys($this->getBcc()):[];
        $message['reply_to'] = !empty($this->getReplyTo())?array_keys($this->getReplyTo()):[];
        $message['charset'] = $this
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值