Laravel大型项目系列教程(五)之文章和标签管理
Laravel大型項(xiàng)目系列教程(五)之文章和標(biāo)簽管理
本節(jié)教程將大概完成文章和標(biāo)簽管理。
1.文章管理
首先創(chuàng)建管理后臺(tái)文章列表視圖:
$ php artisan generate:view admin.articles.list修改views/admin/articles/list.blade.php:
@extends('_layouts.default')@section('main') <div class="am-g am-g-fixed blog-g-fixed"><div class="am-u-sm-12"><table class="am-table am-table-hover am-table-striped "><thead><tr><th>Title</th><th>Tags</th><th>Author</th><th>Managment</th></tr></thead><tbody>@foreach ($articles as $article)<tr><td><a href="{{ URL::route('article.show', $article->id) }}">{{{ $article->title }}}</a></td><td>@foreach ($article->tags as $tag)<span class="am-badge am-badge-success am-radius">{{ $tag->name }}</span>@endforeach</td><td><a href="{{ URL::to('user/' . $article->user->id . '/articles') }}">{{{ $article->user->nickname }}}</a></td><td><a href="{{ URL::to('article/'. $article->id . '/edit') }}" class="am-btn am-btn-xs am-btn-primary"><span class="am-icon-pencil"></span> Edit</a>{{ Form::open(array('url' => 'article/' . $article->id, 'method' => 'DELETE', 'style' => 'display: inline;')) }}<button type="button" class="am-btn am-btn-xs am-btn-danger" id="delete{{ $article->id }}"><span class="am-icon-remove"></span> Delete</button>{{ Form::close() }}</td></tr>@endforeach</tbody></table></div> </div> <div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm"><div class="am-modal-dialog"><div class="am-modal-bd"></div><div class="am-modal-footer"><span class="am-modal-btn" data-am-modal-cancel>No</span><span class="am-modal-btn" data-am-modal-confirm>Yes</span></div></div> </div> <script>$(function() {$('[id^=delete]').on('click', function() {$('.am-modal-bd').text('Sure you want to delete it?');$('#my-confirm').modal({relatedTarget: this,onConfirm: function(options) {$(this.relatedTarget).parent().submit();},onCancel: function() {}});});}); </script> @stop在nav.blade.php中增加一個(gè)Articles的超鏈接:
<li class="{{ (isset($page) and ($page == 'articles')) ? 'am-active' : '' }}"><a href="{{ URL::to('admin/articles') }}">Articles</a></li>創(chuàng)建一個(gè)管理員控制器,在app/controllers下創(chuàng)建一個(gè)名為AdminController.php的文件,修改:
class AdminController extends \BaseController {public function articles(){return View::make('admin.articles.list')->with('articles', Article::with('user', 'tags')->orderBy('created_at', 'desc')->get())->with('page', 'articles');} }在Route::group(array('prefix' => 'admin')中增加:
Route::get('articles', 'AdminController@articles');管理文章可以重用上節(jié)教程寫的業(yè)務(wù)邏輯,修改下ArticleController.php,把destroy()中最后的Redirect::to('home')改成Redirect::back(), 再修改一下home.blade.php,加一個(gè)是否是管理員的判斷,這樣當(dāng)點(diǎn)擊作者跳轉(zhuǎn)到用戶主頁時(shí),除了作者自己管理員也能操作文章:
@if ($user->id == Auth::id() or Auth::user()->is_admin)現(xiàn)在點(diǎn)擊導(dǎo)航欄的Articles,就會(huì)出現(xiàn)所有的文章:
這樣管理員就可以操作所有的文章了。
我們還可以再修改下admin/users/list.blade.php,當(dāng)點(diǎn)擊用戶列表的昵稱時(shí)也會(huì)跳轉(zhuǎn)到用戶主頁:
<a href="{{ URL::to('user/' . $user->id . '/articles') }}">{{{ $user->nickname }}}</a>現(xiàn)在訪問用戶列表頁面:
2.顯示標(biāo)簽列表
創(chuàng)建一個(gè)標(biāo)簽列表視圖:
$ php artisan generate:view admin.tags.list修改admin/tags/list.blade.php:
@extends('_layouts.default')@section('main') <div class="am-g am-g-fixed blog-g-fixed"><div class="am-u-sm-12"><table class="am-table am-table-hover am-table-striped "><thead><tr><th>TagName</th><th>ArticleCount</th><th>CreateDateTime</th><th>Managment</th></tr></thead><tbody>@foreach ($tags as $tag)<tr><td>{{{ $tag->name }}}</td><td>{{ $tag->count }}</td><td>{{ $tag->created_at->format('Y-m-d H:i') }}</td><td><a href="{{ URL::to('tag/'. $tag->id . '/edit') }}" class="am-btn am-btn-xs am-btn-primary"><span class="am-icon-pencil"></span> Edit</a>{{ Form::open(array('url' => 'tag/' . $tag->id, 'method' => 'DELETE', 'style' => 'display: inline;')) }}<button type="button" class="am-btn am-btn-xs am-btn-danger" id="delete{{ $tag->id }}"><span class="am-icon-remove"></span> Delete</button>{{ Form::close() }}</td></tr>@endforeach</tbody></table></div> </div> <div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm"><div class="am-modal-dialog"><div class="am-modal-bd"></div><div class="am-modal-footer"><span class="am-modal-btn" data-am-modal-cancel>No</span><span class="am-modal-btn" data-am-modal-confirm>Yes</span></div></div> </div> <script>$(function() {$('[id^=delete]').on('click', function() {$('.am-modal-bd').text('Sure you want to delete it?');$('#my-confirm').modal({relatedTarget: this,onConfirm: function(options) {$(this.relatedTarget).parent().submit();},onCancel: function() {}});});}); </script> @stop再在nav.blade.php中增加Tags選項(xiàng):
<li class="{{ (isset($page) and ($page == 'tags')) ? 'am-active' : '' }}"><a href="{{ URL::to('admin/tags') }}">Tags</a></li>在Route::group(array('prefix' => 'admin')中增加:
Route::get('tags', 'AdminController@tags');在AdminController.php中增加:
public function tags() {return View::make('admin.tags.list')->with('tags', Tag::all()->orderBy('count', 'desc')); }現(xiàn)在點(diǎn)擊導(dǎo)航欄上方的Tags超鏈接:
3.修改標(biāo)簽
創(chuàng)建修改標(biāo)簽的視圖:
$ php artisan generate:view tags.edit修改views/tags/edit.blade.php:
@extends('_layouts.default')@section('main') <div class="am-g am-g-fixed"><div class="am-u-sm-12"><h1>Edit Tag</h1><hr/>@if (Session::has('message'))<div class="am-alert am-alert-{{ Session::get('message')['type'] }}" data-am-alert><p>{{ Session::get('message')['content'] }}</p></div>@endif@if ($errors->has())<div class="am-alert am-alert-danger" data-am-alert><p>{{ $errors->first() }}</p></div>@endif{{ Form::model($tag, array('url' => URL::route('tag.update', $tag->id), 'method' => 'PUT', 'class' => "am-form")) }}<div class="am-form-group">{{ Form::label('name', 'TagName') }}{{ Form::text('name', Input::old('name')) }}</div><p><button type="submit" class="am-btn am-btn-success"><span class="am-icon-pencil"></span> Modify</button></p>{{ Form::close() }}</div> </div> @stop創(chuàng)建標(biāo)簽控制器:
$ php artisan generate:controller TagsController修改TagsController.php:
public function __construct() {$this->beforeFilter('auth', array('only' => array('create', 'store', 'edit', 'update', 'destroy')));$this->beforeFilter('csrf', array('only' => array('store', 'update', 'destroy'))); }public function edit($id) {return View::make('tags.edit')->with('tag', Tag::find($id)); }public function update($id) {$rules = array('name' => array('required', 'regex:/^\w+$/'),);$validator = Validator::make(Input::only('name'), $rules);if ($validator->passes()) {Tag::find($id)->update(Input::only('name'));return Redirect::back()->with('message', array('type' => 'success', 'content' => 'Modify tag successfully'));} else {return Redirect::back()->withInput()->withErrors($validator);} }把這個(gè)控制器加到routes.php中:
Route::resource('tag', 'TagController');現(xiàn)在就能修改標(biāo)簽了:
4.刪除標(biāo)簽
修改TagsController.php:
public function destroy($id) {$tag = Tag::find($id);$tag->count = 0;$tag->save();foreach ($tag->articles as $article) {$tag->articles()->detach($article->id);}return Redirect::back(); }我這里刪除標(biāo)簽只是把它的文章數(shù)置為0,然后清除與相關(guān)文章的關(guān)聯(lián),你可以自己試下刪除一個(gè)標(biāo)簽,再看看文章的標(biāo)簽是否去除了。
5.關(guān)聯(lián)標(biāo)簽
當(dāng)我們點(diǎn)擊首頁文章、標(biāo)簽欄和顯示文章內(nèi)容的標(biāo)簽的時(shí)候應(yīng)該跳轉(zhuǎn)到顯示相應(yīng)標(biāo)簽下所有文章的頁面:
我們對(duì)上述地方加上超鏈接地址:
<a href="{{ URL::to('tag/' . $tag->id . '/articles') }}">{{ $tag->name }}</a>創(chuàng)建指定標(biāo)簽的文章列表視圖:
$ php artisan generate:view articles.specificTag修改views/articles/specificTag.blade.php:
@extends('_layouts.default')@section('main') <div class="am-g am-g-fixed"><div class="am-u-sm-12"><br/><blockquote>Tag: <span class="am-badge am-badge-success am-radius">{{{ $tag->name }}}</span></blockquote>@foreach ($articles as $article)<article class="blog-main"><h3 class="am-article-title blog-title"><a href="{{ URL::route('article.show', $article->id) }}">{{{ $article->title }}}</a></h3><h4 class="am-article-meta blog-meta">by <a href="{{ URL::to('user/' . $article->user->id . '/articles') }}">{{{ $article->user->nickname }}}</a> posted on {{ $article->created_at->format('Y/m/d H:i') }} under @foreach ($article->tags as $tag)<a href="{{ URL::to('tag/' . $tag->id . '/articles') }}" style="color: #fff;" class="am-badge am-badge-success am-radius">{{ $tag->name }}</a>@endforeach</h4><div class="am-g"><div class="am-u-sm-12">@if ($article->summary)<p>{{ $article->summary }}</p>@endif<hr class="am-article-divider"/></div></div></article>@endforeach{{ $articles->links() }}</div> </div> @stop在TagController.php增加:
public function articles($id) {$tag = Tag::find($id);$articles = $tag->articles()->orderBy('created_at', 'desc')->paginate(10);return View::make('articles.specificTag')->with('tag', $tag)->with('articles', $articles); }在routes.php的Route::resource('tag', 'TagController');的上方增加:
Route::get('tag/{id}/articles', 'TagController@articles');現(xiàn)在當(dāng)我們點(diǎn)擊頁面上的標(biāo)簽時(shí),就會(huì)顯示該標(biāo)簽下的所有文章了:
6.顯示所有標(biāo)簽
我們還需要一個(gè)顯示所有標(biāo)簽的頁面,先創(chuàng)建視圖:
$ php artisan generate:view tags.list修改views/tags/list.blade.php:
@extends('_layouts.default')@section('main') <div class="am-g am-g-fixed"><div class="am-u-sm-12"><h1>All Tags</h1><hr/>@foreach ($tags as $tag)<a href="{{ URL::to('tag/' . $tag->id . '/articles') }}" class="am-badge am-radius {{ array('', 'am-badge-primary', 'am-badge-secondary', 'am-badge-success', 'am-badge-warning', 'am-badge-danger')[rand(0, 5)] }}">{{{ $tag->name }}} {{ $tag->count }}</a>@endforeach<br/><br/></div> </div> @stop現(xiàn)在點(diǎn)擊首頁的Tags鏈接時(shí)就會(huì)跳轉(zhuǎn)到顯示所有標(biāo)簽的頁面了:
7.小結(jié)
本節(jié)教程就到此結(jié)束了,這個(gè)博客系統(tǒng)想要實(shí)現(xiàn)的功能也基本完成了,下節(jié)開始將講解優(yōu)化、單元測試、部署和擴(kuò)展開發(fā)等內(nèi)容,你可以繼續(xù)完善,例如在管理文章和標(biāo)簽的時(shí)候提供一個(gè)搜索功能,給它們都加上分頁,在首頁加上一個(gè)搜索文章的功能,給文章加上評(píng)論功能等等,在評(píng)論功能方面現(xiàn)在有很多第三方評(píng)論插件,可以快速幫你實(shí)現(xiàn)。
最后的代碼下載:
$ git clone https://github.com/shiyanlou/laravel-blog-5.gitlaravel學(xué)習(xí)網(wǎng)站相關(guān)資料:
- Laravel官網(wǎng)
- 中文文檔1、中文文檔2
- 實(shí)驗(yàn)樓論壇
- Laravel中文網(wǎng)問答社區(qū)
- PHPHub中文社區(qū)
- API文檔
- laravel.io
- LARACASTS
總結(jié)
以上是生活随笔為你收集整理的Laravel大型项目系列教程(五)之文章和标签管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: laravel大型项目系列教程(四)之显
- 下一篇: 送ta一朵独一无二的玫瑰花