Laravel大型項(xiàng)目系列教程四_第1頁(yè)
Laravel大型項(xiàng)目系列教程四_第2頁(yè)
Laravel大型項(xiàng)目系列教程四_第3頁(yè)
Laravel大型項(xiàng)目系列教程四_第4頁(yè)
Laravel大型項(xiàng)目系列教程四_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Laravel大型項(xiàng)目系列教程(四)一、前言上節(jié)教程中實(shí)現(xiàn)了發(fā)布文章的功能,本節(jié)教程中將大概實(shí)現(xiàn)在首頁(yè)和用戶(hù)主頁(yè)分頁(yè)顯示文章和標(biāo)簽列表、用戶(hù)能夠修改刪除文章。二、Let's go1.首頁(yè)顯示文章和標(biāo)簽列表我們需要在首頁(yè)顯示文章和標(biāo)簽列表,修改views/index.blade.php:extends('_layouts.default')section('main')<div class="am-g am-g-fixed"> <div class="am-u-md-8"> foreach (

2、$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">

3、 by <a href="#"> $article->user->nickname </a> posted on $article->created_at->format('Y/m/d H:i') under foreach ($article->tags as $tag) <a href="#" style="color: #fff;" class="am-badge am-badge-success am-radius"> $ta

4、g->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 &l

5、t;/div> <div class="am-u-md-4 blog-sidebar"> <br/> <div class="am-panel-group"> <section class="am-panel am-panel-default"> <div class="am-panel-hd"><span class="am-icon-tags"></span> Tags</div> <

6、ul class="am-list"> for ($i = 0, $len = count($tags); $i < $len; $i+) <li> <a href="#"> $tags$i->name if ($i = 0) <span class="am-fr am-badge am-badge-danger am-round"> $tags$i->count </span> elseif ($i = 1) <span class="am-fr

7、 am-badge am-badge-warning am-round"> $tags$i->count </span> elseif ($i = 2) <span class="am-fr am-badge am-badge-success am-round"> $tags$i->count </span> else <span class="am-fr am-badge am-round"> $tags$i->count </span> endif <

8、/a> </li> endfor </ul> </section> </div> </div></div>stop在custom.css中增加:media only screen and (min-width: 641px) .blog-sidebar font-size: 1.4rem; .blog-main padding: 20px 0;.blog-title margin: 10px 0 20px 0;.blog-meta font-size: 14px; margin: 10px 0 20px 0; col

9、or: #222;.blog-meta a color: #27ae60;修改routes.php:Route:get('/', function() $articles = Article:with('user', 'tags')->orderBy('created_at', 'desc')->paginate(10); $tags = Tag:where('count', '>', '0')->orderBy('count'

10、, 'desc')->orderBy('updated_at', 'desc')->take(10)->get(); return View:make('index')->with('articles', $articles)->with('tags', $tags););上面Article:with()使用了預(yù)加載,可以減少查詢(xún)次數(shù)。發(fā)布幾篇文章然后訪(fǎng)問(wèn)首頁(yè):2.實(shí)現(xiàn)用戶(hù)主頁(yè)我們?cè)诎l(fā)表文章后應(yīng)該能在用戶(hù)主頁(yè)看到文章列表,并能對(duì)文章進(jìn)行操作,先在導(dǎo)航欄nav.blade

11、.php的else上面添加一個(gè)按鈕My Articles:<div class="am-topbar-right"> <a href=" URL:to('user/'. Auth:id() . '/articles') " class="am-btn am-btn-primary am-topbar-btn am-btn-sm topbar-link-btn"><span class="am-icon-list"></span> My

12、Articles</a></div>修改home.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&

13、gt; <th>Title</th> <th>Tags</th> if ($user->id = Auth:id() <th>Managment</th> endif </tr> </thead> <tbody> foreach ($articles as $article) <tr> <td><a href=" URL:route('article.show', $article->id) "> $ar

14、ticle->title </a></td> <td> foreach ($article->tags as $tag) <span class="am-badge am-badge-success am-radius"> $tag->name </span> endforeach </td> if ($user->id = Auth:id() <td> <a href=" URL:to('article/'. $article->

15、;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' => 'd

16、isplay: 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> endif </tr> endforeach </tbody> &l

17、t;/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 c

18、lass="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').tex

19、t('Sure you want to delete it?'); $('#my-confirm').modal( relatedTarget: this, onConfirm: function(options) $(this.relatedTarget).parent().submit(); , onCancel: function() ); ); );</script>stop先添加一個(gè)UserController:$ php artisan generate:controller UserController在UserController.p

20、hp中增加:public function articles(User $user) return View:make('home')->with('user', $user)->with('articles', Article:with('tags')->where('user_id', '=', $user->id)->orderBy('created_at', 'desc')->get();在routes.php中增加:Rou

21、te:get('user/user/articles', 'UserControllerarticles');并修改原來(lái)的Route:get('home'):Route:get('home', array('before' => 'auth', function() return View:make('home')->with('user', Auth:user()->with('articles', Article:with(

22、9;tags')->where('user_id', '=', Auth:id()->orderBy('created_at', 'desc')->get(););現(xiàn)在當(dāng)用戶(hù)登錄或點(diǎn)擊My Articles按鈕后會(huì)跳轉(zhuǎn)到用戶(hù)主頁(yè)顯示文章列表,并且點(diǎn)擊標(biāo)題時(shí)能跳轉(zhuǎn)到顯示文章內(nèi)容頁(yè)面:用戶(hù)主頁(yè)完成了,另外當(dāng)在首頁(yè)和文章內(nèi)容頁(yè)面點(diǎn)擊作者時(shí)也能跳轉(zhuǎn)到相應(yīng)用戶(hù)的主頁(yè),但是如果不是本用戶(hù)是沒(méi)有操作權(quán)限的,給views/index.blade.php中的作者增加鏈接地址:<a href=" URL:to

23、('user/' . $article->user->id . '/articles') "> $article->user->nickname </a>給articles/show.blade.php中的作者增加鏈接地址:<a href=" URL:to('user/' . $article->user->id . '/articles') " style="cursor: pointer;"> $article-

24、>user->nickname </a>現(xiàn)在點(diǎn)擊這兩個(gè)超鏈接的時(shí)候就能跳轉(zhuǎn)到相應(yīng)用戶(hù)的主頁(yè)了,但是沒(méi)有操作權(quán)限:3.首頁(yè)分頁(yè)顯示文章當(dāng)文章很多時(shí),我們就要分頁(yè)顯示了,Laravel已經(jīng)為我們實(shí)現(xiàn)好了分頁(yè)邏輯,但它默認(rèn)的是Bootstrap的樣式,由于我們使用AmazeUI,所以需要自定義表示器。先在app目錄創(chuàng)建一個(gè)名為Blog的文件夾,這個(gè)文件夾中主要放置我們自己寫(xiě)的擴(kuò)展類(lèi),在其中新建一個(gè)名為PaginationPresenter.php的文件,修改:class PaginationPresenter extends IlluminatePaginationPrese

25、nter public function getActivePageWrapper($text) return '<li class="am-active"><a href="">'.$text.'</a></li>' public function getDisabledTextWrapper($text) return '<li class="am-disabled"><a href="">'

26、.$text.'</a></li>' public function getPageLinkWrapper($url, $page, $rel = null) return '<li><a href="'.$url.'">'.$page.'</a></li>' 完成之后這個(gè)類(lèi)還不能被找到,需要在composer.josn中的autoload classmap中增加"app/Blog",然后執(zhí)行:$ composer du

27、mp-autoload這樣這個(gè)類(lèi)就能被找到了,現(xiàn)在創(chuàng)建分頁(yè)鏈接的視圖:$ php artisan generate:view pagination修改pagination.blade.php:<ul class="am-pagination am-pagination-centered"> with(new PaginationPresenter($paginator)->render() </ul>完成后修改app/config/view.php中的pagination的值為pagination,在routes.php中Route:get(&

28、#39;/')內(nèi)paginate()的參數(shù)就是指定每頁(yè)顯示的數(shù)量,由于我文章比較少,暫時(shí)把它設(shè)為2,最后在views/index.blade.php中文章顯示之后添加 $articles->links() ,現(xiàn)在訪(fǎng)問(wèn)首頁(yè)就會(huì)看到如下分頁(yè)鏈接了:4.修改文章的視圖這步要實(shí)現(xiàn)在用戶(hù)主頁(yè)能夠修改文章了,自己的文章只能自己或者管理員修改,在A(yíng)rticleController.php中增加一個(gè)過(guò)濾器:public function canOperation($route, $request) if (!(Auth:user()->is_admin or Auth:id() = Art

29、icle:find(Route:input('article')->user_id) return Redirect:to('/'); 上面的Route:input('article')可以獲得路由參數(shù),這里就是文章的id值,然后在構(gòu)造函數(shù)中添加使用過(guò)濾器,再添加一個(gè)csrf過(guò)濾器:$this->beforeFilter('csrf', array('only' => array('store', 'update', 'destroy');$this

30、->beforeFilter('canOperation', array('only' => array('edit', 'update', 'destroy');創(chuàng)建修改文章的視圖:$ php artisan generate:view articles.edit修改articles/edit.blade.php:extends('_layouts.default')section('main')<div class="am-g am-g-fixed&q

31、uot;> <div class="am-u-sm-12"> <h1>Edit Article</h1> <hr/> if ($errors->has() <div class="am-alert am-alert-danger" data-am-alert> <p> $errors->first() </p> </div> endif Form:model($article, array('url' => URL:ro

32、ute('article.update', $article->id), 'method' => 'PUT', 'class' => "am-form") <div class="am-form-group"> Form:label('title', 'Title') Form:text('title', Input:old('title') </div> <div class=&

33、quot;am-form-group"> Form:label('content', 'Content') Form:textarea('content', Input:old('content'), array('rows' => '20') <p class="am-form-help"> <button id="preview" type="button" class="am-btn

34、am-btn-xs am-btn-primary"><span class="am-icon-eye"></span> Preview</button> </p> </div> <div class="am-form-group"> Form:label('tags', 'Tags') Form:text('tags', Input:old('tags') <p class="am-for

35、m-help">Separate multiple tags with a comma ","</p> </div> <p><button type="submit" class="am-btn am-btn-success"> <span class="am-icon-pencil"></span> Modify</button> </p> Form:close() </div></d

36、iv><div class="am-popup" id="preview-popup"> <div class="am-popup-inner"> <div class="am-popup-hd"> <h4 class="am-popup-title"></h4> <span data-am-modal-close class="am-close">&times;</span>

37、</div> <div class="am-popup-bd"> </div> </div></div><script> $(function() $('#preview').on('click', function() $('.am-popup-title').text($('#title').val(); $.post('preview', 'content': $('#content')

38、.val(), function(data, status) $('.am-popup-bd').html(data); ); $('#preview-popup').modal(); ); );</script>stop在routes.php中增加:Route:post('article/id/preview', array('before' => 'auth', 'uses' => 'ArticleControllerpreview');這是為了修改文章時(shí)

39、能夠預(yù)覽文章。在A(yíng)rticleController.php中修改:public function edit($id) $article = Article:with('tags')->find($id); $tags = '' for ($i = 0, $len = count($article->tags); $i < $len; $i+) $tags .= $article->tags$i->name . ($i = $len - 1 ? '' : ','); $article->tags

40、= $tags; return View:make('articles.edit')->with('article', $article);現(xiàn)在在用戶(hù)主頁(yè)點(diǎn)擊修改文章時(shí)會(huì)跳轉(zhuǎn)到修改頁(yè)面:5.修改文章在A(yíng)rticleController.php添加修改文章的業(yè)務(wù)邏輯:public function update($id) $rules = 'title' => 'required|max:100', 'content' => 'required', 'tags' =&

41、gt; array('required', 'regex:/w+$|(w+,)+w+$/'), ; $validator = Validator:make(Input:all(), $rules); if ($validator->passes() $article = Article:with('tags')->find($id); $article->update(Input:only('title', 'content'); $resolved_content = Markdown:pars

42、e(Input:get('content'); $article->resolved_content = $resolved_content; $tags = array_unique(explode(',', Input:get('tags'); if (str_contains($resolved_content, '<p>') $start = strpos($resolved_content, '<p>'); $length = strpos($resolved_content, '</p>') - $start - 3; $article->summary = substr($resolved_content, $star

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論