วิธีการเรียกใช้ routing ใน Laravel

 


ไฟล์ Routing ใน laravel จะอยู่ในพาส routes ซึ่งจะมีไฟล์ 4 ไฟล์ คือ 

  1. web.php เป็นไฟล์แรกที่ถูกเรียกและประมวลผล ดังนั้นเวลาเขียน route งานเรา จะเขียนลงในไฟล์นี้
  2. api.php
  3. chanel.php
  4. console.php
วิธีการเช็ด Routing ใช้คำสั่ง php artisan route:list ผลลัพธ์จะแสดงตาราง Routing ออกมา

ส่วนหัวจะมีการเรียก Routing โดยเรียกใช้คลาส Route ในไฟล์ชื่อ web.php

<?php
    use Illuminate\Support\Facades\Route;


Routing แบบพื้นฐาน

เวลาใช้งานจะมีรูปแบบการเรียกใช้งานอยู่ ได้แก่ 
  1. Route::get เป็นการกำหนด routing แบบพื้นฐาน ตัวอย่างรูปแบบการเรียกใช้แบบส่งข้อความ
            Route::get('welcome', function(){
                    return 'Welcome';
            }
        ถ้ามีการเรียกใช้ Controller จะมีรูปแบบ ดังนี้

            use App\Http\Controllers\UserController;
            Route::get('/user', [UserController:::class,'index']); 

            หรือ

            Route::get('/user', App\Http\Controllers\UserController::class);

            การส่งค่าผ่านเมธอด Route รูปแบบ
      
            Route::get('/test/{id}', function($id){

                    return 'test'.$id;
            }
            Route::get('/test/{id}/{comment}, function($id,$comment){

                    return 'test'.$id,'comment',$comment;

            }

Route:post
Route:put
Route:patch
Route:options
Route:delete
Route:macth
Route:any
Route:redirect
Route:permanentRedirect
Route:view
Route:pattern

การทำ Routing มีตัวแปร หรือ Parameters

การทำ Routing แบบกลุ่ม



การทำ Routing โดยใช้ชื่อแทน

Route:middleware
Route:controller
Route:domain
Route:prefix
Route:name
Route:bind

Route:fallback
Route:current
Route:currentRouteName
Route:currentRouteAction


    ตัวอย่างการกำหนด Routing 
<?php
    use Illuminate\Support\Facades\Route;

    Route:get('user', [UserController::class,'index']) กำหนดหน้าแรกให้เรียกใช้คลาส User โดยเรียกใช้ เมธอด indxe














ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

อะไรคือ NPU (Neural Processing Unit) มีความสำคัญอย่างไร แนวคิดมาจากไหน

TomCat สำหรับติดตั้ง แก้ไข คอนฟิก ใช้งาน JSP

Micro SaaS "ขนาดพอดีคำ" สร้างธุรกิจเริ่มต้นอย่างง่ายด่าย จากไอเดียสู่สร้างรายได้ โดยใช้ AI Agent ตอบสนองความต้องการซอฟต์แวร์ที่เล็ก ง่าย และทรงพลัง