Skip to content

Commit b3f08b2

Browse files
committed
Complete: Publish Routes - WIP: Publish views
1 parent b877df7 commit b3f08b2

File tree

8 files changed

+341
-4
lines changed

8 files changed

+341
-4
lines changed

src/Console/Commands/SetupCommand.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ public function handle()
4545
}
4646
}
4747

48+
if ( ! file_exists(base_path('routes/blog.php'))) {
49+
$this->publishRoutes();
50+
}
51+
4852
if ( ! file_exists(app_path('Providers/BlogServiceProvider.php'))) {
4953
$this->publishProvider();
5054
}
5155

5256
// TODO:
53-
// Routes
5457
// Views?
5558
5659
if ($this->option('data')) {
@@ -126,4 +129,17 @@ private function publishProvider()
126129
$appConfig
127130
));
128131
}
132+
133+
/**
134+
* Register the routes.
135+
*
136+
* @return void
137+
*/
138+
private function publishRoutes()
139+
{
140+
copy(
141+
__DIR__.'/../../../routes/web.php',
142+
base_path('routes/blog.php')
143+
);
144+
}
129145
}

src/Controllers/Controller.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Controllers;
4+
5+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6+
use Illuminate\Foundation\Bus\DispatchesJobs;
7+
use Illuminate\Foundation\Validation\ValidatesRequests;
8+
use Illuminate\Routing\Controller as BaseController;
9+
10+
class Controller extends BaseController
11+
{
12+
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
13+
}

src/Controllers/PostController.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
}

src/Observers/PostObserver.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Observers;
4+
5+
use Chriscreates\Blog\Post;
6+
7+
class PostObserver
8+
{
9+
/**
10+
* Handle the post "saving" event.
11+
*
12+
* @param \Chriscreates\Blog\Post $post
13+
* @return void
14+
*/
15+
public function saving(Post $post)
16+
{
17+
if (request()->filled('tags')) {
18+
$post->tags()->sync(request('tags'));
19+
}
20+
21+
if (request()->filled('category_id')) {
22+
$post->category_id = request('category_id');
23+
}
24+
25+
if ( ! request()->filled('user_id')) {
26+
$post->user_id = auth()->id();
27+
}
28+
29+
if ( ! request()->filled('slug')) {
30+
$post->setSlug(request('title'));
31+
}
32+
33+
if (request()->filled('status')) {
34+
$post->setStatus(request('status'));
35+
}
36+
}
37+
38+
/**
39+
* Handle the post "deleting" event.
40+
*
41+
* @param \Chriscreates\Blog\Post $post
42+
* @return void
43+
*/
44+
public function deleting(Post $post)
45+
{
46+
$post->tags()->detach();
47+
48+
$post->comments()->delete();
49+
}
50+
}

src/Providers/BlogServiceProvider.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace App\Providers;
44

5-
use \Chriscreates\Blog\Post;
5+
use Chriscreates\Blog\Observers\PostObserver;
6+
use Chriscreates\Blog\Post;
67
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
78
use Illuminate\Support\Facades\Route;
89

910
class BlogServiceProvider extends ServiceProvider
1011
{
12+
protected $namespace = '\Chriscreates\Blog\Controllers';
13+
1114
/**
1215
* Define your route model bindings, pattern filters, etc.
1316
*
@@ -23,6 +26,8 @@ public function boot()
2326
->orWhere('slug', 'LIKE', "%{$value}%");
2427
})->first();
2528
});
29+
30+
Post::observe(PostObserver::class);
2631
}
2732

2833
/**
@@ -44,8 +49,7 @@ public function map()
4449
*/
4550
protected function mapBlogRoutes()
4651
{
47-
Route::middleware('api')
48-
->namespace($this->namespace)
52+
Route::namespace($this->namespace)
4953
->group(base_path('routes/blog.php'));
5054
}
5155
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Requests;
4+
5+
use Chriscreates\Blog\Rules\ValidateCategoryId;
6+
use Chriscreates\Blog\Rules\ValidateTagIds;
7+
use Illuminate\Foundation\Http\FormRequest;
8+
9+
class ValidatePostRequest extends FormRequest
10+
{
11+
/**
12+
* Determine if the user is authorized to make this request.
13+
*
14+
* @return bool
15+
*/
16+
public function authorize()
17+
{
18+
return true;
19+
}
20+
21+
/**
22+
* Get the validation rules that apply to the request.
23+
*
24+
* @return array
25+
*/
26+
public function rules()
27+
{
28+
return [
29+
'category_id' => ['nullable', new ValidateCategoryId],
30+
'user_id' => 'nullable',
31+
'tags' => ['nullable', new ValidateTagIds],
32+
'title' => 'required|string|max:150',
33+
'sub_title' => 'nullable|string',
34+
'slug' => 'nullable|string',
35+
'excerpt' => 'nullable|string',
36+
'content' => 'nullable',
37+
'allow_comments' => 'nullable|boolean',
38+
'allow_guest_comments' => 'nullable|boolean',
39+
'status' => 'nullable',
40+
'published_at' => 'string|nullable',
41+
];
42+
}
43+
}

src/Rules/ValidateCategoryId.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Rules;
4+
5+
use Chriscreates\Blog\Category;
6+
use Illuminate\Contracts\Validation\Rule;
7+
8+
class ValidateCategoryId implements Rule
9+
{
10+
/**
11+
* Determine if the validation rule passes.
12+
*
13+
* @param string $attribute
14+
* @param mixed $value
15+
* @return bool
16+
*/
17+
public function passes($attribute, $value)
18+
{
19+
return Category::where('id', $value)->exists();
20+
}
21+
22+
/**
23+
* Get the validation error message.
24+
*
25+
* @return string
26+
*/
27+
public function message()
28+
{
29+
return 'The category selected is invalid or does not exist.';
30+
}
31+
}

src/Rules/ValidateTagIds.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Rules;
4+
5+
use Chriscreates\Blog\Tag;
6+
use Illuminate\Contracts\Validation\Rule;
7+
8+
class ValidateTagIds implements Rule
9+
{
10+
/**
11+
* Determine if the validation rule passes.
12+
*
13+
* @param string $attribute
14+
* @param mixed $value
15+
* @return bool
16+
*/
17+
public function passes($attribute, $value)
18+
{
19+
foreach ($value as $tag_id) {
20+
if ( ! Tag::where('id', $tag_id)->exists()) {
21+
return false;
22+
}
23+
}
24+
25+
return true;
26+
}
27+
28+
/**
29+
* Get the validation error message.
30+
*
31+
* @return string
32+
*/
33+
public function message()
34+
{
35+
return 'The tag(s) selected is invalid or does not exist.';
36+
}
37+
}

0 commit comments

Comments
 (0)