|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Chriscreates\Blog\Controllers; |
| 4 | + |
| 5 | +use Artesaos\SEOTools\Facades\OpenGraph; |
| 6 | +use Artesaos\SEOTools\Facades\SEOMeta; |
| 7 | +use Chriscreates\Blog\Post; |
| 8 | +use Chriscreates\Blog\Requests\ValidatePostRequest; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Illuminate\Support\Facades\Storage; |
| 11 | + |
| 12 | +class PostController extends Controller |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Instantiate a new controller instance. |
| 16 | + * |
| 17 | + * @return void |
| 18 | + */ |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + $this->middleware('auth')->except('show'); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Display a listing of the resource. |
| 26 | + * |
| 27 | + * @return \Illuminate\Http\Response |
| 28 | + */ |
| 29 | + public function index() |
| 30 | + { |
| 31 | + $posts = Post::withoutGlobalScope('tags') |
| 32 | + ->orderBy('created_at', 'desc') |
| 33 | + ->paginate(10); |
| 34 | + |
| 35 | + return view('admin.posts.posts', compact('posts')); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Show the form for creating a new resource. |
| 40 | + * |
| 41 | + * @return \Illuminate\Http\Response |
| 42 | + */ |
| 43 | + public function create() |
| 44 | + { |
| 45 | + return view('admin.posts.create-post'); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Store a newly created resource in storage. |
| 50 | + * |
| 51 | + * @param \App\Http\Requests\ValidatePostRequest $request |
| 52 | + * @return \Illuminate\Http\Response |
| 53 | + */ |
| 54 | + public function store(ValidatePostRequest $request) |
| 55 | + { |
| 56 | + $post = Post::create($request->only([ |
| 57 | + 'title', |
| 58 | + 'sub_title', |
| 59 | + 'slug', |
| 60 | + 'user_id', |
| 61 | + 'excerpt', |
| 62 | + 'content', |
| 63 | + 'allow_comments', |
| 64 | + 'allow_guest_comments', |
| 65 | + ])); |
| 66 | + |
| 67 | + return redirect() |
| 68 | + ->route('posts.edit', ['post' => $post->id]) |
| 69 | + ->with('success', 'Post created.'); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Display the specified resource. |
| 74 | + * |
| 75 | + * @param \Chriscreates\Blog\Post $post |
| 76 | + * @return \Illuminate\Http\Response |
| 77 | + */ |
| 78 | + public function show(Post $post) |
| 79 | + { |
| 80 | + $related_posts = Post::relatedByPostTags($post) |
| 81 | + ->take(3) |
| 82 | + ->get(); |
| 83 | + |
| 84 | + SEOMeta::setTitle($post->title) |
| 85 | + ->setDescription($post->excerpt); |
| 86 | + |
| 87 | + OpenGraph::setTitle($post->title) |
| 88 | + ->setDescription($post->excerpt); |
| 89 | + |
| 90 | + return view('posts.show', compact(['post', 'related_posts'])); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Show the form for editing the specified resource. |
| 95 | + * |
| 96 | + * @param \Chriscreates\Blog\Post $post |
| 97 | + * @return \Illuminate\Http\Response |
| 98 | + */ |
| 99 | + public function edit(Post $post) |
| 100 | + { |
| 101 | + return view('admin.posts.edit-post', compact('post')); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Update the specified resource in storage. |
| 106 | + * |
| 107 | + * @param \App\Http\Requests\ValidatePostRequest $request |
| 108 | + * @param \Chriscreates\Blog\Post $post |
| 109 | + * @return \Illuminate\Http\Response |
| 110 | + */ |
| 111 | + public function update(ValidatePostRequest $request, Post $post) |
| 112 | + { |
| 113 | + $post->update($request->only([ |
| 114 | + 'title', |
| 115 | + 'sub_title', |
| 116 | + 'slug', |
| 117 | + 'user_id', |
| 118 | + 'excerpt', |
| 119 | + 'content', |
| 120 | + 'allow_comments', |
| 121 | + 'allow_guest_comments', |
| 122 | + ])); |
| 123 | + |
| 124 | + return redirect() |
| 125 | + ->route('posts.edit', ['post' => $post->id]) |
| 126 | + ->with('success', 'Post updated.'); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Remove the specified resource from storage. |
| 131 | + * |
| 132 | + * @param \Chriscreates\Blog\Post $post |
| 133 | + * @return \Illuminate\Http\Response |
| 134 | + */ |
| 135 | + public function destroy(Post $post) |
| 136 | + { |
| 137 | + $post->delete(); |
| 138 | + |
| 139 | + return redirect() |
| 140 | + ->route('posts.index') |
| 141 | + ->with('success', 'Post deleted.'); |
| 142 | + } |
| 143 | +} |
0 commit comments