PHP code example of hahadu / laravel-notfication-sms
1. Go to this page and download the library: Download hahadu/laravel-notfication-sms library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hahadu / laravel-notfication-sms example snippets
namespace App\Notifications;
use Hahadu\LaravelSms\SmsNotification;
use Hahadu\LaravelSms\Message\SmsMessage;
class VerificationCodeNotification extends SmsNotification
{
private $code;
public function __construct(string $code)
{
// 传入模板参数和模板代码
parent::__construct(
['code' => $code], // 模板参数
'SMS_TEMPLATE_CODE' // 模板代码
);
$this->code = $code;
}
// 可选:自定义短信内容
public function toSms($notifiable): SmsMessage
{
return (new SmsMessage())
->content(['code' => $this->code])
->template('SMS_TEMPLATE_CODE')
->from('YourApp'); // 可选:自定义签名
}
}
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
// 添加路由方法指定接收短信的手机号
public function routeNotificationForSms($notification)
{
return $this->phone; // 返回手机号字段
}
}