iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 8
1
Software Development

30天開發與部署 Laravel 專案系列 第 8

Laravel Routing

路由(Routing),顧名思義提供路由與轉送資訊的檔案。

首先看看專案中router資料夾可發現有四個檔案,但個人主要會用到 web.php跟 api.php這兩個。
前者設定在 http://localhost:8000 後添加的任意網址,後者是前後端分離時主要會使用的。

基本用法

在 Laravel中route寫法架構如下

Route::http_method($uri, $callback);

  • http_method:支援get,post,put,delete等。
  • url則是字串為主。
  • callback closure/function稍後說明。

導向指定的view

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

localhost網址後什麼都不加會導向這個'welcome'(welcome.blade.php)的頁面

回傳純文字response

我們可以簡單地新增一個get方法產生一個hello world的介面

Route::get('/hello-world', function () {
    return 'hello world!';
});

http://127.0.0.1:8000/hello-world

https://ithelp.ithome.com.tw/upload/images/20200922/201252636cRsmd5Wj0.png

get後方也可以帶上參數

Route::get('/hello-world/{name}', function ($name) {
    return 'hello world! ' . $name;
});

http://127.0.0.1:8000/hello-world/kidd

https://ithelp.ithome.com.tw/upload/images/20200922/20125263dlPDYe1J6X.png

可指定controller與method

Route::get('task', 'TaskApi@index');
Route::get('task/{id}', 'TaskApi@show');
Route::post('task', 'TaskApi@store');
Route::put('task/{id}', 'TaskApi@update');
Route::delete('task/{id}', 'TaskApi@destroy');

可直接做固定資料CRUD

Route::get('/users', function () {
    echo User::all();
});

Route::get('/users/{id}/delete', function($id) {
    if (User::find($id)){
        User::destroy($id);
        echo "delete success";
    }else{
        echo "deltete fail";
    }
});

可用正規表達式限制路由

Route::get('/{name}',function(){
    return 'Hello World';
})->where('name','[0-9]+')

查看當下所有的router狀態

php artisan route:list

透過這個指令,可以快速查看目前專案所有的URI、Action、Middleware等狀態。

https://ithelp.ithome.com.tw/upload/images/20200922/20125263tEHTxr95yS.png


更多用法

1.Auth

如果建立project時是用 laravel new xxxxxx —auth 會產生 Auth::routes()
但其實它跟 Route::apiResource 一樣隱含了以下作法

Auth::routes()

//表示以下作法
// 驗證相關
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
// 註冊相關
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');
// 重設密碼相關
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

2.middleware在路由的寫法

//寫法1
Route::get('profile', 'UserController@show')->middleware('auth');

//寫法2
Route::middleware(['middleware的名稱'])->group(function () {
    //要遵守這些限制的router寫在closure內
});

3.Route Model Binding

//原寫法
Route::put('task/{id}', 'TaskApi@update');

//使用Route Model Binding後寫法
Route::put('task/{tasks}', 'TaskApi@update')

先修改model,再設定RouteServiceProvider的綁定,才能使用Route Model Binding

4. RESTful API的路由

可透過 php artisan make:model model名稱 -rmc 這個指令,
透過快速建立一套RESTful API所需要的檔案,但router得自行設定。

//Route::apiResource('model名稱', '對應的controller');
Route::apiResource('userWater', 'UserWaterController');

參考資料
https://laravel.com/docs/7.x/routing
https://learnku.com/docs/laravel/7.x/routing/7458
https://jigsawye.com/2015/07/23/use-route-model-binding-in-laravel/
https://henry255164.gitbooks.io/laravel_reviews/content/chapter12.html
https://laravel.tw/docs/5.2/controllers


上一篇
版控工具:git
下一篇
Laravel Migrations
系列文
30天開發與部署 Laravel 專案30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言