文章分類
- 后臺文章分類列表頁模板導(dǎo)的詳細(xì)步驟
- 建立數(shù)據(jù)表
blog_category,并添加相應(yīng)的文章字段 - 使用
php artisan make:model Category命令,創(chuàng)建Category.php模型文件,并編寫對應(yīng)的屬性代碼 - 在
Admin文件下使用php artisan make:controller CategoryController命令創(chuàng)建基于CommonController的CategoryController控制器 - 在路由文件
routes.php中配置文章分類的資源路由,代碼如下
// 文章分類路由 Route::resource('category', 'CategoryController');- 使用
php artisan route:list命令找到對文章分類進(jìn)行CRUD的方法,寫到CategoryController控制器中,具體方法代碼如下
//get.admin/category 全部分類列表 public function index() { $categorys = Category::all(); return view('admin.category.index')->with('data',$categorys); } //post.admin/category public function store() { } //get.admin/category/create 添加分類 public function create() { } //get.admin/category/{category} 顯示單個分類信息 public function show() { } //delete.admin/category/{category} 刪除單個分類 public function destroy() { } //put.admin/category/{category} 更新分類 public function update() { } //get.admin/category/{category}/edit 編輯分類 public function edit() { }- 將視圖模板中的
list.html拷貝到admin下新建的文件category下,并重新命名為index.blade.php,并利用admin.blade.php重新調(diào)整代碼結(jié)構(gòu)
- 建立數(shù)據(jù)表
- 基本信息展示的詳細(xì)步驟
- 在
index.blade.php中修改導(dǎo)航欄的鏈接為<a href="{{url('admin/info')}}">首頁</a> » 全部分類 - 根據(jù)控制器中傳過來的數(shù)據(jù)調(diào)整<table></table>標(biāo)簽的內(nèi)容和格式,具體代碼如下:
<table class="list_tab"> <tr> <th class="tc" width="5%">排序</th> <th class="tc" width="5%">ID</th> <th>分類名稱</th> <th>標(biāo)題</th> <th>查看次數(shù)</th> <th>操作</th> </tr> @foreach($data as $v) <tr> <td class="tc"> <input type="text" name="ord[]" value="{{$v->cate_order}}"> </td> <td class="tc">{{$v->cate_id}}</td> <td> <a href="#">{{$v->cate_name}}</a> </td> <td>{{$v->cate_title}}</td> <td>{{$v->cate_view}}</td> <td> <a href="#">修改</a> <a href="#">刪除</a> </td> </tr> @endforeach </table> - 在
- 后臺文章分類頁多級分類列表的詳細(xì)步驟
- 給數(shù)據(jù)表
blog_category,添加相應(yīng)的二級分類字段 - 在
Category.php模型文件中添加如下方法
/* * 獲取分類列表數(shù)據(jù) */ public function tree() { $categorys = $this->all(); return $this->getTree($categorys,'cate_name','cate_id','cate_pid'); } /* * 獲取多級分類列表的數(shù)據(jù) */ public function getTree($data,$field_name,$field_id='id',$field_pid='pid',$pid=0) { $arr = array(); foreach ($data as $k=>$v) { if ($v->$field_pid == $pid) { $data[$k]["_".$field_name] = $data[$k][$field_name]; $arr[] = $data[$k]; foreach ($data as $m=>$n) { if ($n->$field_pid == $v->$field_id) { $data[$m]["_".$field_name] = '├─ '.$data[$m][$field_name]; $arr[] = $data[$m]; } } } } return $arr; }- 在
CategoryController.php文件中修改全部分類的方法index,具體代碼如下
//get.admin/category 全部分類列表 public function index() { $categorys = (new Category)->tree(); return view('admin.category.index')->with('data',$categorys); }- 將視圖模板下的
admin下的category下的index.blade.php中的展示分類名稱的行改為
<td> <a href="#">{{$v->_cate_name}}</a> </td> - 給數(shù)據(jù)表
- 后臺文章分類頁Ajax異步修改分類排序的詳細(xì)步驟
- 在路由文件
routes.php中配置修改文章分類排序的路由,代碼如下
// 修改文章分類排序路由 Route::post('cate/changeorder', 'CategoryController@changeOrder');- 在
CategoryController.php文件中編寫修改文章分類排序的方法changeOrder,具體代碼如下
// 修改文章分類排序 public function changeOrder() { $input = Input::all(); $cate = Category::find($input['cate_id']); $cate->cate_order = $input['cate_order']; $res = $cate->update(); if ($res) { $data = [ 'status' => 0, 'msg' => '分類排序更新成功!', ]; } else { $data = [ 'status' => 1, 'msg' => '分類排序更新失敗,請稍后重試!', ]; } return $data; }- 將
layer文件拷貝到resources下的org下 - 將
layer文件下的layer.js引入到視圖模板views下的layouts下的公共php文件admin.blade.php中,具體代碼如下
<script type="text/javascript" src="{{asset('resources/org/layer/layer.js')}}"></script>- 將視圖模板下的
admin下的category下的index.blade.php中的展示分類排序的行改為
<td class="tc"> <input type="text" onchange="changeOrder(this,{{$v->cate_id}})" value="{{$v->cate_order}}"> </td>- 在視圖模板下的
admin下的category下的index.blade.php中添加如下js代碼
<script> function changeOrder(obj,cate_id) { var cate_order = $(obj).val(); $.post("{{url('admin/cate/changeorder')}}",{'_token':'{{csrf_token()}}','cate_id':cate_id,'cate_order':cate_order},function(data) { if(data.status == 0) { layer.msg(data.msg, {icon: 6}); } else { layer.msg(data.msg, {icon: 5}); } }); } - 在路由文件
</script>
* 在`Category.php`模型文件中修改獲取分類列表數(shù)據(jù)的方法(按照排序的升序進(jìn)行獲取數(shù)據(jù)),具體代碼如下objc
/*
* 獲取分類列表數(shù)據(jù)
*/
public function tree()
{
$categorys = $this->orderBy('cate_order','asc')->get();
return $this->getTree($categorys,'cate_name','cate_id','cate_pid');
}
```
- 后臺文章分類添加模板分配的詳細(xì)步驟
- 將視圖模板中的
add.html拷貝到category下并修改名稱為add.blade.php,并利用admin.blade.php重新調(diào)整代碼結(jié)構(gòu) - 根據(jù)數(shù)據(jù)庫中
blog_category表的字段設(shè)計(jì)頁面<table></table>的內(nèi)容 - 修改導(dǎo)航欄的鏈接
- 在
CategoryController.php的create方法中返回add.blade.php視圖
- 將視圖模板中的
- 后臺文章父級分類嵌入的詳細(xì)步驟
- 修改
CategoryController.php的create方法,具體代碼如下
//get.admin/category/create 添加分類 public function create() { $data = Category::where('cate_pid',0)->get(); return view('admin.category.add',compact('data')); }- 編寫
CategoryController.php的store方法,具體代碼如下
//post.admin/category 添加分類提交 public function store() { $input = Input::all(); dd($input); }- 在
add.blade.php文件中修改父級分類選項(xiàng)的代碼,具體如下
<select name="cate_pid"> <option value="0">==頂級分類==</option> @foreach($data as $d) <option value="{{$d->cate_id}}">{{$d->cate_name}}</option> @endforeach </select> - 修改
- 后臺文章分類添加數(shù)據(jù)Validation驗(yàn)證的詳細(xì)步驟
- 在
CategoryController.php文件中的添加分類提交的方法store中,進(jìn)行后臺文章分類添加數(shù)據(jù)Validation驗(yàn)證,具體代碼如下
//post.admin/category 添加分類提交 public function store() { $input = Input::except('_token'); $rules = [ 'cate_name' => 'required', ]; $message = [ 'cate_name.required' => '分類名稱不能為空!', ]; // 表單輸入驗(yàn)證 $validator = Validator::make($input,$rules,$message); if ($validator->passes()) { return redirect('admin/category'); } else { return back()->withErrors($validator); } }- 在
add.blade.php文件寫上驗(yàn)證錯誤信息提示的代碼,具體如下
- 在
- 后臺文章分類編輯的詳細(xì)步驟
- 在
CategoryController.php文件中編寫edit方法,具體代碼如下
//get.admin/category/{category}/edit 編輯分類 public function edit($cate_id) { $field = Category::find($cate_id); $data = Category::where('cate_pid',0)->get(); return view('admin.category.edit',compact('field','data')); }- 拷貝
add.blade.php到當(dāng)前文件并修改名稱為edit.blade.php,并修改編輯選項(xiàng)的value值 - 修改
edit.blade.php文件中的頂級分類代碼如下
- 在
<select name="cate_pid">
<option value="0">==頂級分類==</option>
@foreach($data as $d)
<option value="{{$d->cate_id}}"
@if($d->cate_id==$field->cate_pid) selected @endif
>{{$d->cate_name}}</option>
@endforeach
</select>
* 修改`category`下的`index.blade.php`中的修改選項(xiàng)的鏈接為`<a href="{{url('admin/category/'.$v->cate_id.'/edit')}}">修改</a>`
- 后臺文章分類模擬PUT方法提交表單的象征步驟
- 在
CategoryController.php文件中編寫update方法,具體代碼如下
//put.admin/category/{category} 更新分類 public function update($cate_id) { $input = Input::except('_token','_method'); $re = Category::where('cate_id',$cate_id)->update($input); if($re){ return redirect('admin/category'); }else{ return back()->with('errors','分類信息更新失敗,請稍后重試!'); } }- 在
edit.blade.php文件中的<form></form>標(biāo)簽中添加如下代碼
<input type="hidden" name="_method" value="put"> {{csrf_field()}} - 在
- 后臺文章分類刪除及Ajax異步模擬DELETE方法的詳細(xì)步驟
- 在
CategoryController.php的destory方法中添加如下代碼
//delete.admin/category/{category} 刪除單個分類 public function destroy($cate_id) { $res = Category::where('cate_id',$cate_id)->delete(); // 刪除頂級分類時(shí),底下的分類變成頂級分類 Category::where('cate_pid',$cate_id)->update(['cate_pid'=>0]); if ($res) { $data = [ 'status' => 0, 'msg' => '分類刪除成功!', ]; } else { $data = [ 'status' => 1, 'msg' => '分類刪除失敗,請稍后重試!', ]; } return $data; }- 在
category下的index.blade.php文件中修改刪除的鏈接為<a href="javascript:;" onclick="delCate({{$v->cate_id}})">刪除</a>,并添加刪除分類的JS代碼,具體代碼如下
// 刪除分類 function delCate(cate_id) { layer.confirm('您確定要刪除這個分類嗎?', { btn:['確定', '取消'] }, function(){ $.post("{{url('admin/category')}}/"+cate_id,{'_method':'delete','_token':"{{csrf_token()}}"}, function (data) { if(data.status == 0) { location.href = location.href; layer.msg(data.msg, {icon:6}); } else { layer.msg(data.msg, {icon:5}); } }) }); }- 修改
category下的各個文件的頁面布局
- 在
文章
- 數(shù)據(jù)庫文章表的創(chuàng)建及添加文章模板導(dǎo)入的詳細(xì)步驟
- 新建數(shù)據(jù)表
blog_article - 使用命令
php artisan make:controller ArticleController創(chuàng)建文章控制器 - 在
route.php文件中配置文章的資源路由,具體代碼如下
// 文章資源路由 Route::resource('article', 'ArticleController');- 在
ArticleController控制器中添加全部文章列表index方法,具體代碼如下
//get.admin/article 全部文章列表 public function index() { echo '全部文章列表'; }- 在
resources下的views下的admin下新建文件article,并拷貝category下的add.blade.php到該文件下,做文字顯示的修改工作 - 在
ArticleController控制器中添加文章create方法,具體代碼如下
//get.admin/article 全部文章列表 //get.admin/article/create 添加文章 public function create() { $data = []; return view('admin.article.add',compact('data')); } - 新建數(shù)據(jù)表
- 后臺文章添加及百度編輯器Ueditor嵌入的詳細(xì)步驟
- 將下載好的編輯器的文件夾拷貝到
resources下的org下,并重新命名為ueditor - 修改
resources下的views下的admin下的article下的add.blade.php文件中的<table></table>里面的代碼,具體代碼如下
<table class="add_tab"> <tbody> <tr> <th width="120">分類:</th> <td> <select name="cate_id"> @foreach($data as $d) <option value="{{$d->cate_id}}">{{$d->_cate_name}}</option> @endforeach </select> </td> </tr> <tr> <th><i class="require">*</i> 文章標(biāo)題:</th> <td> <input type="text" class="lg" name="art_title"> </td> </tr> <tr> <th>編輯:</th> <td> <input type="text" class="sm" name="art_editor"> </td> </tr> <tr> <th>縮略圖:</th> <td> <input type="text" size="50" name="art_thumb"> </td> </tr> <tr> <th>關(guān)鍵詞:</th> <td> <input type="text" class="lg" name="art_tag"> </td> </tr> <tr> <th>描述:</th> <td> <textarea name="art_description"></textarea> </td> </tr> <tr> <th>文章內(nèi)容:</th> <td> <script type="text/javascript" charset="utf-8" src="{{asset('resources/org/ueditor/ueditor.config.js')}}"></script> <script type="text/javascript" charset="utf-8" src="{{asset('resources/org/ueditor/ueditor.all.min.js')}}"> </script> <script type="text/javascript" charset="utf-8" src="{{asset('resources/org/ueditor/lang/zh-cn/zh-cn.js')}}"></script> <script id="editor" name="art_content" type="text/plain" style="width:860px;height:500px;"></script> <script type="text/javascript"> var ue = UE.getEditor('editor'); </script> <style> .edui-default{line-height: 28px;} div.edui-combox-body,div.edui-button-body,div.edui-splitbutton-body {overflow: hidden; height:20px;} div.edui-box{overflow: hidden; height:22px;} </style> </td> </tr> <tr> <th></th> <td> <input type="submit" value="提交"> <input type="button" class="back" onclick="history.go(-1)" value="返回"> </td> </tr> </tbody> </table>- 在數(shù)據(jù)表
blog_article中添加cate_id文章分類字段 - 在
ArticleController控制器中修改添加文章create方法,具體代碼如下
//get.admin/article/create 添加文章 public function create() { $data = (new Category)->tree(); return view('admin.article.add',compact('data')); } - 將下載好的編輯器的文件夾拷貝到
- 后臺文章添加縮略圖上傳之uploadify插件引入的詳細(xì)步驟
- 將下載好的uploadify插件的文件夾拷貝到
resources下的org下 - 修改
resources下的views下的admin下的article下的add.blade.php文件中的縮略圖部分的代碼,具體代碼如下
<th>縮略圖:</th> <td> <input type="text" size="50" name="art_thumb"> <input id="file_upload" name="file_upload" type="file" multiple="true"> <script src="{{asset('resources/org/uploadify/jquery.uploadify.min.js')}}" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="{{asset('resources/org/uploadify/uploadify.css')}}"> <script type="text/javascript"> <?php $timestamp = time();?> $(function() { $('#file_upload').uploadify({ 'buttonText' : '圖片上傳', 'formData' : { 'timestamp' : '<?php echo $timestamp;?>', '_token' : "{{csrf_token()}}" }, 'swf' : "{{asset('resources/org/uploadify/uploadify.swf')}}", 'uploader' : "{{url('admin/upload')}}" }); }); </script> <style> .uploadify{display:inline-block;} .uploadify-button{border:none; border-radius:5px; margin-top:8px;} table.add_tab tr td span.uploadify-button-text{color: #FFF; margin:0;} </style> </td> - 將下載好的uploadify插件的文件夾拷貝到
- 后臺文章添加縮略圖上傳之文件存儲的詳步驟
- 修改
commonController.php中的upload方法,具體代碼如下
//圖片上傳 public function upload() { $file = Input::file('Filedata'); if ($file -> isValid()) { $entension = $file -> getClientOriginalExtension(); // 上傳文件的后綴 $newName = date('YmdHis').mt_rand(100,999).'.'.$entension; // 上傳文件的路徑 $path = $file -> move(base_path().'/uploads',$newName); $filepath = 'uploads/'.$newName; return $filepath; } }- 修改
resources下的views下的admin下的article下的add.blade.php文件中的縮略圖部分的代碼,具體代碼如下
<tr> <th>縮略圖:</th> <td> <input type="text" size="50" name="art_thumb"> <input id="file_upload" name="file_upload" type="file" multiple="true"> <script src="{{asset('resources/org/uploadify/jquery.uploadify.min.js')}}" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="{{asset('resources/org/uploadify/uploadify.css')}}"> <script type="text/javascript"> <?php $timestamp = time();?> $(function() { $('#file_upload').uploadify({ 'buttonText' : '圖片上傳', 'formData' : { 'timestamp' : '<?php echo $timestamp;?>', '_token' : "{{csrf_token()}}" }, 'swf' : "{{asset('resources/org/uploadify/uploadify.swf')}}", 'uploader' : "{{url('admin/upload')}}", 'onUploadSuccess' : function(file, data, response) { $('input[name=art_thumb]').val(data); $('#art_thumb_img').attr('src','/'+data); } }); }); </script> <style> .uploadify{display:inline-block;} .uploadify-button{border:none; border-radius:5px; margin-top:8px;} table.add_tab tr td span.uploadify-button-text{color: #FFF; margin:0;} </style> </td> </tr> - 修改
- 后臺文章添加數(shù)據(jù)及Validation驗(yàn)證的詳細(xì)步驟
- 使用命令
php artisan make:mode Article創(chuàng)建文章模型 - 修改文章模型的命名空間并添加相應(yīng)的屬性
- 在
ArticleController.php文章控制器中添加添加文章提交的方法store,具體代碼如下
//post.admin/article 添加文章提交 public function store() { $input = Input::except('_token'); $input['art_time'] = time(); $rules = [ 'art_title'=>'required', 'art_content'=>'required', ]; $message = [ 'art_title.required'=>'文章名稱不能為空!', 'art_content.required'=>'文章內(nèi)容不能為空!', ]; $validator = Validator::make($input,$rules,$message); if($validator->passes()){ $re = Article::create($input); if($re){ return redirect('admin/article'); }else{ return back()->with('errors','數(shù)據(jù)填充失敗,請稍后重試!'); } }else{ return back()->withErrors($validator); } } - 使用命令
- 后臺文章頁列表展示及分頁功能實(shí)現(xiàn)的詳細(xì)步驟
- 在
ArticleController.php文章控制器中修改全部文章列表的方法index,具體代碼如下
//get.admin/article 全部文章列表 public function index() { $data = Article::orderBy('art_id','desc')->paginate(1); return view('admin.article.index',compact('data')); }- 將視圖模板中的
list.html拷貝到article下并重新命名為為index.blade.php,并利用admin.blade.php重新調(diào)整代碼結(jié)構(gòu),其中form表單中和樣式布局的具體代碼如下
<form action="#" method="post"> <div class="result_wrap"> <!--快捷導(dǎo)航 開始--> <div class="result_title"> <h3>文章列表</h3> </div> <div class="result_content"> <div class="short_wrap"> <a href="{{url('admin/article/create')}}"><i class="fa fa-plus"></i>添加文章</a> <a href="{{url('admin/article')}}"><i class="fa fa-recycle"></i>全部文章</a> </div> </div> <!--快捷導(dǎo)航 結(jié)束--> </div> <div class="result_wrap"> <div class="result_content"> <table class="list_tab"> <tr> <th class="tc">ID</th> <th>標(biāo)題</th> <th>點(diǎn)擊</th> <th>編輯</th> <th>發(fā)布時(shí)間</th> <th>操作</th> </tr> @foreach($data as $v) <tr> <td class="tc">{{$v->art_id}}</td> <td> <a href="#">{{$v->art_title}}</a> </td> <td>{{$v->art_view}}</td> <td>{{$v->art_editor}}</td> <td>{{date('Y-m-d',$v->art_time)}}</td> <td> <a href="{{url('admin/article/'.$v->art_id.'/edit')}}">修改</a> <a href="javascript:;" onclick="delArt({{$v->art_id}})">刪除</a> </td> </tr> @endforeach </table> <div class="page_list"> {{$data->links()}} </div> </div> </div> </form> <style> .result_content ul li span { font-size: 15px; padding: 6px 12px; } </style> - 在
- 后臺文章編輯的詳細(xì)步驟
- 在
ArticleController.php文章控制器中添加編輯文章列表的方法edit,具體代碼如下
//get.admin/article/{article}/edit 編輯文章 public function edit($art_id) { $data = (new Category)->tree(); $field = Article::find($art_id); return view('admin.article.edit',compact('data','field')); }- 拷貝視圖模板
article下的add.blade.php并重新命名為為edit.blade.php,并利用admin.blade.php重新調(diào)整代碼結(jié)構(gòu),其中form表單中和樣式布局的具體代碼如下
<form action="{{url('admin/article/'.$field->art_id)}}" method="post"> <input type="hidden" name="_method" value="put"> {{csrf_field()}} <table class="add_tab"> <tbody> <tr> <th width="120">分類:</th> <td> <select name="cate_id"> @foreach($data as $d) <option value="{{$d->cate_id}}" @if($field->cate_id==$d->cate_id) selected @endif >{{$d->_cate_name}}</option> @endforeach </select> </td> </tr> <tr> <th><i class="require">*</i> 文章標(biāo)題:</th> <td> <input type="text" class="lg" name="art_title" value="{{$field->art_title}}"> </td> </tr> <tr> <th>編輯:</th> <td> <input type="text" class="sm" name="art_editor" value="{{$field->art_editor}}"> </td> </tr> <tr> <th>縮略圖:</th> <td> <input type="text" size="50" name="art_thumb" value="{{$field->art_thumb}}"> <input id="file_upload" name="file_upload" type="file" multiple="true"> <script src="{{asset('resources/org/uploadify/jquery.uploadify.min.js')}}" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="{{asset('resources/org/uploadify/uploadify.css')}}"> <script type="text/javascript"> <?php $timestamp = time();?> $(function() { $('#file_upload').uploadify({ 'buttonText' : '圖片上傳', 'formData' : { 'timestamp' : '<?php echo $timestamp;?>', '_token' : "{{csrf_token()}}" }, 'swf' : "{{asset('resources/org/uploadify/uploadify.swf')}}", 'uploader' : "{{url('admin/upload')}}", 'onUploadSuccess' : function(file, data, response) { $('input[name=art_thumb]').val(data); $('#art_thumb_img').attr('src','/blog/'+data); - 在
// alert(data);
}
});
});
</script>
<style>
.uploadify{display:inline-block;}
.uploadify-button{border:none; border-radius:5px; margin-top:8px;}
table.add_tab tr td span.uploadify-button-text{color: #FFF; margin:0;}
</style>
</td>
</tr>
<tr>
<th></th>
<td>
<img alt="" id="art_thumb_img" style="max-width: 350px; max-height:100px;" src="/blog/{{$field->art_thumb}}">
</td>
</tr>
<tr>
<th>關(guān)鍵詞:</th>
<td>
<input type="text" class="lg" name="art_tag" value="{{$field->art_tag}}">
</td>
</tr>
<tr>
<th>描述:</th>
<td>
<textarea name="art_description">{{$field->art_description}}</textarea>
</td>
</tr>
<tr>
<th>文章內(nèi)容:</th>
<td>
<script type="text/javascript" charset="utf-8" src="{{asset('resources/org/ueditor/ueditor.config.js')}}"></script>
<script type="text/javascript" charset="utf-8" src="{{asset('resources/org/ueditor/ueditor.all.min.js')}}"> </script>
<script type="text/javascript" charset="utf-8" src="{{asset('resources/org/ueditor/lang/zh-cn/zh-cn.js')}}"></script>
<script id="editor" name="art_content" type="text/plain" style="width:860px;height:500px;">{!! $field->art_content !!}</script>
<script type="text/javascript">
var ue = UE.getEditor('editor');
</script>
<style>
.edui-default{line-height: 28px;}
div.edui-combox-body,div.edui-button-body,div.edui-splitbutton-body
{overflow: hidden; height:20px;}
div.edui-box{overflow: hidden; height:22px;}
</style>
</td>
</tr>
<tr>
<th></th>
<td>
<input type="submit" value="提交">
<input type="button" class="back" onclick="history.go(-1)" value="返回">
</td>
</tr>
</tbody>
</table>
</form>
```
* 將視圖模板`article`下的`index.blade.php`中修改的鏈接改為`<a href="{{url('admin/article/'.$v->art_id.'/edit')}}">修改</a>`
* 在`ArticleController.php`文章控制器中添加編輯文章列表的方法`update`,具體代碼如下
```objc
//put.admin/article/{article} 更新文章
public function update($art_id)
{
$input = Input::except('_token','_method');
$re = Article::where('art_id',$art_id)->update($input);
if($re){
return redirect('admin/article');
}else{
return back()->with('errors','文章更新失敗,請稍后重試!');
}
}
```
- 后臺文章刪除的詳細(xì)步驟
- 在
ArticleController.php文章控制器中添加編輯文章列表的方法destroy,具體代碼如下
//delete.admin/article/{article} 刪除單個文章 public function destroy($art_id) { $re = Article::where('art_id',$art_id)->delete(); if($re){ $data = [ 'status' => 0, 'msg' => '文章刪除成功!', ]; }else{ $data = [ 'status' => 1, 'msg' => '文章刪除失敗,請稍后重試!', ]; } return $data; }- 在視圖模板
article下的index.blade.php添加刪除確認(rèn)的彈框,具體代碼如下
<script> //刪除分類 function delArt(art_id) { layer.confirm('您確定要刪除這篇文章嗎?', { btn: ['確定','取消'] //按鈕 }, function(){ $.post("{{url('admin/article/')}}/"+art_id,{'_method':'delete','_token':"{{csrf_token()}}"},function (data) { if(data.status==0){ location.href = location.href; layer.msg(data.msg, {icon: 6}); }else{ layer.msg(data.msg, {icon: 5}); } }); - 在
// layer.msg('的確很重要', {icon: 1});
}, function(){
});
}
</script>
```
* 將視圖模板`article`下的`index.blade.php`中刪除的鏈接改為`<a href="javascript:;" onclick="delArt({{$v->art_id}})">刪除</a>`
* 修改視圖模板`style`下的`index.blade.php`中左側(cè)列表,具體代碼如下
```objc
<ul class="sub_menu">
<li><a href="{{url('admin/category/create')}}" target="main"><i class="fa fa-fw fa-plus-square"></i>添加分類</a></li>
<li><a href="{{url('admin/category')}}" target="main"><i class="fa fa-fw fa-list-ul"></i>分類列表</a></li>
<li><a href="{{url('admin/article/create')}}" target="main"><i class="fa fa-fw fa-plus-square"></i>添加文章</a></li>
<li><a href="{{url('admin/article')}}" target="main"><i class="fa fa-fw fa-list-ul"></i>文章列表</a></li>
</ul>
```
友情鏈接模塊
- 友情鏈接模塊使用Migrations數(shù)據(jù)遷移創(chuàng)建數(shù)據(jù)表的詳細(xì)步驟
- 使用命令
php artisan make:migration create_links_table創(chuàng)建數(shù)據(jù)庫遷移文件 - 數(shù)據(jù)庫遷移文件中的代碼如下所示
public function up() { Schema::create('links', function (Blueprint $table) { $table->engine = 'MyISAM'; $table->increments('link_id'); $table->string('link_name')->default('')->comment('//名稱'); $table->string('link_title')->default('')->comment('//標(biāo)題'); $table->string('link_url')->default('')->comment('//鏈接'); $table->integer('link_order')->default('')->comment('//排序'); }); } public function down() { Schema::drop('links'); }- 使用命令
php artisan migrate執(zhí)行創(chuàng)建數(shù)據(jù)庫遷移文件
- 使用命令
- 友情鏈接模塊使用Seeding填充測試數(shù)據(jù)的詳細(xì)步驟
- 使用
php artisan make:seeder LinksTableSeeder命令創(chuàng)建種子文件 - 在
php artisan make:seeder LinksTableSeeder文件中添加如下代碼
public function run() { $data = [ [ 'link_name' => '后盾網(wǎng)', 'link_title' => '國內(nèi)口碑最好', 'link_url' => 'http://www.houdunwang.com', 'link_order' => 1, ], [ 'link_name' => '后盾論壇', 'link_title' => '人人做后盾', 'link_url' => 'http://bbs.houdunwang.com', 'link_order' => 2, ] ]; DB::table('links')->insert($data); }- 在
DatabaseSeeder.php中修改代碼,具體如下
public function run() { $this->call(LinksTableSeeder::class); }- 使用
php artisan db:seed命令執(zhí)行種子文件,填充測試數(shù)據(jù)
- 使用
- 友情鏈接模塊列表頁面展示的詳細(xì)步驟
- 在
routes.php中配置友情鏈接的資源路由,具體代碼是Route::resource('links', 'LinksController'); - 使用命令
php artisan make:controller LinksController創(chuàng)建友情鏈接控制器 - 使用命令
php artisan make:mode Links創(chuàng)建友情鏈接模型 - 在
LinksController.php文章控制器中添加全部友情鏈接列表的方法index,具體代碼如下
//get.admin/links 全部友情鏈接列表 public function index() { $data = Links::orderBy('link_order','asc')->get(); return view('admin.links.index',compact('data')); }- 拷貝
resources文件下的views下的category到當(dāng)前文件并重新命名為links - 修改
links下的index.blade.php文件中的代碼
- 在
- 友情鏈接模塊Ajax異步修改排序的詳細(xì)步驟
- 在
routes.php中配置修改友情鏈接排序的路由,具體代碼是Route::post('links/changeorder', 'LinksController@changeOrder'); - 在
LinksController.php文章控制器中添加修改友情鏈接排序的方法changeOrder,具體代碼如下
public function changeOrder() { $input = Input::all(); $links = Links::find($input['link_id']); $links->link_order = $input['link_order']; $re = $links->update(); if($re){ $data = [ 'status' => 0, 'msg' => '友情鏈接排序更新成功!', ]; }else{ $data = [ 'status' => 1, 'msg' => '友情鏈接排序更新失敗,請稍后重試!', ]; } return $data; }- 在
LinksController.php文章控制器中添加修改友情鏈接排序的方法show,具體代碼如下
//get.admin/category/{category} 顯示單個分類信息 public function show() { }- 修改
links下的index.blade.php文件中的代碼
- 在
- 友情鏈接模塊之友情鏈接添加的詳細(xì)步驟
- 在
LinksController.php文章控制器中添加全部友情鏈接列表的方法create,具體代碼如下
//get.admin/links/create 添加友情鏈接 public function create() { return view('admin.links.add'); }- 修改
links下的add.blade.php文件中的代碼 - 在
LinksController.php文章控制器中添加全部友情鏈接列表的方法store,具體代碼如下
//post.admin/links 添加友情鏈接提交 public function store() { $input = Input::except('_token'); $rules = [ 'link_name'=>'required', 'link_url'=>'required', ]; $message = [ 'link_name.required'=>'友情鏈接名稱不能為空!', 'link_url.required'=>'友情鏈接URL不能為空!', ]; $validator = Validator::make($input,$rules,$message); if($validator->passes()){ $re = Links::create($input); if($re){ return redirect('admin/links'); }else{ return back()->with('errors','友情鏈接失敗,請稍后重試!'); } }else{ return back()->withErrors($validator); } } - 在
- 友情鏈接模塊之友情鏈接修改的詳細(xì)步驟
- 在
LinksController.php友情鏈接控制器中添加全部友情鏈接列表的方法edit,具體代碼如下
//get.admin/links/{links}/edit 編輯友情鏈接 public function edit($link_id) { $field = Links::find($link_id); return view('admin.links.edit',compact('field')); }- 修改
links下的edit.blade.php文件中的代碼 - 在
LinksController.php友情鏈接控制器中添加全部友情鏈接列表的方法destroy,具體代碼如下
//delete.admin/links/{links} 刪除友情鏈接 public function destroy($link_id) { $re = Links::where('link_id',$link_id)->delete(); if($re){ $data = [ 'status' => 0, 'msg' => '友情鏈接刪除成功!', ]; }else{ $data = [ 'status' => 1, 'msg' => '友情鏈接刪除失敗,請稍后重試!', ]; } return $data; }- 在
links下的index.blade.php文件中添加如下的代碼
//刪除友情鏈接 function delLinks(link_id) { layer.confirm('您確定要刪除這個鏈接嗎?', { btn: ['確定','取消'] //按鈕 }, function(){ $.post("{{url('admin/links/')}}/"+link_id,{'_method':'delete','_token':"{{csrf_token()}}"},function (data) { if(data.status==0){ location.href = location.href; layer.msg(data.msg, {icon: 6}); }else{ layer.msg(data.msg, {icon: 5}); } }); - 在
// layer.msg('的確很重要', {icon: 1});
}, function(){
});
}
```
- 在
style下的index.blade.php文件中修改系統(tǒng)設(shè)置下的代碼具體的代碼如下<ul class="sub_menu " style="display: block;"> <li><a href="{{url('admin/links')}}" target="main"><i class="fa fa-fw fa-cubes"></i>友情鏈接</a></li> <li><a href="#" target="main"><i class="fa fa-fw fa-database"></i>備份還原</a></li> </ul>
自定義導(dǎo)航模塊
- 在友情鏈接模塊基礎(chǔ)上完成自定義導(dǎo)航模塊的詳細(xì)步驟
- 創(chuàng)建數(shù)據(jù)表
blog_liks - 分別在對應(yīng)的控制器,模型和視圖文件下進(jìn)行拷貝,并做相應(yīng)的修改
- 創(chuàng)建數(shù)據(jù)表
網(wǎng)站配置模塊
- 數(shù)據(jù)表創(chuàng)建
- 創(chuàng)建數(shù)據(jù)表名為
blog_config,并添加相應(yīng)的數(shù)據(jù)表字段
- 創(chuàng)建數(shù)據(jù)表名為
- 添加網(wǎng)站配置項(xiàng)
- 在
route.php文件中配置網(wǎng)站配置的資源路由
// 網(wǎng)站配置的資源路由 Route::resource('config', 'ConfigController');- 拷貝
LinksController.php友情鏈接控制器到當(dāng)前文件內(nèi)并重新命名為ConfigController.php,并修改網(wǎng)站配置控制器中的代碼 - 拷貝
Links.php友情鏈接模型到當(dāng)前文件內(nèi)并重新命名為Config.php,并修改網(wǎng)站配置模型中的代碼 - 拷貝
resources下的views下的links友情鏈接到當(dāng)前文件內(nèi)并重新命名為config - 修改
resources下的views下的config文件下的add.blade.php中的代碼
- 在
- 網(wǎng)站配置項(xiàng)列表以及異步修改排序
- 修改
resources下的views下的config文件下的index.blade.php中的代碼 - 在
route.php文件中配置網(wǎng)站配置的資源路由
// 修改網(wǎng)站配置排序路由 Route::post('config/changeorder', 'ConfigController@changeOrder'); - 修改
- 網(wǎng)站配置項(xiàng)修改
- 修改
resources下的views下的config文件下的edit.blade.php中的代碼
- 修改
- 網(wǎng)站配置值分類類型顯示
- 修改
ConfigController.php項(xiàng)目配置控制器的全部配置列表方法index,具體代碼如下
//get.admin/config 全部配置項(xiàng)列表 public function index() { $data = Config::orderBy('conf_order','asc')->get(); foreach ($data as $k=>$v){ switch ($v->field_type){ case 'input': $data[$k]->_html = '<input type="text" class="lg" name="conf_content" value="'.$v->conf_content.'">'; break; case 'textarea': $data[$k]->_html = '<textarea type="text" class="lg" name="conf_content">'.$v->conf_content.'</textarea>'; break; case 'radio': //1|開啟,0|關(guān)閉 $arr = explode(',',$v->field_value); $str = ''; foreach($arr as $m=>$n){ //1|開啟 $r = explode('|',$n); $c = $v->conf_content==$r[0]?' checked ':''; $str .= '<input type="radio" name="conf_content" value="'.$r[0].'"'.$c.'>'.$r[1].' '; } $data[$k]->_html = $str; break; } } return view('admin.config.index',compact('data')); }- 修改
resources下的views下的config文件下的index.blade.php中的代碼,增加配置內(nèi)容的顯示項(xiàng),在<table></table>標(biāo)簽中添加如下代碼
<td> {!! $v->_html !!} </td> - 修改
- 網(wǎng)站配置值修改
- 修改
resources下的views下的config文件下的index.blade.php中的代碼,添加form表單和提交按鈕
<form action="{{url('admin/config/changecontent')}}" method="post"> <div class="btn_group"> <input type="submit" value="提交"> <input type="button" class="back" onclick="history.go(-1)" value="返回" > </div> </form>- 在
route.php文件中配置網(wǎng)站配置內(nèi)容路由
// 修改網(wǎng)站配置內(nèi)容路由 Route::post('config/changecontent', 'ConfigController@changeContent');- 在
ConfigController.php項(xiàng)目配置控制器添加修改網(wǎng)站配置內(nèi)容的方法changeContent,具體代碼如下
public function changeContent() { $input = Input::all(); foreach($input['conf_id'] as $k=>$v){ Config::where('conf_id',$v)->update(['conf_content'=>$input['conf_content'][$k]]); } $this->putFile(); return back()->with('errors','配置項(xiàng)更新成功!'); }- 修改
ConfigController.php項(xiàng)目配置控制器中全部配置項(xiàng)列表的方法index,將name="conf_content"改為name="conf_content[]" - 修改
resources下的views下的config文件下的index.blade.php中的代碼,在顯示網(wǎng)站配置內(nèi)容的地方加上如下代碼<input type="hidden" name="conf_id[]" value="{{$v->conf_id}}">
- 修改
- 生成網(wǎng)站配置項(xiàng)文件
- 修改
ConfigController.php項(xiàng)目配置控制器中添加生成配置文件的方法putFile,具體代碼如下
// 生成配置文件 public function putFile() { $config = Config::pluck('conf_content','conf_name')->all(); $path = base_path().'/config/web.php'; $str = '<?php return '.var_export($config,true).';'; file_put_contents($path,$str); }- 在
route.php文件中配置生成配置文路由
// 生成配置文件的路由 Route::get('config/putfile', 'ConfigController@putFile');- 在
ConfigController.php項(xiàng)目配置控制器中的方法changeContent,destroy,update方法中加上代碼$this->putFile(); - 修改
resources下的views下的style文件下的index.blade.php中的代碼如下
<li><a href="{{url('admin/config')}}" target="main"><i class="fa fa-fw fa-cogs"></i>網(wǎng)站配置</a></li> - 修改