| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 4 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 19
RegisterController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 4 | 56.00 | 0.00% covered (danger) | 0.00% | 0 / 19
__construct | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
validator | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
create | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 5
register | 0.00% covered (danger) | 0.00% | 0 / 1 | 20.00 | 0.00% covered (danger) | 0.00% | 0 / 11

1 | <?php
2 |
3 | namespace App\Http\Controllers\Auth;
4 |
5 | use App\User;
6 | use App\Http\Controllers\Controller;
7 | use Illuminate\Auth\Events\Registered;
8 | use Illuminate\Http\Request;
9 | use Illuminate\Support\Facades\Auth;
10 | use Illuminate\Support\Facades\Hash;
11 | use Illuminate\Support\Facades\Validator;
12 | use Illuminate\Foundation\Auth\RegistersUsers;
13 | use Illuminate\Support\Str;
14 |
15 | class RegisterController extends Controller
16 | {
17 | /*
18 | |--------------------------------------------------------------------------
19 | | Register Controller
20 | |--------------------------------------------------------------------------
21 | |
22 | | This controller handles the registration of new users as well as their
23 | | validation and creation. By default this controller uses a trait to
24 | | provide this functionality without requiring any additional code.
25 | |
26 | */
27 |
28 | use RegistersUsers;
29 |
30 | /**
31 | * Where to redirect users after registration.
32 | *
33 | * @var string
34 | */
35 | protected $redirectTo = '/home';
36 |
37 | /**
38 | * Create a new controller instance.
39 | *
40 | * @return void
41 | */
42 | public function __construct()
43 | {
44 | // $this->middleware('auth');
45 | }
46 |
47 | /**
48 | * Get a validator for an incoming registration request.
49 | *
50 | * @param array $data
51 | * @return \Illuminate\Contracts\Validation\Validator
52 | */
53 | protected function validator(array $data)
54 | {
55 | return Validator::make($data, [
56 | 'name' => ['required', 'string', 'max:255', 'unique:users'],
57 | 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
58 | 'password' => ['required', 'string', 'min:8', 'confirmed'],
59 | ]);
60 | }
61 |
62 | /**
63 | * Create a new user instance after a valid registration.
64 | *
65 | * @param array $data
66 | * @return \App\User
67 | */
68 | protected function create(array $data)
69 | {
70 | return User::create([
71 | 'name' => $data['name'],
72 | 'email' => $data['email'],
73 | 'password' => Hash::make($data['password']),
74 | 'api_token' => Str::random(60),
75 | ]);
76 | }
77 | public function register(Request $request)
78 | {
79 | $headTo=$request->input('headTo')??'';
80 | $this->validator($request->all())->validate();
81 |
82 | event(new Registered($user = $this->create($request->all())));
83 |
84 | $rolesIds=$request->input('role')??'';
85 | if($rolesIds){
86 | $roleIdArr=explode(',',$rolesIds);
87 | $user->roles()->sync($roleIdArr);
88 | }
89 | // $this->guard()->login($user);
90 |
91 | $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),$user['id']);
92 | if($headTo){return redirect(url($headTo))->with('successTip',"录入用户 {$user->name} 成功");}
93 | return $this->registered($request, $user)
94 | ?: redirect($this->redirectPath());
95 | }
96 | }