Skip to content

Commit b6dd99a

Browse files
committed
adding routes to create new posts
1 parent fe7b92c commit b6dd99a

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

app/Http/Controllers/PostController.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,38 @@ public function index()
1717
//return blog.blade.php template from resources/views folder
1818
return view('blog.index', ['posts' => $posts, 'title' => 'Blog Posts']);
1919
}
20+
21+
public function create(Request $request)
22+
{
23+
if ($request->user()->canManagePosts()) {
24+
return view('blog.posts.create');
25+
} else {
26+
return redirect('/')
27+
->withErrors('You do not have permission to create posts.');
28+
}
29+
}
30+
31+
public function save(Request $request)
32+
{
33+
$post = new Post();
34+
$post->title = $request->get('title');
35+
$post->metaTitle = $request->get('metaTitle');
36+
$post->body = $request->get('body');
37+
$post->metaDescription = $request->get('metaDescription');
38+
$post->slug = $request->get('urlSlug') ?? Str::slug($post->title);
39+
$post->user_id = $request->user()->id;
40+
41+
if ($request->has('draft')) {
42+
$post->active = 0;
43+
$message = 'Draft saved!';
44+
} else {
45+
$post->active = 1;
46+
$message = 'Post published!';
47+
}
48+
49+
$post->save();
50+
51+
return redirect('admin/blog/post/' . $post->slug)
52+
->withMessage($message);
53+
}
2054
}

app/Models/User.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,22 @@ public function comments()
5151
return $this->hasMany('App\Models\Comments', 'user_id');
5252
}
5353

54+
/**
55+
* Check if a user can CRUD posts
56+
*/
57+
public function canManagePosts(): bool
58+
{
59+
if ($this->role === 'author' || $this->isAdmin()) {
60+
return true;
61+
}
62+
return false;
63+
}
64+
65+
/**
66+
* Check if the user is an admin
67+
*/
68+
public function isAdmin(): bool
69+
{
70+
return $this->role === 'admin';
71+
}
5472
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@extends('layouts.app')
2+
3+
@section('content')
4+
@if (session('message'))
5+
<div class="alert alert-success">
6+
{{ session('message') }}
7+
</div>
8+
@endif
9+
10+
<form action="/admin/blog/post" method="post" class="container">
11+
<input type="hidden" name="_token" value="{{ csrf_token() }}">
12+
<div class="form-group">
13+
<label for="title">Title</label>
14+
<input type="text" name="title" class="form-control" />
15+
</div>
16+
<div class="form-group">
17+
<label for="metaTitle">Meta Title</label>
18+
<input type="text" name="metaTitle" class="form-control" />
19+
</div>
20+
<div class="form-group">
21+
<label for="body">Body</label>
22+
<textarea name="body" class="form-control"></textarea>
23+
</div>
24+
<div class="form-group">
25+
<label for="metaDescription">Meta Description</label>
26+
<textarea name="metaDescription" class="form-control"></textarea>
27+
</div>
28+
<div class="form-group">
29+
<label for="urlSlug">URL Slug (optional)</label>
30+
<small>If not specified one will be created from the title</small>
31+
<input type="text" name="urlSlug" class="form-control" />
32+
</div>
33+
<input type="submit" name="publish" class="btn btn-success" value="Publish" />
34+
<input type="submit" name="draft" class="btn btn-default" value="Save Draft" />
35+
</form>
36+
@stop

routes/web.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@
1818
Route::get('/blog', 'App\Http\Controllers\PostController@index');
1919

2020
Auth::routes();
21+
22+
Route::group(['prefix' => 'admin'], function () {
23+
Route::group(['prefix' => 'blog'], function () {
24+
Route::get('/post', 'App\Http\Controllers\PostController@create');
25+
Route::post('/post', 'App\Http\Controllers\PostController@save');
26+
});
27+
});

0 commit comments

Comments
 (0)