浏览代码

Lesson 28: Form requests and form objects.

Frederic G. MARAND 7 年之前
父节点
当前提交
3f3829a638
共有 3 个文件被更改,包括 64 次插入19 次删除
  1. 10 19
      app/Http/Controllers/RegistrationController.php
  2. 45 0
      app/Http/Requests/RegistrationForm.php
  3. 9 0
      app/User.php

+ 10 - 19
app/Http/Controllers/RegistrationController.php

@@ -2,9 +2,8 @@
 
 namespace App\Http\Controllers;
 
+use App\Http\Requests\RegistrationForm;
 use App\Mail\Welcome;
-use App\User;
-use Illuminate\Http\Request;
 
 class RegistrationController extends Controller
 {
@@ -13,30 +12,22 @@ class RegistrationController extends Controller
         return view('registration.create');
     }
 
-    public function store(Request $request)
+    /**
+     * Submitted data validation:
+     * - now in RegistrationRequest::rules().
+     * - the code in this method won't be run if validation fails.
+     * - validation is found by Reflection on the method type hint.
+     */
+    public function store(RegistrationForm $request)
     {
-        // Validate submitted data.
-        $this->validate($request, [
-            'name' => 'required',
-            'email' => 'required|email',
-            // "confirmed" uses <field>_confirmation.
-            'password' => 'required|confirmed',
-        ]);
-
-        // Create user. This would give us a clear password.
-        // $user = User::create($request->only(['name', 'email', 'password']));
-        $user = User::create([
-            'name' => $request->get('name'),
-            'email' => $request->get('email'),
-            'password' => bcrypt($request->get('password')),
-        ]);
+        $user = $request->persist();
 
         // Sign user in.
         auth()->login($user);
 
         \Mail::to($user)->send(new Welcome($user));
 
-        // Redirect to home page.
+
         return redirect()->home();
     }
 }

+ 45 - 0
app/Http/Requests/RegistrationForm.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Http\Requests;
+
+use App\User;
+use Illuminate\Foundation\Http\FormRequest;
+
+class RegistrationForm extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        return true;
+    }
+
+    /**
+     * Custom code, not part of standard forms/requests.
+     *
+     * @return User
+     */
+    public function persist(): User
+    {
+        $user = User::create($this->only(['name', 'email', 'password']));
+        return $user;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        return [
+            'name' => 'required',
+            'email' => 'required|email',
+            // "confirmed" uses <field>_confirmation.
+            'password' => 'required|confirmed',
+        ];
+    }
+}

+ 9 - 0
app/User.php

@@ -27,6 +27,15 @@ class User extends Authenticatable
         'password', 'remember_token',
     ];
 
+    public function __construct(array $attributes = [])
+    {
+        if (isset($attributes['password'])) {
+            $attributes['password'] = bcrypt($attributes['password']);
+        }
+
+        parent::__construct($attributes);
+    }
+
     public function posts() {
         return $this->hasMany(Post::class);
     }