【发布时间】:2021-03-20 02:45:42
【问题描述】:
我想向手机发送短信(如果他已经开启了双因素身份验证系统)。
所以在LoginController我添加了这个方法:
protected function authenticated(Request $request, $user)
{
return $this->loggendin($request , $user);
}
这个loggendin 方法位于一个名为TwoFactorAuthentication 的特征中,如下所示:
trait TwoFactorAuthenticate
{
public function loggendin(Request $request , $user)
{
if($user->hasTwoFactorAuthenticatedEnabled()) {
auth()->logout();
$request->session()->flash('auth' , [
'user_id' => $user->id,
'using_sms' => false,
'remember' => $request->has('remember')
]);
if($user->two_factor_type == 'sms') {
$code = ActiveCode::generateCode($user);
// Todo Send Sms
$request->user()->notify(new ActiveCodeNotification($code , $user->phone_number));
$request->session()->push('auth.using_sms' , true);
}
return redirect(route('twofa.token'));
}
return false;
}
}
现在的问题是当我想登录时,屏幕上会出现这条消息:
错误在 null 时调用成员函数 notify()
指的是这一行:
$request->user()->notify(new ActiveCodeNotification($code , $user->phone_number));
这个ActiveCodeNotification 包含一些用于发送短信的设置。
如果您想参观,这里是:
class ActiveCodeNotification extends Notification
{
use Queueable;
public $code;
public $phoneNumber;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($code , $phoneNumber)
{
$this->code = $code;
$this->phoneNumber = $phoneNumber;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [GhasedakChannel::class];
}
public function toGhasedakSms($notifiable)
{
return [
'text' => "{$this->code}",
'number' => $this->phoneNumber
];
}
}
那么这里出了什么问题,我在 null 上调用了一个成员函数 notify(),而它的两个参数都有值。
所以如果你知道,请告诉我。我真的很感谢你们的任何想法......
谢谢。
【问题讨论】: