Correcting teacher:WJ
Correction status:qualified
Teacher's comments:写的非常不错!
//新闻系统
Route::get('/admins/article/index','Article@index');//文章列表
Route::get('/admins/article/add','Article@add');//文章添加
Route::post('/admins/article/save','Article@save');//文章保存
Route::post('/admins/article/del','Article@del');//删除文章
Route::get('/admins/article/cates','Article@cates');//文章分类
Route::get('/admins/article/add_cate','Article@add_cate');//添加文章分类
Route::post('/admins/article/save_cate','Article@save_cate');//保存文章分类
Route::post('/admins/article/del_cate','Article@del_cate');//删除文章分类
// 上传系统
Route::post('/admins/files/upload_img','Files@upload_img');//文章缩略图上传
利用laravel的artisan命令轻松创建
php artisan make:controller admins/Article
php artisan make:controller admins/Files
<?php
namespace App\Http\Controllers\admins;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
class Article extends Controller
{
// 添加新闻
public function add(Request $request)
{
$aid=(int)$request->aid;
$data['article']=DB::table('article')->where('id',$aid)->item();
$data['contents']=DB::table('article_detail')->where('aid',$aid)->item();
$data['cates']=DB::table('article_cate')->lists();
return view('admins/article/add',$data);
}
// 保存新闻
public function save(Request $request)
{
$aid=(int)$request->aid;
$admin=Auth::user();
$data['cid']=(int)$request->cid;
$data['title']=trim($request->title);
$data['subtitle']=trim($request->subtitle);
$data['thumb']=trim($request->preview_img);
$data['keywords']=trim($request->keywords);
$data['descs']=trim($request->descs);
$data['auther_id']=$admin->id;
// echo '<pre>';
// print_r($data);
// print_r($admin);
// return;
$data['pv']=0;
$data['status']=(int)$request->status;
$contents=trim($request->contents);
if ($data['title']==='') {
exit(json_encode(['code'=>1,'message'=>'文章标题怎么可以没有呢?']));
}
if ($aid===0) {
$data['add_time']=time();
$aid = DB::table('article')->insertGetId($data);
if($aid>0){
DB::table('article_detail')->insert(['aid'=>$aid,'contents'=>$contents]);
}
} else {
DB::table('article')->where('id',$aid)->update($data);
DB::table('article_detail')->where('aid',$aid)->update(['aid'=>$aid,'contents'=>$contents]);
}
exit(json_encode(['code'=>0,'message'=>'保存新闻成功']));
}
// 删除新闻
public function del(Request $request) {
$aid=(int)$request->aid;
// echo $aid;
// exit;
DB::table('article')->where('id',$aid)->delete();
DB::table('article_detail')->where('aid',$aid)->delete();
exit(json_encode(['code'=>0,'message'=>'删除文章成功']));
}
// 新闻列表
public function index(Request $request) {
$data['cates']=DB::table('article_cate')->cates('cid');
// echo '<pre>';
// print_r($data);
// return;
$data['auth']=DB::table('new_admin')->cates('id');
// echo '<pre>';
// print_r($data);
// return;
// $data['lists']=DB::table('article')->lists();
$data['page']=(int)$request->page;
$data['pageSize']=5;
$pages= DB::table('article')->orderBy('id','desc')->pages($data['pageSize']);
// 列表
$data['article_lists']=$pages['lists'];
// 总数
$data['total']=$pages['total'];
// echo '<pre>';
// print_r($data);
// return;
return view('admins/article/index',$data);
}
//新闻分类
public function cates() {
$data['lists']=DB::table('article_cate')->lists();
return view('admins/article/cates',$data);
}
// 添加分类
public function add_cate(Request $request) {
$cid=(int)$request->cid;
$data['title']=DB::table('article_cate')->where('cid',$cid)->item();
if(!$data['title']) {
$data['title']['cid']=0;
$data['title']['title']='';
}
// echo '<pre>';
// print_r($data);
return view('admins/article/add_cate',$data);
}
// 保存分类
public function save_cate(Request $request) {
$cid=(int)$request->cid;
$title=trim($request->title);
if ($title==='') {
exit(json_encode(['code'=>1,'message'=>'分类名称不能为空']));
}
if ($cid===0) {
DB::table('article_cate')->insert(['title'=>$title]);
} else {
DB::table('article_cate')->where('cid',$cid)->update(['title'=>$title]);
}
exit(json_encode(['code'=>0,'message'=>'保存分类成功']));
}
// 删除分类
public function del_cate(Request $request) {
$cid=(int)$request->cid;
// echo $aid;
// exit;
DB::table('article_cate')->where('cid',$cid)->delete();
exit(json_encode(['code'=>0,'message'=>'删除分类成功']));
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>新闻列表</title>
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<script src="/static/plugins/layui/layui.js"></script>
<link rel="stylesheet" href="/static/css/admin_index.css">
</head>
<body style="padding: 20px">
<div class="article_add">
<button class="layui-btn" onclick="add()">添加新闻</button>
</div>
<table class="layui-table">
@csrf
<thead>
<tr>
<th>ID</th>
<th>新闻分类</th>
<th>缩略图</th>
<th>文章标题</th>
<th>文章作者</th>
<th>浏览量</th>
<th>发布时间</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach ($article_lists as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$cates[$item['cid']]['title']}}</td>
<td><img src="{{$item['thumb']}}" /></td>
<td>{{$item['title']}}</td>
<td>{{$auth[$item['auther_id']]['name']}}</td>
<td>{{$item['pv']}}</td>
<td>{{date('Y-m-d H:i:s',$item['add_time'])}}</td>
<td>{{$item['status']==0?'草稿':'已发布'}}</td>
<td>
<button class="layui-btn layui-btn-xs" onclick="add({{$item['id']}})">编辑</button>
<button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del({{$item['id']}})">删除</button>
</td>
</tr>
@endforeach
</tbody>
</table>
<div id="pages"></div>
</body>
</html>
<script>
layui.use(['layer', 'laypage'], function() {
var layer = layui.layer;
var laypage = layui.laypage;
$ = layui.jquery;
//执行一个laypage实例
laypage.render({
limit: {{$pageSize}},
elem: 'pages', //注意,这里的 test1 是 ID,不用加 # 号
count: {{$total}}, //数据总数,从服务端得到
curr:{{$page}},
layout:['count','prev', 'page', 'next','skip'],
jump: function(obj, first){
//obj包含了当前分页的所有参数,比如:
//console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。
//console.log(obj.limit); 得到每页显示的条数
//首次不执行
if(!first){
//do something
window.location.href='?page='+obj.curr;
}
}
});
});
// 修改文章
function add(aid) {
//iframe层
layer.open({
type: 2,
title: aid>0?'修改新闻文章':'添加新闻文章',
shadeClose: true,
shade: 0.8,
area: ['80%', '90%'],
content: '/admins/article/add?aid='+aid, //iframe的url
btn: ['保存'],
yes: function(index, layero) {
var body = layer.getChildFrame('body', index);
var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象,执行iframe页的方法:
iframeWin.save();
}
});
}
//删除文章
function del(aid) {
//询问框
layer.confirm('是否确定删除?', {
icon: 3,
btn: ['确定', '取消'] //按钮
}, function() {
var _token = $('input[name="_token"]').val();
$.post('/admins/article/del', {
aid: aid,
_token: _token
}, function(res) {
if (res.code > 0) {
return layer.alert(res.message, {
icon: 2
});
}
layer.msg('删除文章成功');
setTimeout(function() {
window.location.reload();
}, 1000)
}, 'json');
});
}
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加文章</title>
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<script src="/static/plugins/layui/layui.js"></script>
<link rel="stylesheet" href="/static/css/admin_index.css">
<!-- 配置文件 -->
<script type="text/javascript" src="/static/plugins/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/static/plugins/ueditor/ueditor.all.js"></script>
</head>
<body style="padding: 20px">
<div class="layui-form">
@csrf
<input type="hidden" name="aid" value="{{$article['id']}}">
<div class="layui-form-item">
<label class="layui-form-label">文章标题</label>
<div class="layui-input-block">
<input type="text" name="title" class="layui-input" value="{{$article['title']}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">文章副标题</label>
<div class="layui-input-block">
<input type="text" name="subtitle" class="layui-input" value="{{$article['subtitle']}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">文章分类</label>
<div class="layui-input-block">
<select name="cid">
<option value="0">请选择新闻的分类</option>
@foreach ($cates as $cate)
<option value="{{$cate['cid']}}" {{$cate['cid']===$article['cid']?'selected':''}}>{{$cate['title']}}</option>
@endforeach
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">封面图</label>
<div class="layui-input-block">
<img id="preview_img" src="{{$article['thumb']}}" style="height: 30px">
<button type="button" class="layui-btn" id="img_upload">
<i class="layui-icon"></i>上传图片
</button>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">关键词</label>
<div class="layui-input-block">
<input type="text" name="keywords" class="layui-input" value="{{$article['keywords']}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">描述</label>
<div class="layui-input-block">
<textarea name="descs" placeholder="请输入内容" class="layui-textarea">{{$article['descs']}}</textarea>
</div>
</div>
<!-- 文章详情 -->
<div class="layui-form-item">
<label class="layui-form-label">文章内容</label>
<div class="layui-input-block">
<!-- 加载编辑器的容器 -->
<script id="container" name="contents" type="text/plain">{!!$contents['contents']!!}</script>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-block">
<input type="checkbox" name="status" {{$article['status']===1?'checked':''}} lay-skin="primary" title="发布">
</div>
</div>
</div>
</body>
</html>
<script>
layui.use(['upload','form'], function(){
var upload = layui.upload;
var form = layui.form;
$ = layui.jquery;
var _token=$('input[name="_token"]').val();
//执行实例
var uploadInst = upload.render({
elem: '#img_upload' //绑定元素
,url: '/admins/files/upload_img' //上传接口
,data: {_token:_token}
,done: function(res){
//上传完毕回调
// console.log('success');
$('#preview_img').attr('src',res.data.src);
}
,error: function(){
//请求异常回调
}
});
// 实例化编辑器
// var ue = UE.getContent();
// 不要有var
ue = UE.getEditor('container', {
autoHeightEnabled: true
,initialFrameWidth: '100%' //初始化编辑器宽度,默认1000
,minFrameWidth: '760' //编辑器拖动时最小宽度,默认800
,initialFrameHeight: 450 //初始化高度
,minFrameHeight:630
,initialContent:'请输入详细的内容'
});
});
// 文章保存
function save() {
// var title = $.trim($('input[name="title"]').val());
// var subtitle = $.trim($('input[name="subtitle"]').val());
// var cid = parseInt($('select[name="cid"]').val());
// var preview_img = $('#preview_img').arrt('src');
// var keywords = $('input[name="keywords"]').val();
// var descs = $('textarea[name="descs"]').val();
// var content = UE.getContent();
// var status = $('input[name="status"]').is(':checked')?1:0;
var data={};
data.aid=$('input[name="aid"]').val();
data.title=$.trim($('input[name="title"]').val());
data.subtitle=$.trim($('input[name="subtitle"]').val());
data.cid=parseInt($('select[name="cid"]').val());
data.preview_img=$('#preview_img').attr('src');
data.keywords=$('input[name="keywords"]').val();
data.descs=$('textarea[name="descs"]').val();
// 全局变量
data.contents=ue.getContent();
data.status=$('input[name="status"]').is(':checked')?1:0;
data._token=$('input[name="_token"]').val();
if (data.title==='') {
return layer.alert('填写文章标题',{icon:2});
}
if (isNaN(data.cid) || data.cid<=0) {
return layer.alert('请勾选文章分类',{icon:2});
}
$.post('/admins/article/save',data,function(res){
if (res.code > 0) {
return layer.alert(res.message, {
icon: 2
});
}
layer.msg('添加文章成功');
setTimeout(function() {
parent.window.location.reload();
}, 1000)
},'json');
}
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文章分类添加</title>
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<script src="/static/plugins/layui/layui.js"></script>
<link rel="stylesheet" href="/static/css/admin_index.css">
</head>
<body style="padding:20px;">
</body>
<div class="layui-form">
@csrf
<input type="hidden" name="cid" value="{{$title['cid']}}">
<div class="layui-form-item">
<label class="layui-form-label">分类名称</label>
<div class="layui-input-block">
<input type="text" class="layui-input" name="title" value="{{$title['title']}}">
</div>
</div>
<!-- <div class="layui-input-block">
<button class="layui-btn" onclick="save()">保存</button>
</div> -->
</div>
</html>
<script>
layui.use(['layer'], function() {
var layer = layui.layer;
$ = layui.jquery;
});
function save() {
var title = $.trim($('input[name="title"]').val());
var cid = $('input[name="cid"]').val();
if (title === '') {
return layer.alert('请填写分类标题', {
icon: 2
});
}
var _token = $('input[name="_token"]').val();
$.post('/admins/article/save_cate', {
cid: cid,
title: title,
_token: _token
}, function(res) {
if (res.code > 0) {
return layer.alert(res.message, {
icon: 2
});
}
layer.msg('添加分类成功');
setTimeout(function() {
parent.window.location.reload();
}, 1000)
}, 'json');
}
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>新闻分类列表</title>
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<script src="/static/plugins/layui/layui.js"></script>
<link rel="stylesheet" href="/static/css/admin_index.css">
</head>
<body style="padding: 20px;">
<div style="float: right;padding: 10px;">
<button class="layui-btn" onclick="add()">添加</button>
</div>
<table class="layui-table">
@csrf
<thead>
<tr>
<th>ID</th>
<th>分类名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach ($lists as $item)
<tr>
<td>{{$item['cid']}}</td>
<td>{{$item['title']}}</td>
<td>
<button class="layui-btn layui-btn-xs" onclick="add({{$item['cid']}})">修改</button>
<button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del({{$item['cid']}})">删除</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
<script>
layui.use(['layer'], function() {
var layer = layui.layer;
$ = layui.jquery;
});
function add(cid) {
//iframe层
layer.open({
type: 2,
title: cid > 0 ? '修改分类' : '添加分类',
shadeClose: true,
shade: 0.8,
area: ['500px', '400px'],
content: '/admins/article/add_cate?cid=' + cid, //iframe的url
btn: ['保存'],
yes: function(index, layero) {
var body = layer.getChildFrame('body', index);
var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象,执行iframe页的方法:
iframeWin.save();
}
});
}
function del(cid) {
//询问框
layer.confirm('是否确定删除分类?', {
icon: 3,
btn: ['确定', '取消'] //按钮
}, function() {
var _token = $('input[name="_token"]').val();
$.post('/admins/article/del_cate', {
cid: cid,
_token: _token
}, function(res) {
if (res.code > 0) {
return layer.alert(res.message, {
icon: 2
});
}
layer.msg('删除分类成功');
setTimeout(function() {
window.location.reload();
}, 1000)
}, 'json');
});
}
</script>
http://www.php520.vip/
测试账号:admin 密码:123456 权限:超管
测试账号:ceshi 密码:123456 权限:普管
一个完整的博客应用不能没有后台管理系统。laravel框架结合layui快速开发一个小型门户博客文章系统,不仅速度快,而且维护方便。