#php #authentication #eloquent #laravel-5.2
#php #аутентификация #красноречивый #laravel-5.2
Вопрос:
Я пытаюсь создать нового пользователя, и это запись в user_info
таблице, все записи в базе данных создаются, но я вижу эту ошибку, когда пытаюсь перенаправить вновь зарегистрированного пользователя.
Аргумент 1, переданный в IlluminateAuthSessionGuard::login() должен реализовывать интерфейс IlluminateContractsAuthAuthenticatable, экземпляр IlluminateHttpRedirectResponse, вызывается в /var/www/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php в строке 35 и определен
Мой контроллер
<?php
namespace AppHttpControllersAuth;
use AppUser;
use AppUserInfo;
use IlluminateSupportFacadesAuth;
use Validator;
use AppHttpControllersController;
use IlluminateFoundationAuthRegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* 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 IlluminateContractsValidationValidator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'username' => 'required|max:255|unique:users',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6',
'confirm_email' => 'required|email|max:255|same:email',
'month' => 'required',
'day' => 'required',
'year' => 'required',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
$dateString = $data['year'].'-'.$data['month'].'-'.$data['day'];
$date=date_create($dateString);
$dateString = date_format($date, 'Y-m-d');
// echo $dateString;
$user = User::create([
'username' => $data['username'],
'email' => $data['email'],
'uuid' => $this->getRandomString(),
'password' => bcrypt($data['password']),
'user_type' => 2,
'status' => 'active'
]);
$lastInsertId = $user->id;;
//send mail to user to notify, account has been created
$to = $user->email;
$from = "noreply@project.com";
$msg = "Welcome! This is free for Mobile, Tablets and Computers. Listen and create the right messge with your music, where ever you are";
$subject = "Welcome to Telatunes";
mail($to, $subject, $msg);
//create record in user info
$userInfo = UserInfo::create([
'user_id' => $user->id,
'address' => '',
'date_of_birth' => $dateString,
'zipcode' => '',
'country_id' => 0,
'state_id' => 0,
'city_id' => 0,
'profile_pic_path' => '',
'gender' => 'male'
]);
return redirect('redirect');
// return User::create([
// 'name' => $data['name'],
// 'email' => $data['email'],
// 'password' => bcrypt($data['password']),
// ]);
}
private function getRandomString($randomStringLength = 15) {
$fullString = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for($i=0; $i<$randomStringLength; $i ) {
$randomIndex = mt_rand(0, 61);
$randomString .= $fullString[$randomIndex];
}
return $randomString;
}
}
Ответ №1:
Чтобы изменить URL перенаправления, вам нужно отредактировать эту строку:
protected $redirectTo = '/home';
Ваша функция create должна возвращать массив из метода «create».
protected function create(array $data)
{
$dateString = $data['year'].'-'.$data['month'].'-'.$data['day'];
$date=date_create($dateString);
$dateString = date_format($date, 'Y-m-d');
//send mail to user to notify, account has been created
$to = $user->email;
$from = "noreply@project.com";
$msg = "Welcome! This is free for Mobile, Tablets and Computers. Listen and create the right messge with your music, where ever you are";
$subject = "Welcome to Telatunes";
mail($to, $subject, $msg);
//create record in user info
$userInfo = UserInfo::create([
'user_id' => $user->id,
'address' => '',
'date_of_birth' => $dateString,
'zipcode' => '',
'country_id' => 0,
'state_id' => 0,
'city_id' => 0,
'profile_pic_path' => '',
'gender' => 'male'
]);
return User::create([
'username' => $data['username'],
'email' => $data['email'],
'uuid' => $this->getRandomString(),
'password' => bcrypt($data['password']),
'user_type' => 2,
'status' => 'active'
]);
}
Переменная $ lastInsertId могла быть проблемой, но оказалось, что вы ее не используете, поэтому я ее удалил.