Laravel-注册后重定向用户

时间:2016-10-07 22:58:04

标签: php authentication eloquent laravel-5.2

我正在尝试创建一个新用户及其在user_info表中的记录,所有数据库条目都在制作中,但是当我尝试路由新注册的用户时,我看到了这个错误。

传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须实现接口Illuminate \ Contracts \ Auth \ Authenticatable,给出的Illuminate \ Http \ RedirectResponse实例,在/ var / www / myproject / vendor中调用第35行/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php并定义

我的控制器是

    <?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\UserInfo;
use Illuminate\Support\Facades\Auth;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;


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 \Illuminate\Contracts\Validation\Validator
     */
    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 个答案:

答案 0 :(得分:0)

要更改重定向网址,您需要修改此行:

protected $redirectTo = '/home';

你的create函数需要从&#34; create&#34;返回数组。方法。

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变量可能是一个问题,但事实证明你没有使用它,所以我已将其删除。