本节我们将会完成博客后台管理系统的文章发布功能:我们将会继续完善 posts
表迁移、引入一些额外前端资源、并实现文章创建、修改和删除。
我们在十分钟创建博客应用中已经创建了posts
表迁移,现在要对其进行修改和完善。
在 Laravel 5.1 中如果需要修改数据表的列,则需要安装 Doctrine 依赖包,我们使用 Composer 安装该依赖包:
composer require "doctrine/dbal"
接下来使用 Artisan 命令创建新的迁移文件:
php artisan make:migration --table=posts restructure_posts_table
然后编辑刚刚创建的迁移文件:
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class RestructurePostsTable extends Migration { /** * Run the migrations. */ public function up() { Schema::table('posts', function (Blueprint $table) { $table->string('subtitle')->after('title'); $table->renameColumn('content', 'content_raw'); $table->text('content_html')->after('content'); $table->string('page_image')->after('content_html'); $table->string('meta_description')->after('page_image'); $table->boolean('is_draft')->after('meta_description'); $table->string('layout')->after('is_draft')->default('blog.layouts.post'); }); } /** * Reverse the migrations. */ public function down() { Schema::table('posts', function (Blueprint $table) { $table->dropColumn('layout'); $table->dropColumn('is_draft'); $table->dropColumn('meta_description'); $table->dropColumn('page_image'); $table->dropColumn('content_html'); $table->renameColumn('content_raw', 'content'); $table->dropColumn('subtitle'); }); } }
我们对表字段略作说明:
subtitle
:文章副标题content_raw
:Markdown格式文本content_html
:使用 Markdown 编辑内容但同时保存 HTML 版本page_image
:文章缩略图(封面图)meta_description
:文章备注说明is_draft
:该文章是否是草稿layout
:使用的布局迁移已经创建并编辑好了,接下来我们登录到 Homestead 虚拟机中运行该迁移:
php artisan migrate
现在数据库部分已经完成了!
接下来我们来编辑 Post
模型类和 Tag
模型类来建立两者之间的关联关系。
首先编辑 app\Tag.php
文件内容如下:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Tag extends Model { protected $fillable = [ 'tag', 'title', 'subtitle', 'page_image', 'meta_description','reverse_direction', ]; /** * 定义文章与标签之间多对多关联关系 * * @return BelongsToMany */ public function posts() { return $this->belongsToMany('App\Post', 'post_tag_pivot'); } /** * Add any tags needed from the list * * @param array $tags List of tags to check/add */ public static function addNeededTags(array $tags) { if (count($tags) === 0) { return; } $found = static::whereIn('tag', $tags)->lists('tag')->all(); foreach (array_diff($tags, $found) as $tag) { static::create([ 'tag' => $tag, 'title' => $tag, 'subtitle' => 'Subtitle for '.$tag, 'page_image' => '', 'meta_description' => '', 'reverse_direction' => false, ]); } } }
然后修改 app\Post.php
文件内容如下:
<?php namespace App; use App\Services\Markdowner; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $dates = ['published_at']; /** * The many-to-many relationship between posts and tags. * * @return BelongsToMany */ public function tags() { return $this->belongsToMany('App\Tag', 'post_tag_pivot'); } /** * Set the title attribute and automatically the slug * * @param string $value */ public function setTitleAttribute($value) { $this->attributes['title'] = $value; if (! $this->exists) { $this->setUniqueSlug($value, ''); } } /** * Recursive routine to set a unique slug * * @param string $title * @param mixed $extra */ protected function setUniqueSlug($title, $extra) { $slug = str_slug($title.'-'.$extra); if (static::whereSlug($slug)->exists()) { $this->setUniqueSlug($title, $extra + 1); return; } $this->attributes['slug'] = $slug; } /** * Set the HTML content automatically when the raw content is set * * @param string $value */ public function setContentRawAttribute($value) { $markdown = new Markdowner(); $this->attributes['content_raw'] = $value; $this->attributes['content_html'] = $markdown->toHTML($value); } /** * Sync tag relation adding new tags as needed * * @param array $tags */ public function syncTags(array $tags) { Tag::addNeededTags($tags); if (count($tags)) { $this->tags()->sync( Tag::whereIn('tag', $tags)->lists('id')->all() ); return; } $this->tags()->detach(); } }
下面为后台文章功能引入两个前端 JS 资源,我们使用 Bower 下载资源,然后使用 Gulp 将这些资源放到指定位置。
首先是 Selectize.js。Selectize.js 是一个基于 jQuery 的 UI 控件,对于标签选择和下拉列表功能非常有用。我们将使用它来处理文章标签输入。使用 Bower 下载 Seletize.js:
bower install selectize --save
接下来下载 Pickadate.js。Pickadate.js 是一个轻量级的 jQuery 日期时间选择插件,日期时间插件很多,选择使用 Pickadate.js 的原因是它在小型设备上也有很好的体验。下面我们使用 Bower 下载安装 Pickadate.js:
bower install pickadate --save
现在相应的前端资源已经下载好了,接下来我们使用 Gulp 来管理这些资源,编辑 gulpfile.js
文件内容如下:
var gulp = require('gulp'); var rename = require('gulp-rename'); var elixir = require('laravel-elixir'); /** * 拷贝文件 * * Do a 'gulp copyfiles' after bower updates */ gulp.task("copyfiles", function() { // Copy jQuery, Bootstrap, and FontAwesome gulp.src("vendor/bower_dl/jquery/dist/jquery.js") .pipe(gulp.dest("resources/assets/js/")); gulp.src("vendor/bower_dl/bootstrap/less/**") .pipe(gulp.dest("resources/assets/less/bootstrap")); gulp.src("vendor/bower_dl/bootstrap/dist/js/bootstrap.js") .pipe(gulp.dest("resources/assets/js/")); gulp.src("vendor/bower_dl/bootstrap/dist/fonts/**") .pipe(gulp.dest("public/assets/fonts")); gulp.src("vendor/bower_dl/fontawesome/less/**") .pipe(gulp.dest("resources/assets/less/fontawesome")); gulp.src("vendor/bower_dl/fontawesome/fonts/**") .pipe(gulp.dest("public/assets/fonts")); // Copy datatables var dtDir = 'vendor/bower_dl/datatables-plugins/integration/'; gulp.src("vendor/bower_dl/datatables/media/js/jquery.dataTables.js") .pipe(gulp.dest('resources/assets/js/')); gulp.src(dtDir + 'bootstrap/3/dataTables.bootstrap.css') .pipe(rename('dataTables.bootstrap.less')) .pipe(gulp.dest('resources/assets/less/others/')); gulp.src(dtDir + 'bootstrap/3/dataTables.bootstrap.js') .pipe(gulp.dest('resources/assets/js/')); // Copy selectize gulp.src("vendor/bower_dl/selectize/dist/css/**") .pipe(gulp.dest("public/assets/selectize/css")); gulp.src("vendor/bower_dl/selectize/dist/js/standalone/selectize.min.js") .pipe(gulp.dest("public/assets/selectize/")); // Copy pickadate gulp.src("vendor/bower_dl/pickadate/lib/compressed/themes/**") .pipe(gulp.dest("public/assets/pickadate/themes/")); gulp.src("vendor/bower_dl/pickadate/lib/compressed/picker.js") .pipe(gulp.dest("public/assets/pickadate/")); gulp.src("vendor/bower_dl/pickadate/lib/compressed/picker.date.js") .pipe(gulp.dest("public/assets/pickadate/")); gulp.src("vendor/bower_dl/pickadate/lib/compressed/picker.time.js") .pipe(gulp.dest("public/assets/pickadate/")); }); /** * Default gulp is to run this elixir stuff */ elixir(function(mix) { // 合并 JS mix.scripts( [ 'js/jquery.js', 'js/bootstrap.js', 'js/jquery.dataTables.js', 'js/dataTables.bootstrap.js' ], 'public/assets/js/admin.js', 'resources/assets' ); // 编译 Less mix.less('admin.less', 'public/assets/css/admin.css'); });
配置和之前基本一致,不同之处在于新增了 Selectize 和 Pickadate 配置。
下面我们运行 gulp copyfiles
命令将上述两个前端资源拷贝到 public
目录下:
正如我们在上一节处理标签时所做的一样,我们使用表单请求类来验证文件创建及更新请求。
首先,使用 Artisan 命令创建表单请求处理类,对应文件会生成在 app/Http/Requests
目录下:
php artisan make:request PostCreateRequest php artisan make:request PostUpdateRequest
编辑新创建的 PostCreateRequest.php
内容如下:
<?php namespace App\Http\Requests; use Carbon\Carbon; class PostCreateRequest extends Request { /** * Determine if the user is authorized to make this request. */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'title' => 'required', 'subtitle' => 'required', 'content' => 'required', 'publish_date' => 'required', 'publish_time' => 'required', 'layout' => 'required', ]; } /** * Return the fields and values to create a new post from */ public function postFillData() { $published_at = new Carbon( $this->publish_date.' '.$this->publish_time ); return [ 'title' => $this->title, 'subtitle' => $this->subtitle, 'page_image' => $this->page_image, 'content_raw' => $this->get('content'), 'meta_description' => $this->meta_description, 'is_draft' => (bool)$this->is_draft, 'published_at' => $published_at, 'layout' => $this->layout, ]; } }
这是一个包含 authorize()
和 rules()
方法的标准表单请求类,此外我们还添加了一个 postFillData()
方法,使用该方法可以轻松从请求中获取数据填充 Post
模型。
然后修改 PostUpdateRequest.php
内容如下:
<?php namespace App\Http\Requests; class PostUpdateRequest extends PostCreateRequest { // }
该类继承自 PostCreateRequest
,当然目前来看这连个类做的事情完全一样,我们也可以使用同一个请求类处理文章创建和修改,但是为了方便以后扩展这里我们使用两个请求类分别处理创建和更新请求。
接下来我们创建一个公用的、可以从 PostController
中调用的任务类,我们将其称之为 PostFormFields
。该任务会在我们想要获取文章所有字段填充文章表单时被执行。
首先使用 Artisan 命令创建任务类模板:
php artisan make:job PostFormFields
创建的任务类位于 app/Jobs
目录下。编辑新生成的 PostFormFields.php
文件内容如下:
<?php namespace App\Jobs; use App\Post; use App\Tag; use Carbon\Carbon; use Illuminate\Contracts\Bus\SelfHandling; class PostFormFields extends Job implements SelfHandling { /** * The id (if any) of the Post row * * @var integer */ protected $id; /** * List of fields and default value for each field * * @var array */ protected $fieldList = [ 'title' => '', 'subtitle' => '', 'page_image' => '', 'content' => '', 'meta_description' => '', 'is_draft' => "0", 'publish_date' => '', 'publish_time' => '', 'layout' => 'blog.layouts.post', 'tags' => [], ]; /** * Create a new command instance. * * @param integer $id */ public function __construct($id = null) { $this->id = $id; } /** * Execute the command. * * @return array of fieldnames => values */ public function handle() { $fields = $this->fieldList; if ($this->id) { $fields = $this->fieldsFromModel($this->id, $fields); } else { $when = Carbon::now()->addHour(); $fields['publish_date'] = $when->format('M-j-Y'); $fields['publish_time'] = $when->format('g:i A'); } foreach ($fields as $fieldName => $fieldValue) { $fields[$fieldName] = old($fieldName, $fieldValue); } return array_merge( $fields, ['allTags' => Tag::lists('tag')->all()] ); } /** * Return the field values from the model * * @param integer $id * @param array $fields * @return array */ protected function fieldsFromModel($id, array $fields) { $post = Post::findOrFail($id); $fieldNames = array_keys(array_except($fields, ['tags'])); $fields = ['id' => $id]; foreach ($fieldNames as $field) { $fields[$field] = $post->{$field}; } $fields['tags'] = $post->tags()->lists('tag')->all(); return $fields; } }
该任务最终返回文章字段和值的键值对数组,我们将使用其返回结果用来填充文章编辑表单。如果 Post
模型未被加载(比如创建文章时),那么就会返回默认值。如果 Post
被成功加载(文章更新),那么就会从数据库获取值。
此外,还有两个额外的字段被返回,即 tags
和 allTags
。tags
是与该 Post
模型实例关联的所有标签数组;allTags
是所有标签数组。
我们还需要两个辅助函数,因此我们编辑 app/helpers.php
文件内容添加这两个函数:
/** * Return "checked" if true */ function checked($value) { return $value ? 'checked' : ''; } /** * Return img url for headers */ function page_image($value = null) { if (empty($value)) { $value = config('blog.page_image'); } if (! starts_with($value, 'http') && $value[0] !== '/') { $value = config('blog.uploads.webpath') . '/' . $value; } return $value; }
checked()
方法用于在视图的复选框和单选框中设置 checked
属性。
page_image()
方法用于返回上传图片的完整路径。
你可能已经注意到我们将 published_at
分割成了 publish_date
和 publish_time
,下面我们在 Post
模型中添加这两个字段:
<?php // 在 Post 类的 $dates 属性后添加 $fillable 属性 protected $fillable = [ 'title', 'subtitle', 'content_raw', 'page_image', 'meta_description','layout', 'is_draft', 'published_at', ]; // 然后在 Post 模型类中添加如下几个方法 /** * Return the date portion of published_at */ public function getPublishDateAttribute($value) { return $this->published_at->format('M-j-Y'); } /** * Return the time portion of published_at */ public function getPublishTimeAttribute($value) { return $this->published_at->format('g:i A'); } /** * Alias for content_raw */ public function getContentAttribute($value) { return $this->content_raw; }
此外我们还添加了 getContentAttribute()
方法作为访问器以便返回 $this->content_raw
。现在如果我们使用 $post->content
就会执行该方法。
现在我们在 PostController
类中实现所有需要的功能。由于我们将表单验证和填充数据分散到表单请求类和 PostFormFields
类中完成,控制器的代码量将会很小:
<?php namespace App\Http\Controllers\Admin; use App\Jobs\PostFormFields; use App\Http\Requests; use App\Http\Requests\PostCreateRequest; use App\Http\Requests\PostUpdateRequest; use App\Http\Controllers\Controller; use App\Post; class PostController extends Controller { /** * Display a listing of the posts. */ public function index() { return view('admin.post.index') ->withPosts(Post::all()); } /** * Show the new post form */ public function create() { $data = $this->dispatch(new PostFormFields()); return view('admin.post.create', $data); } /** * Store a newly created Post * * @param PostCreateRequest $request */ public function store(PostCreateRequest $request) { $post = Post::create($request->postFillData()); $post->syncTags($request->get('tags', [])); return redirect() ->route('admin.post.index') ->withSuccess('New Post Successfully Created.'); } /** * Show the post edit form * * @param int $id * @return Response */ public function edit($id) { $data = $this->dispatch(new PostFormFields($id)); return view('admin.post.edit', $data); } /** * Update the Post * * @param PostUpdateRequest $request * @param int $id */ public function update(PostUpdateRequest $request, $id) { $post = Post::findOrFail($id); $post->fill($request->postFillData()); $post->save(); $post->syncTags($request->get('tags', [])); if ($request->action === 'continue') { return redirect() ->back() ->withSuccess('Post saved.'); } return redirect() ->route('admin.post.index') ->withSuccess('Post saved.'); } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $post = Post::findOrFail($id); $post->tags()->detach(); $post->delete(); return redirect() ->route('admin.post.index') ->withSuccess('Post deleted.'); } }
接下来唯一要做的就是创建相应视图了。
现在我们来创建 PostController
中用到的所有视图。
首先修改已经存在的位于 resources/views/admin/post
目录下的 index.blade.php
:
@extends('admin.layout') @section('content') <div class="container-fluid"> <div class="row page-title-row"> <div class="col-md-6"> <h3>Posts <small>» Listing</small></h3> </div> <div class="col-md-6 text-right"> <a href="/admin/post/create" class="btn btn-success btn-md"> <i class="fa fa-plus-circle"></i> New Post </a> </div> </div> <div class="row"> <div class="col-sm-12"> @include('admin.partials.errors') @include('admin.partials.success') <table id="posts-table" class="table table-striped table-bordered"> <thead> <tr> <th>Published</th> <th>Title</th> <th>Subtitle</th> <th data-sortable="false">Actions</th> </tr> </thead> <tbody> @foreach ($posts as $post) <tr> <td data-order="{{ $post->published_at->timestamp }}"> {{ $post->published_at->format('j-M-y g:ia') }} </td> <td>{{ $post->title }}</td> <td>{{ $post->subtitle }}</td> <td> <a href="/admin/post/{{ $post->id }}/edit" class="btn btn-xs btn-info"> <i class="fa fa-edit"></i> Edit </a> <a href="/blog/{{ $post->slug }}" class="btn btn-xs btn-warning"> <i class="fa fa-eye"></i> View </a> </td> </tr> @endforeach </tbody> </table> </div> </div> </div> @stop @section('scripts') <script> $(function() { $("#posts-table").DataTable({ order: [[0, "desc"]] }); }); </script> @stop
该视图很简单,就是使用文章数据填充表格然后使用 DataTables 初始化表格。
接下来,在 resources/views/admin/post
目录下新建一个 create.blade.php
:
@extends('admin.layout') @section('styles') <link href="/assets/pickadate/themes/default.css" rel="stylesheet"> <link href="/assets/pickadate/themes/default.date.css" rel="stylesheet"> <link href="/assets/pickadate/themes/default.time.css" rel="stylesheet"> <link href="/assets/selectize/css/selectize.css" rel="stylesheet"> <link href="/assets/selectize/css/selectize.bootstrap3.css" rel="stylesheet"> @stop @section('content') <div class="container-fluid"> <div class="row page-title-row"> <div class="col-md-12"> <h3>Posts <small>» Add New Post</small></h3> </div> </div> <div class="row"> <div class="col-sm-12"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">New Post Form</h3> </div> <div class="panel-body"> @include('admin.partials.errors') <form class="form-horizontal" role="form" method="POST" action="{{ route('admin.post.store') }}"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> @include('admin.post._form') <div class="col-md-8"> <div class="form-group"> <div class="col-md-10 col-md-offset-2"> <button type="submit" class="btn btn-primary btn-lg"> <i class="fa fa-disk-o"></i> Save New Post </button> </div> </div> </div> </form> </div> </div> </div> </div> </div> @stop @section('scripts') <script src="/assets/pickadate/picker.js"></script> <script src="/assets/pickadate/picker.date.js"></script> <script src="/assets/pickadate/picker.time.js"></script> <script src="/assets/selectize/selectize.min.js"></script> <script> $(function() { $("#publish_date").pickadate({ format: "mmm-d-yyyy" }); $("#publish_time").pickatime({ format: "h:i A" }); $("#tags").selectize({ create: true }); }); </script> @stop
这里我们引入了 Selectize 和 Pickadate 库。你可能还注意到了我们还引入了一个尚未创建的局部视图 admin.post._form
。下面我们就在 resources/views/admin/post
目录下创建这个视图,在该目录先新建一个 _form.blade.php
,编辑其内容如下:
<div class="row"> <div class="col-md-8"> <div class="form-group"> <label for="title" class="col-md-2 control-label"> Title </label> <div class="col-md-10"> <input type="text" class="form-control" name="title" autofocus id="title" value="{{ $title }}"> </div> </div> <div class="form-group"> <label for="subtitle" class="col-md-2 control-label"> Subtitle </label> <div class="col-md-10"> <input type="text" class="form-control" name="subtitle" id="subtitle" value="{{ $subtitle }}"> </div> </div> <div class="form-group"> <label for="page_image" class="col-md-2 control-label"> Page Image </label> <div class="col-md-10"> <div class="row"> <div class="col-md-8"> <input type="text" class="form-control" name="page_image" id="page_image" onchange="handle_image_change()" alt="Image thumbnail" value="{{ $page_image }}"> </div> <script> function handle_image_change() { $("#page-image-preview").attr("src", function () { var value = $("#page_image").val(); if ( ! value) { value = {!! json_encode(config('blog.page_image')) !!}; if (value == null) { value = ''; } } if (value.substr(0, 4) != 'http' && value.substr(0, 1) != '/') { value = {!! json_encode(config('blog.uploads.webpath')) !!} + '/' + value; } return value; }); } </script> <div class="visible-sm space-10"></div> <div class="col-md-4 text-right"> <img src="{{ page_image($page_image) }}" class="img img_responsive" id="page-image-preview" style="max-height:40px"> </div> </div> </div> </div> <div class="form-group"> <label for="content" class="col-md-2 control-label"> Content </label> <div class="col-md-10"> <textarea class="form-control" name="content" rows="14" id="content">{{ $content }}</textarea> </div> </div> </div> <div class="col-md-4"> <div class="form-group"> <label for="publish_date" class="col-md-3 control-label"> Pub Date </label> <div class="col-md-8"> <input class="form-control" name="publish_date" id="publish_date" type="text" value="{{ $publish_date }}"> </div> </div> <div class="form-group"> <label for="publish_time" class="col-md-3 control-label"> Pub Time </label> <div class="col-md-8"> <input class="form-control" name="publish_time" id="publish_time" type="text" value="{{ $publish_time }}"> </div> </div> <div class="form-group"> <div class="col-md-8 col-md-offset-3"> <div class="checkbox"> <label> <input {{ checked($is_draft) }} type="checkbox" name="is_draft"> Draft? </label> </div> </div> </div> <div class="form-group"> <label for="tags" class="col-md-3 control-label"> Tags </label> <div class="col-md-8"> <select name="tags[]" id="tags" class="form-control" multiple> @foreach ($allTags as $tag) <option @if (in_array($tag, $tags)) selected @endif value="{{ $tag }}"> {{ $tag }} </option> @endforeach </select> </div> </div> <div class="form-group"> <label for="layout" class="col-md-3 control-label"> Layout </label> <div class="col-md-8"> <input type="text" class="form-control" name="layout" id="layout" value="{{ $layout }}"> </div> </div> <div class="form-group"> <label for="meta_description" class="col-md-3 control-label"> Meta </label> <div class="col-md-8"> <textarea class="form-control" name="meta_description" id="meta_description" rows="6"> {{ $meta_description }} </textarea> </div> </div> </div> </div>
我们创建这个局部视图的目的是让 create 和 edit 视图可以共享它。
下面我们在同一目录下创建 edit.blade.php
:
@extends('admin.layout') @section('styles') <link href="/assets/pickadate/themes/default.css" rel="stylesheet"> <link href="/assets/pickadate/themes/default.date.css" rel="stylesheet"> <link href="/assets/pickadate/themes/default.time.css" rel="stylesheet"> <link href="/assets/selectize/css/selectize.css" rel="stylesheet"> <link href="/assets/selectize/css/selectize.bootstrap3.css" rel="stylesheet"> @stop @section('content') <div class="container-fluid"> <div class="row page-title-row"> <div class="col-md-12"> <h3>Posts <small>» Edit Post</small></h3> </div> </div> <div class="row"> <div class="col-sm-12"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Post Edit Form</h3> </div> <div class="panel-body"> @include('admin.partials.errors') @include('admin.partials.success') <form class="form-horizontal" role="form" method="POST" action="{{ route('admin.post.update', $id) }}"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <input type="hidden" name="_method" value="PUT"> @include('admin.post._form') <div class="col-md-8"> <div class="form-group"> <div class="col-md-10 col-md-offset-2"> <button type="submit" class="btn btn-primary btn-lg" name="action" value="continue"> <i class="fa fa-floppy-o"></i> Save - Continue </button> <button type="submit" class="btn btn-success btn-lg" name="action" value="finished"> <i class="fa fa-floppy-o"></i> Save - Finished </button> <button type="button" class="btn btn-danger btn-lg" data-toggle="modal" data-target="#modal-delete"> <i class="fa fa-times-circle"></i> Delete </button> </div> </div> </div> </form> </div> </div> </div> </div> {{-- 确认删除 --}} <div class="modal fade" id="modal-delete" tabIndex="-1"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"> × </button> <h4 class="modal-title">Please Confirm</h4> </div> <div class="modal-body"> <p class="lead"> <i class="fa fa-question-circle fa-lg"></i> Are you sure you want to delete this post? </p> </div> <div class="modal-footer"> <form method="POST" action="{{ route('admin.post.destroy', $id) }}"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <input type="hidden" name="_method" value="DELETE"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="submit" class="btn btn-danger"> <i class="fa fa-times-circle"></i> Yes </button> </form> </div> </div> </div> </div> </div> @stop @section('scripts') <script src="/assets/pickadate/picker.js"></script> <script src="/assets/pickadate/picker.date.js"></script> <script src="/assets/pickadate/picker.time.js"></script> <script src="/assets/selectize/selectize.min.js"></script> <script> $(function() { $("#publish_date").pickadate({ format: "mmm-d-yyyy" }); $("#publish_time").pickatime({ format: "h:i A" }); $("#tags").selectize({ create: true }); }); </script> @stop
至此,所有后台文章管理所需的视图都已经创建好了。
最后要做的收尾工作是移除显示文章详情路由 show
。编辑 app/Http/routes.php
如下:
// 讲如下这行 resource('admin/post', 'PostController'); // 修改成 resource('admin/post', 'PostController', ['except' => 'show']);
好了,接下来可以在后台测试文章创建、编辑、删除了。
在浏览器中访问 http://blog.app/admin/post
,点击“发布文章”(New Post)按钮,进入发布文章页面:
点击“Save New Post”按钮,发布文章成功后跳转到后台文章列表:
第一篇即为我们刚刚发布的文章,其它是之前十分中创建博客的测试数据。接下来我们可以点击“Edit”编辑文章:
在该页面可以删除文章。“Save – Continue”与“Save – Finished”区别在于前者保存后会停留在编辑页面,后台保存后跳转到文章列表页。
在文章列表页我们还可以点击“View”查看文章详情:
当然现在页面还比较丑,而且并没有对 Markdown 格式内容做处理(其实只要将 posts
表中 content_html
字段内容输出即可),下一节我们将开始优化博客前台页面。