laravel - 参数 1 传递给 Illuminate\Auth\SessionGuard::login()
问题描述
我已经尝试了类似问题的答案中的所有内容,但我仍然陷入此错误,请帮助我。我知道它来自哪里,但我不知道如何解决这个问题。
<?php
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/verify';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'mobile' => ['required', 'min:10'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $request
* @return string
* @throws \Twilio\Exceptions\ConfigurationException
*/
protected function create(array $data)
{
/* Get credentials from .env */
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio_sid = getenv("TWILIO_SID");
$twilio_verify_sid = getenv("TWILIO_VERIFY_SID");
$twilio = new Client($twilio_sid, $token);
$twilio->verify->v2->services($twilio_verify_sid)
->verifications
->create('+977'.$data['mobile'], "sms");
User::create([
'name' => $data['name'],
'mobile' => $data['mobile'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
return redirect()->route('verify')->with('mobile', $data->mobile);
}
protected function verify(Request $request)
{
$data = $request->validate([
'otp' => ['required', 'numeric'],
'mobile' => ['required', 'string'],
]);
/* Get credentials from .env */
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio_sid = getenv("TWILIO_SID");
$twilio_verify_sid = getenv("TWILIO_VERIFY_SID");
$twilio = new Client($twilio_sid, $token);
$verification = $twilio->verify->v2->services($twilio_verify_sid)
->verificationChecks
->create($data['otp'], array('to' => $data['mobile']));
if ($verification->valid) {
$user = tap(User::where('mobile', $data['mobile']))->update(['isVerified' => true]);
/* Authenticate user */
Auth::login($user->first());
return redirect()->route('dashboard')->with(['message' => 'Phone number verified']);
}
return back()->with(['mobile' => $data['mobile'], 'error' => 'Invalid verification code entered!']);
}
}
这是我的路线:
<?php
Route::get('/', 'HomeController@index');
Auth::routes();
Route::get('/verify', function () {
return view('backend.verify');
})->name('verify');
Route::get('/', function () {
return view('auth.register');
})->name('register');
Route::get('/dashboard', 'HomeController@index')->name('home');
Route::post('/verify', 'RegisterController@verify')->name('verify');
Route::group(['middleware' => 'auth'], function () {
Route::resource('user', 'UserController', ['except' => ['show']]);
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController@edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController@update']);
Route::put('profile/password', ['as' => 'profile.password', 'uses' => 'ProfileController@password']);
});
解决方案
创建方法必须返回 $user 对象。您不能从 RegisterController 的 create 方法重定向到任何路由。有一些方法可以处理这个问题。
像这样覆盖 web.php 中的默认注册路由。
Route::post('/register', 'Auth\RegisterController@created')->name('register');
然后在您的注册控制器中将参数从类型数组更改为 Illuminate\Http\Request 像这样。
protected function created(Request $data)
{
/* Get credentials from .env */
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio_sid = getenv("TWILIO_SID");
$twilio_verify_sid = getenv("TWILIO_VERIFY_SID");
$twilio = new Client($twilio_sid, $token);
$twilio->verify->v2->services($twilio_verify_sid)
->verifications
->create('+977'.$data->mobile, "sms");
User::create([
'name' => $data->name,
'mobile' => $data->mobile,
'email' => $data->email,
'password' => Hash::make($data->password),
]);
return redirect()->route('verify')->with('mobile', $data->mobile);
}
希望这能解决您的问题。:)
推荐阅读
- javascript - 如何使用 JavaScript 在本地存储用户输入
- c# - Asp.net MVC 5 使控制器动作接受休息类型参数
- php - Symfony 4,find() 一个实体
- php - 代客不适用于 WordPress 网站:建立数据库连接时出错
- c++ - 用c ++中函数的返回值初始化一个类
- java - Java (Selenium) - 用于初始化子类的超类构造函数
- javascript - 无法使用 react.js 将其他文件导入 client.js
- sql - Postgres - 查询以从多个表中选择字段作为列
- python - 如何从 Python 中的内部类访问外部类成员?
- c++ - C ++如何从迭代器(内部类)访问集合成员变量-这有意义吗?