iT邦幫忙

2025 iThome 鐵人賽

DAY 7
0
Modern Web

Laravel 是甚麼系列 第 7

繼續安裝

  • 分享至 

  • xImage
  •  

裝8.0版本
composer -v

https://ithelp.ithome.com.tw/upload/images/20250808/20119035TPDHYjgViE.png

composer create-project --prefer-dist laravel/laravel blog "8.*"

https://ithelp.ithome.com.tw/upload/images/20250808/20119035Q4ZtYO0kGw.png

cd blog
https://ithelp.ithome.com.tw/upload/images/20250808/20119035KFNxW3H0Rq.png

php artisan serve

https://ithelp.ithome.com.tw/upload/images/20250808/20119035p5A1oEt0a2.png

http://127.0.0.1:8000/

https://ithelp.ithome.com.tw/upload/images/20250808/20119035FK6fXcnelj.png

php artisan route:list

https://ithelp.ithome.com.tw/upload/images/20250808/20119035C54SZCorX3.png

php artisan make:controller ProductController –resource

https://ithelp.ithome.com.tw/upload/images/20250808/20119035QTtQPNbRMp.png

加入protected $namespace = 'App\Http\Controllers';

https://ithelp.ithome.com.tw/upload/images/20250808/20119035oN0p4j0CVS.png

加入Route::post('/', 'ProductController@create');
Route::resource('products', 'ProductController');
用php artisan route:list

https://ithelp.ithome.com.tw/upload/images/20250808/20119035XJifZnZOvp.png
路由


MVC就是view/controller/model

打開檔案看

看app裡面的Controllers裡面進階

重點是app

https://ithelp.ithome.com.tw/upload/images/20250808/20119035R9KEDwEiok.png

看 web.php
https://ithelp.ithome.com.tw/upload/images/20250808/20119035h4Tu98DJhv.png

設定一些路由

https://ithelp.ithome.com.tw/upload/images/20250808/20119035jcBAQb2RYj.png

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
   return view('welcome');
});

Route::post('/','ProductController@create');
Route::resource('products','ProductController');
重點是controller
php artisan make:controller ProductController

https://ithelp.ithome.com.tw/upload/images/20250808/20119035eptGgRPz3X.png

會自動新增一個ProductController

用指令自動產生CRUD先把原來的刪掉

https://ithelp.ithome.com.tw/upload/images/20250808/20119035U6fPWbFQrZ.png
php artisan make:controller ProductController --resource
裡面的程式碼:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}

POSTMAN的安裝
https://www.postman.com/downloads/
https://ithelp.ithome.com.tw/upload/images/20250808/201190356ssh4xPiFA.png

下載後執行,登入Google信箱
https://ithelp.ithome.com.tw/upload/images/20250808/20119035te2uxniKbi.png
參考
https://ithelp.ithome.com.tw/articles/10352893
裝好後:

https://ithelp.ithome.com.tw/upload/images/20250808/20119035zRH8BUJpLC.png
大家明天見~8/9-8/10是一年一度在台科大的COSCUP喔~
我也會從南部北上~所以明天會先初版~


上一篇
就是安裝
下一篇
寫code用postman測試
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言