添加提供器文件
# php artisan make:provider SmsServiceProvider
编辑文件
<?php
namespace App\Providers;
use App\Service\SmsService;
use Illuminate\Support\ServiceProvider;
class SmsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//使用bind绑定实例到接口以便依赖注入
//使用singleton绑定单例
$this->app->singleton('sms',function(){
return new SmsService();
});
}
}
3、绑定
编辑 config/app.php 文件
'providers' => [
...
\App\Providers\SmsServiceProvider::class, // 添加
],
3、使用
$smsProvider = \App::make('sms');
$sms = $smsProvider->verifySms($param['mobile'], $param['sms']);
if ($sms == false) {
return showJson(102, '短信验证码错误');
}