小練習 – 共識 Controller function

練習時數: 請您拿出你的倒數計時器計時一小時


目的:今天要來練習跟您達成共識。

雖然設計模式沒有對或錯,只能多看看,並且在適應的場合時做,今天想跟大家分享達成Controller的共識,controller 裡面的function,laravel 預設的有一些既有的方法定義。

新建Laravel 專案

laravel new petstore

進入 petstore 資料夾

cd petstore

建立:Animal Model,以下指令除了建立 Model 以外還會新建 Migration 、Controller 這就是今天的主角,都是第一個字的縮寫,那麼 r 呢?Resource 等等會看到。

php artisan make:model Animal -rmc

應該會看到如下圖輸出結果。

使用 編輯器 開啟這個資料夾 petstore,打開 AnimalController 應該在 App\Controller 資料夾中

App\Controller\AnimalController.php

檔案裡面會看到以下內容,複製全部的程式碼。

<?php

namespace App\Http\Controllers;

use App\Models\Animal;
use Illuminate\Http\Request;

class AnimalController 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  \App\Models\Animal  $animal
     * @return \Illuminate\Http\Response
     */
    public function show(Animal $animal)
    {
        //
    }

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

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

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

這些就是定義好的function 也就是剛剛 r 這個參數加進來的東西,這些就是要用來操作 Animal 這個資源。

我們先達成一個共識這是給網站使用的,不是拿來寫API專用的 Controller ,所以會以建立網站系統的原則來說明。

方法名稱定義備註
index列表那一頁顯示的畫面
create建立 Animal 操作的那個畫面
store使用者在建立Animal資源的儲存進入資料庫的動作
show顯示一單動物資源的畫面
edit更新單一動物資源的畫面
update更新但一動物資源進入資料庫的動作
destroy移除一個動物資源的動作

請在每個方法中加入以下程式碼,以 index 為例。

public function index()
{
    return '我是可以看動物列表的「畫面」';
}

以此類推,都加上return 並回傳文字,文字自己打一下,用自己懂的的方式打出來。

最後一個步驟,我們要設定路由,讓別人(使用者),可以依照網址連進這個 Controller ,打開以下檔案。

routes/web.php

在檔案中加入以下程式碼

Route::resource('animals', \App\Http\Controllers\AnimalController::class);

以上程式碼也等於在檔案中下入以下程式碼,則一選擇即可。

use App\Http\Controllers\AnimalController;

Route::resource('animals', AnimalController::class);

接著我們開啟暫時用的網頁伺服器輸入以下指令。

php artisan serve

應該會出現如下圖的輸出

打開瀏覽器,網址輸入 http://127.0.0.1:8000/ ,畫面如下圖所示。

網址上輸入

http://127.0.0.1:8000/animals

會看到回傳的

我是可以看動物列表的「畫面」

這就是使用 GET 的方式請求 http://127.0.0.1:8000/animals ,可以看到動物資源的列表畫面。

恭喜您!有空再來看看,讓自己更厲害,也歡迎跟我分享。

Victor
Victor

哈囉!

文章: 230

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *