Laravel大型项目系列教程(五)之文章和标签管理

Laravel大型项目系列教程(五)之文章和标签管理

一、前言

本节教程将大概完成文章和标签管理以及标签关联。

二、Let’s go

1.文章管理

首先创建管理后台文章列表视图:

$ 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中增加一个Articles的超链接:

<li class="{
    { (isset($page) and ($page == 'articles')) ? 'am-active' : '' }}"><a href="{
      { URL::to('admin/articles') }}">Articles</a></li>

 

创建一个管理员控制器,在app/controllers下创建一个名为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');

 

管理文章可以重用上节教程写的业务逻辑,修改下ArticleController.php,把destroy()中最后的Redirect::to('home')改成Redirect::back(), 再修改一下home.blade.php,加一个是否是管理员的判断,这样当点击作者跳转到用户主页时,除了作者自己管理员也能操作文章:

@if ($user->id == Auth::id() or (Auth::check() and Auth::user()->is_admin))

 

现在点击导航栏的Articles,就会出现所有的文章:

Laravel大型项目系列教程(五)之文章和标签管理

这样管理员就可以操作所有的文章了。

我们还可以再修改下admin/users/list.blade.php,当点击用户列表的昵称时也会跳转到用户主页:

<a href="{
   { URL::to('user/' . $user->id . '/articles') }}">{
    {
    { $user->nickname }}}</a>

 

现在访问用户列表页面:

Laravel大型项目系列教程(五)之文章和标签管理

2.显示标签列表

创建一个标签列表视图:

$ 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选项:

<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')); }

 

现在点击导航栏上方的Tags超链接:

Laravel大型项目系列教程(五)之文章和标签管理

3.修改标签

创建修改标签的视图:

$ 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
复制代码

 

创建标签控制器:

$ 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); } }
复制代码

 

把这个控制器加到routes.php中:

Route::resource('tag', 'TagController');

 

现在就能修改标签了:

Laravel大型项目系列教程(五)之文章和标签管理

Laravel大型项目系列教程(五)之文章和标签管理

4.删除标签

修改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(); }
复制代码

 

我这里删除标签只是把它的文章数置为0,然后清除与相关文章的关联,你可以自己试下删除一个标签,再看看文章的标签是否去除了。

5.关联标签

当我们点击首页文章、标签栏和显示文章内容的标签的时候应该跳转到显示相应标签下所有文章的页面:

Laravel大型项目系列教程(五)之文章和标签管理

Laravel大型项目系列教程(五)之文章和标签管理

我们对上述地方加上超链接地址:

<a href="{
   { URL::to('tag/' . $tag->id . '/articles') }}">{
    { $tag->name }}</a>

 

创建指定标签的文章列表视图:

$ 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');

 

现在当我们点击页面上的标签时,就会显示该标签下的所有文章了:

Laravel大型项目系列教程(五)之文章和标签管理

6.显示所有标签

我们还需要一个显示所有标签的页面,先创建视图:

$ 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
复制代码

 

现在点击首页的Tags链接时就会跳转到显示所有标签的页面了:

Laravel大型项目系列教程(五)之文章和标签管理

Laravel大型项目系列教程(五)之文章和标签管理

7.小结

本节教程就到此结束了,这个博客系统想要实现的功能也基本完成了,下节开始将讲解优化、单元测试、部署和扩展开发等内容,你可以继续完善,例如在管理文章和标签的时候提供一个搜索功能,给它们都加上分页,在首页加上一个搜索文章的功能,给文章加上评论功能等等,在评论功能方面现在有很多第三方评论插件,可以快速帮你实现。

最后的代码下载:

$ git clone https://github.com/shiyanlou/laravel-blog-5.git

 

本文详细出自实验楼

转载请注明出处

https://www.cnblogs.com/shiyanlou/p/4284531.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/112546.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • cuda编程基础(编程软件有哪些)

    CUDA(ComputeUnifiedDeviceArchitecture),是显卡厂商NVIDIA推出的运算平台。CUDA™是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。

    2022年4月18日
  • MATLAB分段函数及应用实例

    MATLAB分段函数及应用实例简单实例:用matlab画分段函数        当x&lt;0时 y=5*sin(x);        当x&gt;=0且x&lt;=5时y=x^2;        当x&gt;5时y=(8-x)^2+16.x=-5:0.001:10;y=5*sin(x).*(x&lt;0)+x.^2.*(x&gt;=0&amp;x&lt;=5)+((8-x).^2+16).*(x&gt;5);p…

    2022年6月7日
  • JWT — JWT原理解析及实际使用[通俗易懂]

    JWT — JWT原理解析及实际使用[通俗易懂]一、JWT1、JWT介绍JWT(jsonwebtoken)是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准。JWT的声明一般被用来在身份提供者和服务提供者间传递被认证的用户身份信息,以便于从资源服务器获取资源。比如用户登录。在传统的用户登录认证中,因为http是无状态的,所以都是采用session方式。用户登录成功,服务端会保存一个session,服务端会返回给客户端一个sessionId,客户端会把sessionId保存在cookie中,每次请求都会携带这个sessionId。

    2022年10月17日
  • Axis2创建WebService实例(转载)

    Axis2创建WebService实例(转载)博文地址:http://clq9761.iteye.com/blog/976029

    2022年7月21日
  • CSS的四种基本选择器和四种高级选择器[通俗易懂]

    CSS的四种基本选择器和四种高级选择器[通俗易懂]CSS选择器:就是指定CSS要作用的标签,那个标签的名称就是选择器。意为:选择哪个容器。CSS的选择器分为两大类:基本选择题和扩展选择器。基本选择器:标签选择器:针对一类标签 ID选择器:针对某一个特定的标签使用 类选择器:针对你想要的所有标签使用 通用选择器(通配符):针对所有的标签都适用(不建议使用)下面来分别讲一讲。1、标签选择器:选择器的名字代表html页面上的标签…

    2022年7月27日
  • matlab的插值方法[通俗易懂]

    matlab的插值方法[通俗易懂]1.Lagrange插值插值是在已知数据之间寻找估计值的过程。在信号处理和图像处理中,插值极其常用。类型很多:比如多项式插值,一、二、三维插值,样条插值等。方法介绍:对给定的n个插值点x1,x2,⋯,xnx1,x2,⋯,xn{x_1},{x_2},\cdots,{x_n}及对应的函数值y1,y2,⋯,yny1,y2,⋯,yn{y_1},{y_2},\cdots,{y_n}…

    2022年5月4日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号