如何在Laravel API控制器中发送会话
我正在与Laravel 5.8合作,并试图为移动应用程序开发人员构建我的API。
基本上,我制作了一个具有这样方法的API registerController
:
public function register(Request $request)
{
// Validation Data
$validData = $this->validate($request, [
'user_input' => 'required|unique:users,usr_name|unique:members,mbr_mobile|regex:/^09\d{9}$/|max:11|min:11',
]);
// sending verify sms
$sms = new SendSms(request()->all()['user_input'],43,request()->all());
$sms->send();
// submit session ?
return response([
'data' => 'An sms sent to you in order to verify',
'status' => 200
]);
}
因此,它只是将验证代码发送给用户手机,现在我需要以另一种方法进行验证:
public function verifyCode(Request $request)
{
DB::beginTransaction();
try {
// need the retrieved session phone number here
$check = DB::table('verify_sms')
->whereVfsVerifyCode($request->verifyCode)
->whereVfsMobileNumber(session("req")['user_input'])
->whereRaw("(UNIX_TIMESTAMP() - vfs_time) < ?", [180])
->first();
if (!empty($check)) {
$user = User::Create([
'usr_name' => session("req")["user_input"],
'usr_is_active' => 1,
]);
DB::table('verify_sms')->whereVfsId($check->vfs_id)->update(['vfs_status' => '1']);
DB::commit();
Auth::loginUsingId($user->usr_id);
return response([
'status' => 'success'
],200);
} else {
return response([
'data' => 'Something goes wrong',
'status' => 'error',
],401);
}
} catch (\Exception $e) {
dd($e);
DB::rollBack();
}
}
现在问题是,这里的问题是,我需要用户输入的电话号码,但不知道如何将其作为API控制器中的会话发送!
因此,如果您知道如何正确执行此操作,请让我知道...
I'm working with Laravel 5.8 and I'm trying to build my API for mobile app developer.
Basically I have made an API RegisterController
that has a method like this:
public function register(Request $request)
{
// Validation Data
$validData = $this->validate($request, [
'user_input' => 'required|unique:users,usr_name|unique:members,mbr_mobile|regex:/^09\d{9}$/|max:11|min:11',
]);
// sending verify sms
$sms = new SendSms(request()->all()['user_input'],43,request()->all());
$sms->send();
// submit session ?
return response([
'data' => 'An sms sent to you in order to verify',
'status' => 200
]);
}
So it simply sends a verification code to user mobile phone and now I need to verify it at another method:
public function verifyCode(Request $request)
{
DB::beginTransaction();
try {
// need the retrieved session phone number here
$check = DB::table('verify_sms')
->whereVfsVerifyCode($request->verifyCode)
->whereVfsMobileNumber(session("req")['user_input'])
->whereRaw("(UNIX_TIMESTAMP() - vfs_time) < ?", [180])
->first();
if (!empty($check)) {
$user = User::Create([
'usr_name' => session("req")["user_input"],
'usr_is_active' => 1,
]);
DB::table('verify_sms')->whereVfsId($check->vfs_id)->update(['vfs_status' => '1']);
DB::commit();
Auth::loginUsingId($user->usr_id);
return response([
'status' => 'success'
],200);
} else {
return response([
'data' => 'Something goes wrong',
'status' => 'error',
],401);
}
} catch (\Exception $e) {
dd($e);
DB::rollBack();
}
}
Now the problem here is that, I need the phone number that user had entered but don't know how to send that as session in API Controllers!
So if you know how to properly do this, please let me know...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要回答特定的问题,您可以使用 session helper 并在注册功能中的会话中写入电话号码:
and than retrieve it from session in verifyCode:
However, better approach would be if you make another Model / Db table "TempUser" and store user data there.
我的方法是我不会验证电话号码。我认为检查通过SMS发送的“验证代码”足够。
To answer specific question, you could use session helper and write phone number in session in register function:
and than retrieve it from session in verifyCode:
However, better approach would be if you make another Model / Db table "TempUser" and store user data there.
My approach would be that I would not validate phone number. I think checking "verifyCode" that was sent via sms in enough.
成功注册后,生成令牌并将其添加到标题中。令牌将验证用户,因此您无需获取手机号码,但是您可以通过调用Auth() - &gt; user() - &gt; Mobile_phone获得手机号码。
After you have successfully registered, generate the token and add it to the header. The token will verify the user, so you don't need to get the mobile number, but you can get the mobile number by calling auth()->user()->mobile_phone.