Skip to content

Commit

Permalink
User can create reply
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Nov 1, 2017
1 parent a12664e commit 9d78a70
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 76 deletions.
55 changes: 15 additions & 40 deletions app/Http/Controllers/RepliesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,30 @@
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ReplyRequest;
use Auth;

class RepliesController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
$this->middleware('auth');
}

public function index()
{
$replies = Reply::paginate();
return view('replies.index', compact('replies'));
}

public function show(Reply $reply)
public function store(ReplyRequest $request, Reply $reply)
{
return view('replies.show', compact('reply'));
}

public function create(Reply $reply)
{
return view('replies.create_and_edit', compact('reply'));
}

public function store(ReplyRequest $request)
{
$reply = Reply::create($request->all());
return redirect()->route('replies.show', $reply->id)->with('message', 'Created successfully.');
}

public function edit(Reply $reply)
{
$this->authorize('update', $reply);
return view('replies.create_and_edit', compact('reply'));
}
$reply->content = $request->content;
$reply->user_id = Auth::id();
$reply->topic_id = $request->topic_id;
$reply->save();

public function update(ReplyRequest $request, Reply $reply)
{
$this->authorize('update', $reply);
$reply->update($request->all());

return redirect()->route('replies.show', $reply->id)->with('message', 'Updated successfully.');
}
return redirect()->to($reply->topic->link())->with('sucess', '回复创建成功!');
}

public function destroy(Reply $reply)
{
$this->authorize('destroy', $reply);
$reply->delete();
public function destroy(Reply $reply)
{
$this->authorize('destroy', $reply);
$reply->delete();

return redirect()->route('replies.index')->with('message', 'Deleted successfully.');
}
return redirect()->route('replies.index')->with('sucess', '回复删除成功!');
}
}
32 changes: 2 additions & 30 deletions app/Http/Requests/ReplyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,9 @@
class ReplyRequest extends Request
{
public function rules()
{
switch($this->method())
{
// CREATE
case 'POST':
{
return [
// CREATE ROLES
];
}
// UPDATE
case 'PUT':
case 'PATCH':
{
return [
// UPDATE ROLES
];
}
case 'GET':
case 'DELETE':
default:
{
return [];
};
}
}

public function messages()
{
return [
// Validation messages
'content' => 'required|min:2',
];
}
}
}
8 changes: 4 additions & 4 deletions app/Observers/ReplyObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

class ReplyObserver
{
public function creating(Reply $reply)
public function created(Reply $reply)
{
//
$reply->topic->increment('reply_count', 1);
}

public function updating(Reply $reply)
public function creating(Reply $reply)
{
//
$reply->content = clean($reply->content, 'user_topic_body');
}
}
11 changes: 11 additions & 0 deletions resources/views/topics/_reply_box.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="reply-box">
<form action="{{ route('replies.store') }}" method="POST" accept-charset="UTF-8">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="topic_id" value="{{ $topic->id }}">
<div class="form-group">
<textarea class="form-control" rows="3" placeholder="分享你的想法" name="content"></textarea>
</div>
<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-share"></i>回复</button>
</form>
</div>
<hr>
2 changes: 1 addition & 1 deletion resources/views/topics/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
{{-- 用户回复列表 --}}
<div class="panel panel-default topic-reply">
<div class="panel-body">
@include('topics._reply_box', ['topic' => $topic])
@includeWhen(Auth::check(), 'topics._reply_box', ['topic' => $topic])
@include('topics._reply_list', ['replies' => $topic->replies()->with('user')->get()])
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
Route::post('upload_image', 'TopicsController@uploadImage')->name('topics.upload_image');

Route::get('topics/{topic}/{slug?}', 'TopicsController@show')->name('topics.show');
Route::resource('replies', 'RepliesController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);
Route::resource('replies', 'RepliesController', ['only' => ['store', 'destroy']]);

0 comments on commit 9d78a70

Please sign in to comment.