sms for Yii2
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yiier/yii2-smser "*"
or add
"yiier/yii2-smser": "*"
to the require section of your composer.json
file.
Run the following command
$ php yii migrate --migrationPath=@yiier/smser/migrations/
Once the extension is installed, simply modify your application configuration as follows:
<?php
return [
// something code
'components' => [
'smser' => [
'class' => 'yiier\smser\Smser',
'config' => [
'gateways' => [
// 云片网
'yunpian' => [
'api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxx',
],
],
]
]
]
];
More detail overtrue/easy-sms 使用
Validator
<?php
class SignupForm extends \yii\base\Model
{
// something code
public $verifyCode;
/**
* @inheritdoc
*/
public function rules()
{
return [
// something code
// usage is Optional
['verifyCode', '\yiier\smser\validators\SmsCodeValidator', 'usage' => 'Signup'],
];
}
}
send code && update code status
<?php
// send code
Yii::$app->smser->send(18688888888, [
'content' => '您的验证码为: 1234',
'template' => 'SMS_001',
'data' => ['code' => 1234, 'usage' => 'Signup'], // or 'data' => ['code' => 1234],
]);
// update used status
\yiier\smser\models\SmsLog::used(18688888888, 1234);