本书未发布
73. 删除话题
简介
在本节里,我们完成话题删除功能。
需求分解
删除话题的要求如下:
- 只有作者可以删除自己创建的话题;
- 删除前需要弹出确认框,用户确认后才可以删除;
- 删除请求必须是
Delete
请求; - 删除成功后转到话题列表页。
模型
首先,添加实例方法 canDelete
判断当前用户是否可以删除话题记录。
application/common/model/Topic.php
<?php
.
.
.
class Topic extends Model
{
.
.
.
/**
* 登录用户是否可以删除记录
* @Author zhanghong(Laifuzi)
* @DateTime 2019-06-21
* @return [type] [description]
*/
public function canDelete(){
$current_user = User::currentUser();
if(empty($current_user)){
return false;
}else if($this->user_id != $current_user->id){
return false;
}
return true;
}
}
控制器
接下来,完成控制器里的 delete
方法。
application/index/controller/Topic.php
.
.
.
public function delete($id)
{
$topic = TopicModel::find($id);
if(empty($topic)){
$this->error('删除话题不存在', '[topic.index]');
}else if(!$topic->canDelete()){
$this->error('对不起,您没有权限删除该话题', '[topic.index]');
}
$topic->delete();
$message = '删除成功';
Session::set('success', $message);
$this->success($message, '[topic.index]');
}
.
.
.
路由
添加删除话题路由配置。route/route.php
<?php
.
.
.
// 话题管理
Route::get('topic/create', 'topic/create')->name('topic.create');
Route::post('topic', 'topic/save')->name('topic.save');
Route::get('topic/<id>/edit', 'topic/edit')->name('topic.edit');
Route::put('topic/<id>', 'topic/update')->name('topic.update');
Route::get('topic/<id>', 'topic/read')->name('topic.read');
Route::delete('topic/<id>', 'topic/delete')->name('topic.delete');
Route::get('topic', 'topic/index')->name('topic.index');
Route::get('category/<id>', 'category/read')->name('category.read');
视图模板
在话题详情页添加删除操作入口:
application/index/view/topic/read.html
<?php if($topic->canUpdate() || $topic->canDelete()): ?>
<div class="operate">
<hr>
<?php if($topic->canUpdate()): ?>
<a href="{:url('[topic.edit]', ['id' => $topic->id])}" class="btn btn-default btn-xs pull-left" role="button">
<i class="glyphicon glyphicon-edit"></i> 编辑
</a>
<?php endif; ?>
<?php if($topic->canUpdate()): ?>
<form id='delete-form' action="{:url('[topic.delete]', ['id' => $topic->id])}" method="post">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-default btn-xs pull-left" style="margin-left: 6px">
<i class="glyphicon glyphicon-trash"></i>
删除
</button>
</form>
<?php endif; ?>
</div>
<?php endif; ?>
代码重构
到目前为止我们已经完成了删除话题的所有实现代码,但我们发现话题模型文件里, canUpdate
和 canDelete
的代码完全一样。虽然这两个方法的代码完全一样,但因为它们处理的业务场景不一样所以我们不考虑把这两个方法进行合并。但在这两个方法里都是通过 `$this->user_id != $current_user->id' 来判断当前用户是否可以编辑或删除话题,考虑到下一章我们要开发的回复功能也需要这样来判断用户是否可以删除回复记录,所以我们对这块代码进行重构。
- 我们在用户模型文件里添加添加一个实例方法来判断用户是不是记录的作者:
application/common/model/User.php
<?php
.
.
.
class User extends Model
{
.
.
.
/**
* 是否是实例对象的作者
* @Author zhanghong(Laifuzi)
* @DateTime 2019-06-19
* @param Object $item 实例对象
* @return boolean [description]
*/
public function isAuthorOf($item)
{
if($this->id == $item->user_id){
return true;
}
return false;
}
}
- 在话题模型里调用
isAuthorOf
方法来判断当前用户是不是可以编辑或删除当前话题:
application/common/model/Topic.php
<?php
.
.
.
class Topic extends Model
{
.
.
.
/**
* 是否可以编辑记录
* @Author zhanghong(Laifuzi)
* @DateTime 2019-06-21
* @return [type] [description]
*/
public function canUpdate(){
$current_user = User::currentUser();
if(empty($current_user)){
return false;
}else if(!$current_user->isAuthorOf($this)){
return false;
}
return true;
}
.
.
.
/**
* 登录用户是否可以删除记录
* @Author zhanghong(Laifuzi)
* @DateTime 2019-06-21
* @return [type] [description]
*/
public function canDelete(){
$current_user = User::currentUser();
if(empty($current_user)){
return false;
}else if(!$current_user->isAuthorOf($this)){
return false;
}
return true;
}
}
效果预览
Git 版本控制
下面把代码纳入到版本管理:
$ git add -A
$ git commit -m "用户可以删除自己发的话题"
推荐文章: