日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Laravel大型项目系列教程(三)之发表文章

發布時間:2024/4/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Laravel大型项目系列教程(三)之发表文章 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Laravel大型項目系列教程(三)之發表文章


一、前言

上一節教程中完成了用戶管理,這節教程將大概完成發表Markdown格式文章并展示的功能。


二、Let's go

1.數據庫遷移

文章模塊中我們需要建立articles、tags以及article_tag表,每篇文章會有一到多個標簽,每個標簽會有一到多篇文章,創建遷移文件:

$ php artisan migrate:make create_articles_table --create=articles $ php artisan migrate:make create_tags_table --create=tags $ php artisan migrate:make create_article_tag_table --create=article_tag

修改*_create_articles_table.php:

Schema::create('articles', function(Blueprint $table) {$table->increments('id');$table->string('title');$table->string('summary')->nullable();$table->text('content');$table->text('resolved_content');$table->integer('user_id');$table->softDeletes();$table->timestamps(); });

修改*_create_tags_table.php:

Schema::create('tags', function(Blueprint $table) {$table->increments('id');$table->string('name')->unique();$table->integer('count')->default(0);$table->softDeletes();$table->timestamps(); });

修改*_create_article_tag_table.php:

Schema::create('article_tag', function(Blueprint $table) {$table->increments('id');$table->integer('article_id');$table->integer('tag_id'); });

執行遷移:

$ php artisan migrate

2.創建Article和Tag模型

創建Article和Tag模型:

$ php artisan generate:model article $ php artisan generate:model tag

先在User.php中增加:

public function articles() {return $this->hasMany('Article'); }

一個用戶會有多篇文章。

修改Article.php:

use Illuminate\Database\Eloquent\SoftDeletingTrait;class Article extends \Eloquent {use SoftDeletingTrait;protected $fillable = ['title', 'content'];public function tags(){return $this->belongsToMany('Tag');}public function user(){return $this->belongsTo('User');} }

一篇文章會有多個標簽并屬于一個用戶。

修改Tag.php:

use Illuminate\Database\Eloquent\SoftDeletingTrait;class Tag extends \Eloquent {use SoftDeletingTrait;protected $fillable = ['name'];public function articles(){return $this->belongsToMany('Article');} }

一個標簽會有多篇文章。

上面用到了軟刪除,belongsToMany用于多對多關聯。

3.發表文章視圖

首先在導航條nav.blade.php中添加一個發表文章的選項:

<li><a href="{{ URL::to('article/create') }}"><span class="am-icon-edit"></span> Publish Article</a></li>

這時候登錄會發現多了一個選項:

下面創建視圖:

$ php artisan generate:view articles.create

修改articles/create.blade.php:

@extends('_layouts.default')@section('main') <div class="am-g am-g-fixed"><div class="am-u-sm-12"><h1>Publish Article</h1><hr/>@if ($errors->has())<div class="am-alert am-alert-danger" data-am-alert><p>{{ $errors->first() }}</p></div>@endif{{ Form::open(array('url' => 'article', 'class' => 'am-form')) }}<div class="am-form-group"><label for="title">Title</label><input id="title" name="title" type="text" value="{{ Input::old('title') }}"/></div><div class="am-form-group"><label for="content">Content</label><textarea id="content" name="content" rows="20">{{ Input::old('content') }}</textarea><p class="am-form-help"><button id="preview" type="button" class="am-btn am-btn-xs am-btn-primary"><span class="am-icon-eye"></span> Preview</button></p></div><div class="am-form-group"><label for="tags">Tags</label><input id="tags" name="tags" type="text" value="{{ Input::old('tags') }}"/><p class="am-form-help">Separate multiple tags with a comma ","</p></div><p><button type="submit" class="am-btn am-btn-success"><span class="am-icon-send"></span> Publish</button></p>{{ Form::close() }}</div> </div><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-closeclass="am-close">×</span></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').val()}, function(data, status) {$('.am-popup-bd').html(data);});$('#preview-popup').modal();});}); </script> @stop

開始上一節中我們發現routes.php中的代碼已經很零亂,那是因為我們把業務邏輯寫在了這個文件中,對于文章模塊我們把業務邏輯寫在控制器中,首先創建一個文章控制器:

$ php artisan generate:controller ArticleController

我們會發現在app\controllers下多了一個ArticleController.php文件,它是一個資源控制器,在routes.php中增加:

Route::resource('article', 'ArticleController');

對應路由如下:

現在在ArticleController.php中增加過濾器并修改create方法:

public function __construct() {$this->beforeFilter('auth', array('only' => array('create', 'store', 'edit', 'update', 'destroy'))); }public function create() {return View::make('articles.create'); }

這時在登錄后點擊Publish Article選項后,會出現發表文章的頁面:

4.文章預覽

這里我們將使用Markdown格式來編寫文章,同時需要提供一個預覽功能,先需要安裝Markdown解析插件,在composer.json的require中增加:

"maxhoffmann/parsedown-laravel": "dev-master"

然后composer update安裝,在config/app.php中增加:

'providers' => array(...'MaxHoffmann\Parsedown\ParsedownServiceProvider' ),'aliases' => array(...'Markdown' => 'MaxHoffmann\Parsedown\ParsedownFacade', ),

安裝完后,在ArticleController.php中增加:

public function preview() {return Markdown::parse(Input::get('content')); }

在routes.php中增加:

Route::post('article/preview', array('before' => 'auth', 'uses' => 'ArticleController@preview'));

切記要添加在Route::resource('article', 'ArticleController');前面。

現在來試試我們的預覽功能吧,點擊Preview按鈕:

出現預覽界面:

5.添加文章

下面就要向數據庫添加文章了,在ArticleController.php中修改:

public function store() {$rules = ['title' => 'required|max:100','content' => 'required','tags' => array('required', 'regex:/^\w+$|^(\w+,)+\w+$/'),];$validator = Validator::make(Input::all(), $rules);if ($validator->passes()) {$article = Article::create(Input::only('title', 'content'));$article->user_id = Auth::id();$resolved_content = Markdown::parse(Input::get('content'));$article->resolved_content = $resolved_content;$tags = 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, $start + 3, $length);} else if (str_contains($resolved_content, '</h')) {$start = strpos($resolved_content, '<h');$length = strpos($resolved_content, '</h') - $start - 4;$article->summary = substr($resolved_content, $start + 4, $length);}$article->save();foreach ($tags as $tagName) {$tag = Tag::whereName($tagName)->first();if (!$tag) {$tag = Tag::create(array('name' => $tagName));}$tag->count++;$article->tags()->save($tag);}return Redirect::route('article.show', $article->id);} else {return Redirect::route('article.create')->withInput()->withErrors($validator);} }public function show($id) {return View::make('articles.show')->with('article', Article::find($id)); }

上面代碼實現了保存文章和顯示文章的業務邏輯,保存文章時驗證tags用了regex正則表達式來驗證標簽是否用,號分隔,有沒有發現Article模型中有一個resolved_content字段,這個字段來保存解析后的內容,這樣只需要在保存的時候解析一次,顯示文章頁面就顯示resolved_content字段的內容,不需要再解析一次,這是空間換時間的做法,看個人喜好了,如果想要更好的體驗,可以只在前臺頁面解析,保存的時候把前臺解析的內容保存到resolved_content字段,前臺解析Markdown有一個很好的工具StackEdit。

現在就差個顯示文章的視圖了,創建:

$ php artisan generate:view articles.show

修改articles/show.blade.php:

@extends('_layouts.default')@section('main') <article class="am-article"><div class="am-g am-g-fixed"><div class="am-u-sm-12"><br/><div class="am-article-hd"><h1 class="am-article-title">{{{ $article->title }}}</h1><p class="am-article-meta">Author: <a style="cursor: pointer;">{{{ $article->user->nickname }}}</a> Datetime: {{ $article->updated_at }}</p></div><div class="am-article-bd"><blockquote>Tags:@foreach ($article->tags as $tag)<a class="am-badge am-badge-success am-radius">{{ $tag->name }}</a>@endforeach</blockquote> </p><p>{{ $article->resolved_content }}</p></div><br/></div></div> </article> @stop

完成之后看看效果吧,先編輯文章:

發布后跳轉到顯示文章頁面:

6.小結

這節教程使用了控制器,完成了發布文章并展示的功能,但還是有很多瑕疵,在代碼方面,現在你可以重構下User和驗證登錄的路由,把它們都變成控制器,在用戶體驗上你可以把發布文章編輯內容時由服務器端解析改成客戶端解析,推薦StackEdit,下一節教程將完成網站首頁和用戶主頁展示文章列表和標簽,并且讓用戶能夠刪除和修改文章。


本文詳細出處http://www.shiyanlou.com/courses/123

總結

以上是生活随笔為你收集整理的Laravel大型项目系列教程(三)之发表文章的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。