這篇文章主要給你們介紹了關於利用laravel搭建一個迷你博客的相關資料,文中將一步步的實現步驟經過示例代碼介紹的很是詳細,對你們具備必定的參考學習價值,須要的朋友們下面跟着來一塊兒學習學習吧。php
本文主要給你們介紹的是關於利用laravel搭建一個迷你博客的相關內容,分享出來供你們參考學習,下面話很少說了,來一塊兒看看詳細的介紹:html
1、設計與思路node
在開始寫第一行代碼以前,必定要儘可能從頭至尾將咱們要作的產品設計好,避免寫完又改,多寫沒必要要的代碼。mysql
2、建立路由laravel
完成這個博客大概須要如下幾條路由:sql
| 路由 | 功能 | | -------- | ---------------- | | 文章列表頁面路由 | 返回文章列表頁面 | | 新增文章頁面路由 | 返回新增文章頁面 | | 文章保存功能路由 | 將文章保存到數據庫 | | 查看文章頁面路由 | 返回文章詳情頁面 | | 編輯文章頁面路由 | 返回編輯文章頁面 | | 編輯文章功能路由 | 將文章取出更新後從新保存到數據庫 | | 刪除文章功能路由 | 將文章從數據庫刪除 |數據庫
能夠看到幾乎所有是對文章的數據操做路由,針對這種狀況,Laravel 提供了很是方便的辦法:RESTful 資源控制器和路由。app
打開routes.php加入以下代碼:post
Route::resource('articles', 'ArticlesController');
只須要上面這樣一行代碼,就至關於建立了以下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');
3、建立控制器
利用 artisan 建立一個文章控制器:
php artisan make:controller ArticlesController
4、建立基礎視圖
resources/views/layouts/art.blade.php
見模板index.html
5、新建文章表單
@extends('layouts.art') @section('content') <form class="form-horizontal" method="post" action="{{route('blog.store')}}"> {{ csrf_field() }} <p class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">標題</label> <p class="col-sm-8"> <input type="title" class="form-control" id="title" name="title"> </p> </p> <p class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">內容</label> <p class="col-sm-8"> <textarea class="form-control" rows="5" id="content" name="content"></textarea> </p> </p> <p class="form-group"> <p class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">建立</button> </p> </p> </form> @endsection
6、文章存儲
此時若是你填寫新建文章表單點擊提交也會跳到一個空白頁面,一樣的道理,由於咱們後續的控制器代碼還沒寫。
要實現文章存儲,首先要配置數據庫,建立數據表,建立模型,而後再完成存儲邏輯代碼。
一、配置數據庫
修改.env文件
二、建立數據表
利用 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'); }
咱們建立了一張 articles 表,包含遞增的 id 字段,字符串title字段,長文本content字段,和時間戳。
執行數據庫遷移:
php artisan migrate
登陸mysql,查看數據表。
三、建立模型
利用 artisan 命令建立模型:
php artisan make:model Article
打開模型文件,輸入如下代碼:
app/Article.php
namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Model { //對應的表 protected $table = 'articles'; //經過model能夠寫入的字段 protected $fillable = [ 'title', 'content', ]; }
四、存儲邏輯代碼
打開 ArticlesController.php 控制器,找到 store() 方法。
app/Http/Controllers/ArticlesController.php
public function store(Request $request) { //數據驗證 錯誤處理 $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'); }
驗證錯誤顯示
@if (count($errors) > 0) <p class="alert alert-danger"> <ul> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </p> @endif
7、文章列表展現
完成了添加文章功能後,就能夠實現咱們的文章列表展現頁了。
打開 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) <p class="panel panel-default"> <p 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> </p> </p> @endforeach {!! $articles->render() !!} @endsection
8、編輯文章表單
編輯文章表單其實和以前建立的新建文章表單很相似,只是須要額外將現有的數據讀取出來填在表單上。
首先咱們在文章列表頁的每一個文章上添加一個編輯按鈕:
視圖:
@extends('layouts.art') @section('content') <form class="form-horizontal" method="post" action="{{route('blog.update',$article->id)}}"> {{ csrf_field() }} {{ method_field('PATCH') }} <p class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">標題</label> <p class="col-sm-10"> <input type="title" class="form-control" id="title" name="title" value="{{ $article->title }}"> </p> </p> <p class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">內容</label> <p class="col-sm-10"> <textarea class="form-control" rows="5" id="content" name="content"> {{ $article->content }}</textarea> </p> </p> <p class="form-group"> <p class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">修改</button> </p> </p> </form> @endsection
注意這段代碼中的 {{ method_field('PATCH') }}
,這是跨站方法僞造,HTML 表單沒有支持 PUT、PATCH 或 DELETE 動做。因此在從 HTML 表單中調用被定義的 PUT、PATCH 或 DELETE 路由時,你將須要在表單中增長隱藏的 _method 字段來僞造該方法,詳情參考 官方文檔。
控制器
//展現修改模板 public function edit($id) { $article = Article::findOrFail($id); return view('art.edit',['article'=>$article]); } //執行修改 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'); }
9、刪除文章
刪除按鈕
<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(); }
10、結語
本次實驗經過一個很簡單的迷你博客對 Laravel RESTful 資源控制器和路由,視圖,orm進行了強化練習。
以上就是laravel搭建博客實戰的教程實例的詳細內容關注個人知乎專欄