利用laravel搭建一個(gè)迷你博客實(shí)戰(zhàn)教程
本文主要給大家介紹的是關(guān)于利用laravel搭建一個(gè)迷你博客的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹:
一、設(shè)計(jì)與思路
在開(kāi)始寫第一行代碼之前,一定要盡量從頭到尾將我們要做的產(chǎn)品設(shè)計(jì)好,避免寫完又改,多寫不必要的代碼。
- 需求分析:我們的迷你博客應(yīng)該至少包含:新增/編輯/查看/刪除文章,以及文章列表展示功能。
- 數(shù)據(jù)庫(kù)分析:基于這個(gè)功能,我們只需要一張 Articles 數(shù)據(jù)表來(lái)存放文章即可。
- 頁(yè)面結(jié)構(gòu)分析:應(yīng)該使用模板繼承建立一張基礎(chǔ)模板包含:頭部/文章列表/底部信息
二、創(chuàng)建路由
完成這個(gè)博客大概需要以下幾條路由:
| 路由 | 功能 | | -------- | ---------------- | | 文章列表頁(yè)面路由 | 返回文章列表頁(yè)面 | | 新增文章頁(yè)面路由 | 返回新增文章頁(yè)面 | | 文章保存功能路由 | 將文章保存到數(shù)據(jù)庫(kù) | | 查看文章頁(yè)面路由 | 返回文章詳情頁(yè)面 | | 編輯文章頁(yè)面路由 | 返回編輯文章頁(yè)面 | | 編輯文章功能路由 | 將文章取出更新后重新保存到數(shù)據(jù)庫(kù) | | 刪除文章功能路由 | 將文章從數(shù)據(jù)庫(kù)刪除 |
可以看到幾乎全部是對(duì)文章的數(shù)據(jù)操作路由,針對(duì)這種情況,Laravel 提供了非常方便的辦法:RESTful 資源控制器和路由。
打開(kāi)routes.php加入如下代碼:
Route::resource('articles', 'ArticlesController');
只需要上面這樣一行代碼,就相當(dāng)于創(chuàng)建了如下7條路由,且都是命名路由,我們可以使用類似route('articles.show') 這樣的用法。
Route::get('/articles', 'ArticlesController@index')->name('articles.index'); Route::get('/articles/{id}', 'ArticlesController@show')->name('articles.show'); Route::get('/articles/create', 'ArticlesController@create')->name('articles.create'); Route::post('/articles', 'ArticlesController@store')->name('articles.store'); Route::get('/articles/{id}/edit', 'ArticlesController@edit')->name('articles.edit'); Route::patch('/articles/{id}', 'ArticlesController@update')->name('articles.update'); Route::delete('/articles/{id}', 'ArticlesController@destroy')->name('articles.destroy');
三、創(chuàng)建控制器
利用 artisan 創(chuàng)建一個(gè)文章控制器:
php artisan make:controller ArticlesController
四、創(chuàng)建基礎(chǔ)視圖
resources/views/layouts/art.blade.php
見(jiàn)模板index.html
五、新建文章表單
@extends('layouts.art') @section('content') <form class="form-horizontal" method="post" action="{{route('blog.store')}}"> {{ csrf_field() }} <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">標(biāo)題</label> <div class="col-sm-8"> <input type="title" class="form-control" id="title" name="title"> </div> </div> <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">內(nèi)容</label> <div class="col-sm-8"> <textarea class="form-control" rows="5" id="content" name="content"></textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">創(chuàng)建</button> </div> </div> </form> @endsection
六、文章存儲(chǔ)
此時(shí)如果你填寫新建文章表單點(diǎn)擊提交也會(huì)跳到一個(gè)空白頁(yè)面,同樣的道理,因?yàn)槲覀兒罄m(xù)的控制器代碼還沒(méi)寫。
要實(shí)現(xiàn)文章存儲(chǔ),首先要配置數(shù)據(jù)庫(kù),創(chuàng)建數(shù)據(jù)表,創(chuàng)建模型,然后再完成存儲(chǔ)邏輯代碼。
1、配置數(shù)據(jù)庫(kù)
修改.env文件
2、創(chuàng)建數(shù)據(jù)表
利用 artisan 命令生成遷移:
php artisan make:migration create_articles_talbe --create=articles
修改遷移文件
public function up() { Schema::create('articles', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->longText('content'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('articles'); }
我們創(chuàng)建了一張 articles 表,包含遞增的 id 字段,字符串title字段,長(zhǎng)文本content字段,和時(shí)間戳。
執(zhí)行數(shù)據(jù)庫(kù)遷移:
php artisan migrate
登錄mysql,查看數(shù)據(jù)表。
3、創(chuàng)建模型
利用 artisan 命令創(chuàng)建模型:
php artisan make:model Article
打開(kāi)模型文件,輸入以下代碼:
app/Article.php
namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Model { //對(duì)應(yīng)的表 protected $table = 'articles'; //通過(guò)model可以寫入的字段 protected $fillable = [ 'title', 'content', ]; }
4、存儲(chǔ)邏輯代碼
打開(kāi) ArticlesController.php 控制器,找到 store() 方法。
app/Http/Controllers/ArticlesController.php
public function store(Request $request) { //數(shù)據(jù)驗(yàn)證 錯(cuò)誤處理 $this->validate($request,[ 'title'=>'required|max:50', 'content'=>'required|max:500', ]); // 1 orm方式寫入 $article = Article::create([ 'title'=>$request->title, 'content'=>$request->content, ]); //2 或者 /* $article = new Article(); $article->title =$request->title; $article->content = $request->content; $article->save();*/ //3 db方式寫入 //insert()方法返回值為true 和 false //$res = DB::table('articles')->insert(['title'=>$request->title,'content'=>$request->content]); return redirect()->route('blog.index'); }
驗(yàn)證錯(cuò)誤顯示
@if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif
七、文章列表展示
完成了添加文章功能后,就可以實(shí)現(xiàn)我們的文章列表展示頁(yè)了。
打開(kāi) ArticlesController.php 找到 index()
方法,添加代碼如下:
app/Http/Controllers/ArticlesController.php
use App\Article; public function index() { $articles = Article::orderBy('created_at','asc')->get(); return view('articles.index', ['articles'=>$articles]); }
視圖index.blade.php
@extends('layouts.art') @section('content') <a class="btn btn-primary" href="{{route('blog.create')}}" rel="external nofollow" >添加文章</a> @foreach($articles as $article) <div class="panel panel-default"> <div class="panel-body"> {{$article->title}} <a href="{{route('blog.show',$article->id)}}" rel="external nofollow" class="btn btn-info">閱讀</a> <a href="{{route('blog.edit', $article->id)}}" rel="external nofollow" class="btn btn-info">修改</a> <form action="{{ route('blog.destroy', $article->id) }}" method="post" style="display: inline-block;"> {{ csrf_field() }} {{ method_field('DELETE') }} <button type="submit" class="btn btn-danger">刪除</button> </form> </div> </div> @endforeach {!! $articles->render() !!} @endsection
八、編輯文章表單
編輯文章表單其實(shí)和之前創(chuàng)建的新建文章表單很類似,只是需要額外將現(xiàn)有的數(shù)據(jù)讀取出來(lái)填在表單上。
首先我們?cè)谖恼铝斜眄?yè)的每個(gè)文章上添加一個(gè)編輯按鈕:
視圖:
@extends('layouts.art') @section('content') <form class="form-horizontal" method="post" action="{{route('blog.update',$article->id)}}"> {{ csrf_field() }} {{ method_field('PATCH') }} <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">標(biāo)題</label> <div class="col-sm-10"> <input type="title" class="form-control" id="title" name="title" value="{{ $article->title }}"> </div> </div> <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">內(nèi)容</label> <div class="col-sm-10"> <textarea class="form-control" rows="5" id="content" name="content"> {{ $article->content }}</textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">修改</button> </div> </div> </form> @endsection
注意這段代碼中的 {{ method_field('PATCH') }}
,這是跨站方法偽造,HTML 表單沒(méi)有支持 PUT、PATCH 或 DELETE 動(dòng)作。所以在從 HTML 表單中調(diào)用被定義的 PUT、PATCH 或 DELETE 路由時(shí),你將需要在表單中增加隱藏的 _method 字段來(lái)偽造該方法,詳情參考 官方文檔。
控制器
//展示修改模板 public function edit($id) { $article = Article::findOrFail($id); return view('art.edit',['article'=>$article]); } //執(zhí)行修改 public function update(Request $request, $id) { $this->validate($request, [ 'title' => 'required|max:50', 'content'=>'required|max:500', ]); $article = Article::findOrFail($id); $article->update([ 'title' => $request->title, 'content' => $request->content, ]); return redirect()->route('blog.index'); }
九、刪除文章
刪除按鈕
<form action="{{ route('blog.destroy', $article->id) }}" method="post" style="display: inline-block;"> {{ csrf_field() }} {{ method_field('DELETE') }} <button type="submit" class="btn btn-danger">刪除</button> </form>
控制器:
public function destroy($id) { $article = Article::findOrFail($id); $article->delete(); return back(); }
十、結(jié)語(yǔ)
本次實(shí)驗(yàn)通過(guò)一個(gè)很簡(jiǎn)單的迷你博客對(duì) Laravel RESTful 資源控制器和路由,視圖,orm進(jìn)行了強(qiáng)化練習(xí)。
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
PHP加Nginx實(shí)現(xiàn)動(dòng)態(tài)裁剪圖片方案
這篇文章主要介紹了PHP加Nginx實(shí)現(xiàn)動(dòng)態(tài)裁剪圖片的方案,使用Imagick組件實(shí)現(xiàn),需要的朋友可以參考下2014-03-03php的api數(shù)據(jù)接口書寫實(shí)例(推薦)
下面小編就為大家?guī)?lái)一篇php的api數(shù)據(jù)接口書寫實(shí)例(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09destoon公司主頁(yè)模板風(fēng)格的添加方法
這篇文章主要介紹了destoon公司主頁(yè)模板風(fēng)格的添加方法,需要的朋友可以參考下2014-06-06PHP簡(jiǎn)單實(shí)現(xiàn)HTTP和HTTPS跨域共享session解決辦法
這篇文章主要介紹了PHP簡(jiǎn)單實(shí)現(xiàn)HTTP和HTTPS跨域共享session解決辦法,本文講解的方法相對(duì)簡(jiǎn)單,需要的朋友可以參考下2015-05-05laravel框架模型和數(shù)據(jù)庫(kù)基礎(chǔ)操作實(shí)例詳解
這篇文章主要介紹了laravel框架模型和數(shù)據(jù)庫(kù)基礎(chǔ)操作,結(jié)合實(shí)例形式詳細(xì)分析了laravel框架模型的定義及數(shù)據(jù)庫(kù)的增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2020-01-01Yii2 RESTful中api的使用及開(kāi)發(fā)實(shí)例詳解
這篇文章主要介紹了Yii2 RESTful中api的使用及開(kāi)發(fā)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-07-07