Laravel入門與實戰

Laravel入門與實戰

《Laravel入門與實戰》全稱《Laravel入門與實戰:構建主流PHP套用開發框架》,是2018年4月電子工業出版社出版的圖書,作者是Matt Stauffer,譯者是韋瑋。

內容簡介

本書從基礎開始詳細地講解了Laravel開發框架。本書對Laravel框架進行了系統的介紹,包括Laravel的背景、Laravel開發環境的搭建、路由與控制器、Blade模板、前端組件、收集和處理用戶數據、Artisan和Tinker、資料庫和Eloquent、容器、Laravel測試等知識。通過閱讀本書,讀者可以比較全面地學習並掌握Laravel開發的相關理論知識。另外,書中涵蓋大量實例,更有利於讀者在學習過程中不斷實踐。

圖書目錄

前言 ..........................................................................................xxiii
第1 章 為什麼使用Laravel ..........................................................1
為什麼要使用框架 .................................................................................................1
“自己動手做” ..................................................................................................2
一致性和靈活性 ..............................................................................................2
Web 和PHP 框架的簡史 ........................................................................................2
Ruby on Rail ....................................................................................................2
PHP 框架的湧入 ..............................................................................................3
CodeIgniter 的優點與缺點 ..............................................................................3
Laravel 1、Laravel 2 和Laravel 3 ...................................................................3
Laravel 4 .........................................................................................................4
Laravel 5 .........................................................................................................4
Laravel 有什麼特別之處 ........................................................................................4
Laravel 的哲學 ................................................................................................4
Laravel 如何讓開發者快樂 ..............................................................................5
Laravel 社區 ....................................................................................................6
它是如何工作的 .....................................................................................................6
為什麼使用Laravel ................................................................................................9
第2 章 搭建Laravel 的開發環境 ................................................11
系統要求 ....................................................................................................... 11
Composer ...................................................................................................... 11
本地開發環境 ......................................................................................................12
Laravel Valet ..................................................................................................12
Laravel Homestead ........................................................................................13
創建一個新的Laravel 項目 .................................................................................17
使用Laravel 安裝工具來安裝Laravel ..........................................................17
通過Composer 的create-project 來安裝Laravel ...........................................17
Laravel 的目錄結構 ..............................................................................................18
資料夾 ...........................................................................................................18
檔案 ...............................................................................................................19
配置 .....................................................................................................................19
啟動和運行 ..........................................................................................................20
測試 .....................................................................................................................21
本章小結 ..............................................................................................................21
第3 章 路由和控制器 ................................................................23
路由定義 ..............................................................................................................23
路由處理 .......................................................................................................25
路由參數 .......................................................................................................26
路由名稱 .......................................................................................................27
路由組 ..................................................................................................................30
中間件 ...........................................................................................................30
路徑前綴 .......................................................................................................31
子域名路由 ...................................................................................................32
命名空間前綴 ................................................................................................33
名稱前綴 .......................................................................................................33
視圖 .....................................................................................................................33
使用視圖Composers 在每個視圖中共享變數 ...............................................35
控制器 ..................................................................................................................35
獲取用戶輸入 ................................................................................................38
將依賴注入控制器 ........................................................................................39
資源控制器 ...................................................................................................40
路由模型綁定 ......................................................................................................42
隱式路由模型綁定 ........................................................................................42
自定義路由模型綁定 .....................................................................................43
路由快取 ..............................................................................................................44
表單方法欺騙(Form Method Spoofing) .............................................................44
HTTP 動詞的介紹 .........................................................................................44
Laravel 中的HTTP 動詞 ...............................................................................45
在HTML 表單中進行HTTP 方法欺騙 .........................................................45
CSRF 保護 ...........................................................................................................45
重定向 ..................................................................................................................47
redirect()->to() ...............................................................................................48
redirect()->route() ..........................................................................................48
redirect()->back() ..........................................................................................49
其他重定向方法 ............................................................................................49
redirect()->with() ...........................................................................................49
中止請求 ..............................................................................................................51
自定義回響 ..........................................................................................................51
response()->make() ........................................................................................51
response()->json() 和->jsonp() ......................................................................52
response()->download() 和->file() ................................................................52
測試 .....................................................................................................................52
本章小結 ..............................................................................................................53
第4 章 Blade 模板 ....................................................................55
輸出數據 ..............................................................................................................56
控制結構 ..............................................................................................................56
條件語句 .......................................................................................................57
循環語句 .......................................................................................................57
or ...................................................................................................................59
模板繼承 ..............................................................................................................60
用@section/@show 和@yield 定義Section .................................................60
@parent .........................................................................................................62
@include .......................................................................................................62
@each ............................................................................................................63
視圖Composer 和服務注入 .................................................................................63
用視圖Composer 綁定數據到視圖 ...............................................................64
Blade 服務注入 .............................................................................................67
自定義Blade 指令 ...............................................................................................68
自定義Blade 指令中的參數 ..........................................................................69
示例:對多租戶應用程式(Multitenant App)使用自定義Blade 指令........70
測試 .....................................................................................................................71
本章小結 ..............................................................................................................72
第5 章 前端組件 .......................................................................73
Elixir ....................................................................................................................73
Elixir 資料夾結構 ..........................................................................................75
運行Elixir .....................................................................................................75
Elixir 提供了什麼 ..........................................................................................76
分頁 .....................................................................................................................82
分頁資料庫結果 ............................................................................................82
手動創建分頁 ................................................................................................83
訊息包 ..................................................................................................................84
錯誤包命名 ...................................................................................................85
字元串助手、多元化和本地化 ............................................................................86
字元串助手和多元化 .....................................................................................86
本地化 ...........................................................................................................87
測試 .....................................................................................................................89
用Elixir 進行測試 .........................................................................................89
測試訊息包和錯誤包 .....................................................................................90
翻譯和本地化 ................................................................................................90
本章小結 ..............................................................................................................90
第6 章 收集和處理用戶數據 ......................................................91
注入請求對象 ......................................................................................................91
$request->all() ...............................................................................................92
$request->except() 和 $request->only() .......................................................92
$request->has() 與$request->exists() ............................................................93
$request->input() ...........................................................................................93
數組輸入 .......................................................................................................94
JSON 輸入($request->json()) ......................................................................94
路由數據 ..............................................................................................................96
通過Request 實現 .........................................................................................96
通過路由參數實現 ........................................................................................96
上傳的檔案 ..........................................................................................................96
驗證 .....................................................................................................................99
在控制器中使用ValidatesRequests 的validate() 方法 ..................................99
手動驗證 ..................................................................................................... 102
顯示驗證錯誤信息 ...................................................................................... 102
表單請求 ............................................................................................................ 103
創建表單請求 .............................................................................................. 103
使用表單請求 .............................................................................................. 104
Eloquent 模型質量分配 ...................................................................................... 105
{{ 與 {!! ............................................................................................................. 106
測試 ................................................................................................................... 106
本章小結 ............................................................................................................ 107
第7 章 Artisan 和Tinker .........................................................109
Artisan 入門 ....................................................................................................... 109
Artisan 的基本命令 ............................................................................................ 110
選項 ............................................................................................................. 110
組合命令 ..................................................................................................... 111
書寫常見的Artisan 命令 .................................................................................... 113
註冊命令 ..................................................................................................... 115
示例命令 ..................................................................................................... 116
參數和選項 ................................................................................................. 117
輸入 ............................................................................................................. 118
提示 ............................................................................................................ 120
輸出 ............................................................................................................. 121
在其他代碼中調用Artisan 命令 ........................................................................ 122
Tinker ................................................................................................................. 123
測試 ................................................................................................................... 124
本章小結 ............................................................................................................ 125
第8 章 資料庫和Eloquent .......................................................127
配置 ................................................................................................................... 127
資料庫連線 ................................................................................................. 127
其他資料庫配置選項 ................................................................................... 129
遷移 ................................................................................................................... 129
定義遷移 ..................................................................................................... 129
運行遷移 ..................................................................................................... 137
填充 ................................................................................................................... 138
創建填充器 ................................................................................................. 138
模型工廠 ..................................................................................................... 139
查詢構造器 ........................................................................................................ 143
DB Facade 的基本使用................................................................................ 143
原始SQL 語句 ............................................................................................ 144
查詢構造器鏈 .............................................................................................. 145
事務 ............................................................................................................. 153
Eloquent 入門 ..................................................................................................... 154
新建和定義Eloquent 模型 .......................................................................... 156
通過Eloquent 獲取數據 .............................................................................. 157
Eloquent 的插入和更新 ............................................................................... 159
Eloquent 中的刪除 ...................................................................................... 162
作用域 ......................................................................................................... 164
自定義與訪問器、修改器和屬性轉換器的欄位互動 .................................. 167
Eloquent 集合 .............................................................................................. 171
Eloquent 序列化 .......................................................................................... 173
Eloquent 關係 .............................................................................................. 175
通過子類更新父類時間戳 ........................................................................... 186
Eloquent 事件 ..................................................................................................... 188
測試 ................................................................................................................... 189
本章小結 ............................................................................................................ 191
第9 章 用戶認證和授權 ...........................................................193
用戶模型和遷移 ................................................................................................. 194
使用auth() 全局助手和認證Facade .................................................................. 197
Auth 控制器 ....................................................................................................... 197
RegisterController ....................................................................................... 197
LoginController ........................................................................................... 199
ResetPasswordController ............................................................................. 200
ForgotPasswordController ........................................................................... 200
Auth::routes() ..................................................................................................... 200
認證腳手架(Auth Scaffold) ............................................................................. 201
“記住我” ............................................................................................................ 202
手動認證用戶 .................................................................................................... 203
認證中間件 ........................................................................................................ 204
保護 ................................................................................................................... 204
修改默認保護 .............................................................................................. 205
在不改變默認情況下使用其他保護 ............................................................ 205
添加新的保護 .............................................................................................. 205
創建自定義用戶提供器 ............................................................................... 206
為非關係型資料庫自定義用戶提供器 ........................................................ 207
認證事件 ............................................................................................................ 207
授權(ACL)和角色 ......................................................................................... 208
定義授權規則 .............................................................................................. 208
Gate facade(和注入Gate) ......................................................................... 209
Authorize 中間件 ......................................................................................... 210
控制器授權 ................................................................................................. 210
檢查用戶實例 .............................................................................................. 212
Blade 檢查 ................................................................................................... 213
插入檢查 ..................................................................................................... 213
政策 ............................................................................................................. 214
測試 ................................................................................................................... 216
本章小結 ............................................................................................................ 218
第10 章 請求和回響 ................................................................221
Laravel 請求的生命周期 .................................................................................... 221
引導應用程式 .............................................................................................. 222
服務提供者 ................................................................................................. 223
Request 對象 ...................................................................................................... 224
在Laravel 中獲取請求對象 ......................................................................... 225
獲取請求的基本信息 ................................................................................... 225
持久性 ......................................................................................................... 228
Response 對象 .................................................................................................... 228
在容器中使用和創建Response 類 .............................................................. 229
特殊的回響類型 .......................................................................................... 230
Laravel 和中間件 ............................................................................................... 233
中間件入門 ................................................................................................. 233
創建自定義中間件 ...................................................................................... 234
綁定中間件 ................................................................................................. 236
向中間件傳參 .............................................................................................. 239
測試 ................................................................................................................... 240
本章小結 ............................................................................................................ 241
第11 章 容器 ..........................................................................243
依賴注入簡介 .................................................................................................... 243
依賴注入和Laravel ............................................................................................ 245
app() 全局助手 ................................................................................................... 245
容器如何連線 .................................................................................................... 246
將類綁定到容器 ................................................................................................. 247
綁定到閉包 ................................................................................................. 247
綁定單例模式、別名和實例 ....................................................................... 248
將具體實例綁定到接口 ............................................................................... 249
語境綁定 ..................................................................................................... 250
構造器注入 ........................................................................................................ 250
方法注入 ............................................................................................................ 251
facade 與容器 ..................................................................................................... 252
facade 如何工作 .......................................................................................... 252
服務提供者 ........................................................................................................ 254
測試 ................................................................................................................... 254
本章小結 ............................................................................................................ 255
第12 章 測試 ..........................................................................257
測試基礎 ............................................................................................................ 258
命名測試 ............................................................................................................ 260
測試環境 ............................................................................................................ 261
測試特性 ............................................................................................................ 262
WithoutMiddleware ..................................................................................... 262
資料庫遷移 ................................................................................................. 262
資料庫事務 ................................................................................................. 263
應用程式測試 .................................................................................................... 263
測試用例 ..................................................................................................... 263
“訪問”路徑 ................................................................................................ 264
自定義套用測試斷言 ................................................................................... 266
JSON 和Non-visit() 套用測試斷言 ............................................................. 267
點擊和表單 ................................................................................................. 271
任務和事件 ................................................................................................. 273
認證和會話 ................................................................................................. 273
Artisan 和Seed................................................................................................... 274
mock(模擬) ..................................................................................................... 275
Mockery ...................................................................................................... 275
模擬facade .................................................................................................. 278
本章小結 ............................................................................................................ 279
第13 章 編寫API ....................................................................281
類REST JSON API 基礎 .................................................................................... 281
控制器組織和JSON 返回 .................................................................................. 282
讀取和傳送頭 .................................................................................................... 286
在Laravel 中傳送回響頭 ............................................................................ 287
在Laravel 中讀取請求頭 ............................................................................ 287
Eloquent 分頁 ..................................................................................................... 287
排序和篩選 ........................................................................................................ 289
對API 結果排序 .......................................................................................... 289
過濾API 結果 ............................................................................................. 291
數據轉換 ............................................................................................................ 292
編寫自己的轉換器 ...................................................................................... 292
嵌套和關係 ........................................................................................................ 293
使用Laravel Passport 的API 認證 ..................................................................... 295
OAuth2.0 簡介 ............................................................................................. 295
安裝Passport ............................................................................................... 296
Passport 的API ........................................................................................... 297
Passport 可用的授權類型 ............................................................................ 298
使用Passport API 和Vue 組件管理客戶端和令牌 ...................................... 305
Passport 作用域 ........................................................................................... 307
Laravel 5.2 以上版本的API 令牌認證 ............................................................... 309
測試 ................................................................................................................... 310
本章小結 ............................................................................................................ 311
第14 章 存儲和檢索 ................................................................313
本地和雲端檔案管理器 ...................................................................................... 313
配置檔案訪問 .............................................................................................. 313
使用存儲facade .......................................................................................... 314
添加額外的Flysystem 提供商 ..................................................................... 316
基本的檔案上傳和操作 ...................................................................................... 317
會話 ................................................................................................................... 318
訪問會話 ..................................................................................................... 318
會話實例的可用方法 ................................................................................... 319
快閃記憶體會話存儲 .............................................................................................. 321
高速快取器cache ............................................................................................... 321
訪問高速快取 .............................................................................................. 322
Cache 實例中可用的方法 ............................................................................ 322
cookie ................................................................................................................. 324
Laravel 中的cookie ..................................................................................... 324
訪問cookie 工具 ......................................................................................... 324
基於Laravel Scout 全文搜尋 ............................................................................. 327
安裝Scout ................................................................................................... 327
標記索引模型 .............................................................................................. 327
索引檢索 ..................................................................................................... 328
佇列和Scout ............................................................................................... 328
執行無索引操作 .......................................................................................... 328
通過代碼手動觸發索引 ............................................................................... 329
利用CLI 手動觸發索引 .............................................................................. 329
測試 ................................................................................................................... 329
檔案存儲 ..................................................................................................... 329
會話 ............................................................................................................. 331
高速快取 ..................................................................................................... 332
cookie .......................................................................................................... 332
本章小結 ............................................................................................................ 334
第15 章 郵件和通知 ................................................................335
郵件 ................................................................................................................... 335
“classic”郵件 ............................................................................................. 336
基本“mailable”郵件................................................................................. 336
郵件模版 ..................................................................................................... 339
build() 中可用的方法 .................................................................................. 339
附屬檔案和內聯圖片 .......................................................................................... 340
佇列 ............................................................................................................. 341
本地開發 ..................................................................................................... 342
通知 ................................................................................................................... 344
為通知對象定義via() 方法 ......................................................................... 346
傳送通知 ..................................................................................................... 347
排隊通知 ..................................................................................................... 348
開箱即用的通知類型 ................................................................................... 348
測試 ................................................................................................................... 351
郵件 ............................................................................................................. 351
通知 ............................................................................................................. 352
本章小結 ............................................................................................................ 352
第16 章 佇列,任務,事件,廣播及調度程式 ...........................353
佇列 ................................................................................................................... 353
為什麼使用佇列 .......................................................................................... 354
基本佇列配置 .............................................................................................. 354
列隊任務 ..................................................................................................... 354
運行佇列工作者 .......................................................................................... 358
錯誤處理 ..................................................................................................... 358
控制佇列 ..................................................................................................... 361
支持其他功能的佇列 ................................................................................... 361
事件 ................................................................................................................... 362
觸發事件 ..................................................................................................... 362
監聽事件 ..................................................................................................... 364
通過WebSocket 廣播事件及Laravel Echo ........................................................ 367
配置和設定 ................................................................................................. 368
廣播事件 ..................................................................................................... 368
接收訊息 ..................................................................................................... 371
高級廣播工具 .............................................................................................. 372
Laravel Echo(JavaScript 方面).................................................................. 376
調度程式 ............................................................................................................ 379
可用任務類型 .............................................................................................. 380
可用時間框架 .............................................................................................. 380
阻塞和重疊 ................................................................................................. 382
處理任務輸出 .............................................................................................. 382
任務鉤子 ..................................................................................................... 383
測試 ................................................................................................................... 384
本章小結 ............................................................................................................ 385
第17 章 助手和集合 ................................................................387
助手 ................................................................................................................... 387
數組 ............................................................................................................. 387
字元串 ......................................................................................................... 389
套用路徑 ..................................................................................................... 391
URL ............................................................................................................. 392
Misc(宏指令結構技術體系) ..................................................................... 394
集合 ................................................................................................................... 397
集合的基礎 ................................................................................................. 397
幾種方法 ..................................................................................................... 399
本章小結 ............................................................................................................ 404
辭彙表 ......................................................................................405

相關詞條

相關搜尋

熱門詞條

聯絡我們