Skip to content

Commit

Permalink
feat: 完成专题模块
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangbao05 committed Nov 4, 2019
1 parent b21dfac commit c1966c4
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
38 changes: 38 additions & 0 deletions app/Admin/Controllers/TopicController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Admin\Controllers;

class TopicController extends Controller
{
public function index()
{
$topics = \App\Topic::all();
return view('admin/topic/index', compact('topics'));
}

public function create()
{
return view('admin/topic/create');
}

public function store()
{
$this->validate(request(), [
'name' => 'required|string'
]);

\App\Topic::create(['name' => request('name')]);

return redirect("/admin/topics");
}

public function destroy(\App\Topic $topic)
{
$topic->delete();

return [
'error' => 0,
'msg' => ''
];
}
}
26 changes: 25 additions & 1 deletion public/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $(".post-audit").click(function (event) {
$.ajax({
url: "/admin/posts/" + post_id + "/status",
method: "POST",
data: { "status": status },
data: {"status": status},
dataType: "json",
success: function success(data) {
if (data.error != 0) {
Expand All @@ -23,4 +23,28 @@ $(".post-audit").click(function (event) {
target.parent().parent().remove();
}
});
});

$(".resource-delete").click(function (event) {
if (confirm("确定执行删除操作么?") == false) {
return;
}

var target = $(event.target);
event.preventDefault();
var url = $(target).attr("delete-url");
$.ajax({
url: url,
method: "POST",
data: {"_method": 'DELETE'},
dataType: "json",
success: function (data) {
if (data.error != 0) {
alert(data.msg);
return;
}

window.location.reload();
}
});
});
36 changes: 36 additions & 0 deletions resources/views/admin/topic/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@extends('admin.layout.main')
@section("content")
<!-- Main content -->
<section class="content">
<!-- Small boxes (Stat box) -->
<div class="row">
<div class="col-lg-10 col-xs-6">
<div class="box">

<!-- /.box-header -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">增加专题</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="/admin/topics" method="POST">
{{csrf_field()}}
<div class="box-body">
<div class="form-group">
<label for="exampleInputEmail1">专题名</label>
<input type="text" class="form-control" name="name">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">提交</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
<!-- /.content -->
@endsection
41 changes: 41 additions & 0 deletions resources/views/admin/topic/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@extends('admin.layout.main')
@section("content")
<!-- Main content -->
<section class="content">
<!-- Small boxes (Stat box) -->
<div class="row">
<div class="col-lg-10 col-xs-6">
<div class="box">

<div class="box-header with-border">
<h3 class="box-title">专题列表</h3>
</div>
<a type="button" class="btn " href="/admin/topics/create">增加专题</a>
<!-- /.box-header -->
<div class="box-body">
<table class="table table-bordered">
<tbody>
<tr>
<th style="width: 10px">#</th>
<th>专题名称</th>
<th>操作</th>
</tr>
@foreach($topics as $topic)
<tr>
<td>{{$topic->id}}</td>
<td>{{$topic->name}}</td>
<td>
<a type="button" class="btn resource-delete" delete-url="/admin/topics/{{$topic->id}}"
href="#">删除</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
<!-- /.content -->
@endsection
5 changes: 5 additions & 0 deletions routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
Route::get('/posts', '\App\Admin\Controllers\PostController@index');
Route::post('/posts/{post}/status', '\App\Admin\Controllers\PostController@status');
});

Route::group(['middleware' => 'can:topic'], function () {
Route::resource('topics', '\App\Admin\Controllers\TopicController', ['only' => ['index', 'create'
, 'store', 'destroy']]);
});
});
});

0 comments on commit c1966c4

Please sign in to comment.