iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 9
2
Modern Web

讓PHP再次偉大,使用Laravel系列 第 9

綜合應用(1):Laravel RESTFul API

前面幾天我們大概導覽了一下整個Laravel 的結構、以及分別M-model、V-view、C-Controller,並且也講到Database的部分還有Route,基本上我們可以算是學會Laravel 的一半了,剩下的就是真的是遇到需求再說,例如event、middleware 還是auth之類的,如果有需要在自行加入即可。

所以在這裡我想要以一個簡單的Laravel 部落格的簡單應用,部落格不外乎就是新增文章、刪除文章、看文章以及修改文章,暫時我們先不用處理登入登出的問題,這個我覺得可以以後再談談

在正式進入環境之前我們仍然要過一下概念的部分,在網頁的世界中,尤其是後端的角度來看,URL(網址)的組成分為兩個

  1. domain name
  2. path或者檔名之類的
    詳細的說明可以參考維基百科:https://zh.wikipedia.org/wiki/统一资源定位符

其實網頁上我們的任何一個動作的對象都可以被視為一種資源
以樓上維基百科的網址為例,https://zh.wikipedia.org是domain name,也就是網頁的家,而wiki/统一资源定位符就是一種資源,翻成白話來說就是當你打上這串網址,就是告訴瀏覽器我要找一個在https://zh.wikipedia.org 底下有一個叫做wiki的地方,裡面有一個统一资源定位符的資源,請把它拿給我

瀏覽器也是一種軟體,當他拿到统一资源定位符這個資源的時候,發現他是一個HTML文檔,就使用他的css引擎、讀取HTML的工具以及運行JS的東西去讀取這個資源給你看

所以這樣你有比較理解我所講的是什麼嗎?我們一切在網路上所做的就是資源,而對於資源來說最常做的就是CRUD,也就是

  • C-創建新資源
  • R-讀取資源
  • U-更新資源
  • D-刪除資源

於是RESTful 的概念因應而生,他建議了URL的形式以及溝通方法、架構,
分別就是Get、Post、Put/Patch以及Delete

如果想更了解RESTFul API的話請到這裡來:https://zh.wikipedia.org/wiki/%E8%A1%A8%E7%8E%B0%E5%B1%82%E7%8A%B6%E6%80%81%E8%BD%AC%E6%8D%A2

話不多說,Laravel 也對RESTFul 有一定程度上的支援,首先,讓我們先使用一個指令

$ php artisan make:controller ArticleController --resource

請記得加--resource

然後你看到ArticleController 在Controller這個資料夾裡面,讓我們來看看它

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

前幾天我們有對Controller做了一段簡單的敘述,這裡我就不多說,你看到有很多的方法,其實他們分別就是對應好關於RESTFul API的模式...

動詞 路徑 行為(對應到Controller的方法) 路由名稱(可以放在route()的參數,之後會詳解)
GET /article index article.index
GET /article/create create article.create
POST /article store article.store
GET /article/{article} show article.show
GET /article/{article}/edit edit article.edit
PUT/PATCH /article/{article} update article.update
DELETE /article/{article} destroy article.destroy

然後我們該怎麼用Route去對應這些function呢?難道要一行一行打嗎?類似這樣

Route::get('articles', 'ArticleController@index');
...(略)
Route::delete('article','ArticleController@destroy')

其實你可以不用這麼累,Laravel都幫你想到了!

接下來請看仔細了,一行解決:

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

打完收工,明天我還是會從網站一開始(Laravel new)開始約有2~3天從頭到尾簡單實作,敬請期待!

如果你想要更深入研究的話可以參考這裡:

  1. https://laravel.tw/docs/5.2/controllers#restful-resource-controllers
  2. https://laravel.com/docs/6.x/eloquent-resources#generating-resources

上一篇
Laravel View
下一篇
綜合應用(2) 使用 Laravel 做一個簡單的部落格:複習-上
系列文
讓PHP再次偉大,使用Laravel30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言