laravel การใช้งาน Controllers, Resource Controllers

29 เม.ย. 2020 , 5,580 Views   , หมวดหมู่ Laravel PHP ทุกหมวดหมู่ โค๊ดดิ้ง   , ป้ายกำกับ:, ,


การสร้าง Resource Controllers

ระบบจะกำหนดค่า Routing และสร้าง Controllers แบบ CRUD ให้อัตโนมัติ เริ่มจากสร้าง Resource Controllers ด้วยคำสั่งด้านล่าง

หากต้องการเชื่อมกับ Model ด้วย ให้ใช้คำสั่งด้านล่าง

ระบบจะสร้างไฟล์ app/Http/Controllers/DepartmentController.php ขึ้นมา

 

จากนั้นให้เราไปกำหนด Route ในไฟลฟ์ routes/web.php

หรือสามารถกำหนดได้หลายๆ Resource Controllers

Actions Handled By Resource Controller

Verb URI Action Route Name
GET /departments index departments.index
GET /departments/create create departments.create
POST /departments store departments.store
GET /departments/{department} show departments.show
GET /departments/{department}/edit edit departments.edit
PUT/PATCH /departments/{department} update departments.update
DELETE /departments/{department} destroy departments.destroy

กำหนดให้ใช้เฉพาะบาง Action

ดูเพิ่มเติม https://laravel.com/docs/master/controllers#basic-controllers


ป้ายกำกับ:, ,