瀏覽代碼

Lesson 19: Associating With Users: Part 2.

Frederic G. MARAND 6 年之前
父節點
當前提交
d8e0d3fd90

+ 7 - 4
app/Http/Controllers/PostsController.php

@@ -8,6 +8,12 @@ use Illuminate\Http\Request;
 class PostsController extends Controller
 {
 
+    public function __construct()
+    {
+        $this->middleware('auth')
+        ->except(['index', 'show']);
+    }
+
     /**
      * Show the form for creating a new resource.
      *
@@ -82,10 +88,7 @@ class PostsController extends Controller
             'body' => 'required',
         ]);
 
-        Post::create([
-            'title' => $request->get('title'),
-            'body' => $request->get('body'),
-        ]);
+        auth()->user()->publish(new Post(request(['title', 'body'])));
 
         return redirect('/');
     }

+ 39 - 0
app/Http/Controllers/RegistrationController.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\User;
+use Illuminate\Http\Request;
+
+class RegistrationController extends Controller
+{
+    public function create()
+    {
+        return view('registration.create');
+    }
+
+    public function store(Request $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')),
+        ]);
+
+        // Sign user in.
+        auth()->login($user);
+
+        // Redirect to home page.
+        return redirect()->home();
+    }
+}

+ 39 - 0
app/Http/Controllers/SessionsController.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+
+class SessionsController extends Controller
+{
+    public function __construct()
+    {
+        $this->middleware('guest')->only('store');
+    }
+
+    public function create()
+    {
+        return view('sessions.create');
+    }
+
+    public function destroy() {
+        auth()->logout();
+        return redirect()->home();
+    }
+
+    public function store()
+    {
+        // Attempt to authenticate the user.
+        // On failure, redirect back.
+        if (!auth()->attempt(request(['email', 'password']))) {
+            return back()->withErrors([
+                'message' => 'Please check your credentials and try again',
+            ]);
+        };
+
+        // On success, sign them in.
+
+        // Then redirect to home.
+        return redirect()->home();
+    }
+}

+ 1 - 1
app/Post.php

@@ -10,7 +10,7 @@ class Post extends Model
      *
      * @var array
      */
-    protected $fillable = ['body', 'title'];
+    protected $fillable = ['body', 'title', 'user_id'];
 
     /**
      * @param string $body

+ 7 - 0
app/User.php

@@ -30,4 +30,11 @@ class User extends Authenticatable
     public function posts() {
         return $this->hasMany(Post::class);
     }
+
+    public function publish(Post $post) {
+        // Why not $post->save(); ?
+        // It would not carry the user information present in the posts() collection.
+
+        $this->posts()->save($post);
+    }
 }

+ 4 - 1
resources/views/layouts/nav.blade.php

@@ -5,7 +5,10 @@
             <a class="nav-link" href="#">New features</a>
             <a class="nav-link" href="#">Press</a>
             <a class="nav-link" href="#">New hires</a>
-            <a class="nav-link" href="#">About</a>
+            @if (\Illuminate\Support\Facades\Auth::check())
+            <a class="nav-link ml-auto" href="#">{{ auth()->user()->name }}</a>
+            @endif
+
         </nav>
     </div>
 </div>

+ 1 - 1
resources/views/posts/post.blade.php

@@ -4,7 +4,7 @@
     </h2>
     <p class="blog-post-meta">
         {{ $post->created_at->toFormattedDateString() }}
-        by <a href="#">Mark</a>
+        by <a href="#">{{ $post->user->name }}</a>
     </p>
     {{ $post->body }}
 </div>

+ 1 - 1
resources/views/posts/show.blade.php

@@ -5,7 +5,7 @@
         <h1>{{ $post->title }}</h1>
         <p class="blog-post-meta">
             {{ $post->created_at->toFormattedDateString() }}
-            by <a href="#">Mark</a>
+            by <a href="#">{{ $post->user->name }}</a>
         </p>
 
         {{ $post->body }}

+ 37 - 0
resources/views/registration/create.blade.php

@@ -0,0 +1,37 @@
+@extends('layouts.master')
+
+@section('content')
+<div class="col-sm-8">
+    <h1>Register</h1>
+    <form method="post" action="/register">
+        {{ csrf_field() }}
+
+        @include('layouts.errors')
+
+        <div class="form-group">
+            <label for="name">Name:</label>
+            <input type="text" class="form-control"
+                   id="name" name="name" required />
+        </div>
+
+        <div class="form-group">
+            <label for="email">E-mail:</label>
+            <input type="text" class="form-control"
+                   id="email" name="email" required />
+        </div>
+
+        <div class="form-group">
+            <label for="password">Password:</label>
+            <input type="password" class="form-control"
+                   id="password" name="password" required />
+            <label for="password_confirmation">Password confirmation:</label>
+            <input type="password" class="form-control"
+                   id="password_confirmation" name="password_confirmation" required />
+        </div>
+
+        <div class="form-group">
+            <button type="submit" class="btn btn-primary">Register</button>
+        </div>
+    </form>
+</div>
+@endsection

+ 28 - 0
resources/views/sessions/create.blade.php

@@ -0,0 +1,28 @@
+@extends('layouts.master')
+
+@section('content')
+<div class="col-sm-8">
+    <h1>Login</h1>
+    <form method="post" action="/login">
+        {{ csrf_field() }}
+
+        @include('layouts.errors')
+
+        <div class="form-group">
+            <label for="email">E-mail:</label>
+            <input type="text" class="form-control"
+                   id="email" name="email" required />
+        </div>
+
+        <div class="form-group">
+            <label for="password">Password:</label>
+            <input type="password" class="form-control"
+                   id="password" name="password" required />
+        </div>
+
+        <div class="form-group">
+            <button type="submit" class="btn btn-primary">Login</button>
+        </div>
+    </form>
+</div>
+@endsection

+ 9 - 1
routes/web.php

@@ -21,8 +21,16 @@
 */
 
 /** @var \Illuminate\Routing\Router $this */
-$this->get('/', 'PostsController@index');
+$this->get('/', 'PostsController@index')
+    ->name('home');
 $this->post('/posts', 'PostsController@store');
 $this->get('/posts/create', 'PostsController@create');
 $this->get('/posts/{post}', 'PostsController@show');
 $this->post('/posts/{post}/comments', 'CommentsController@store');
+
+$this->get('register', 'RegistrationController@create');
+$this->post('register', 'RegistrationController@store');
+
+$this->get('login', 'SessionsController@create');
+$this->post('login', 'SessionsController@store');
+$this->get('logout', 'SessionsController@destroy');