Laravel ??? ?? ??(TDD): ?? ?? ? ?? ??? ??
Aug 27, 2023 am 09:48 AMLaravel ??? ?? ??(TDD): ?? ?? ? ?? ?? ??? ??
??
????? ?? ??????? ?? ??? ?? ?? ???? ???? ?? ?????. TDD(??? ?? ??)? ??? ??? ???? ?? ??? ?????? ??? ??? ?? ???? ????? ?? ??? ?????. ? ????? ??? ?? ??? ?? Laravel ?????? ???? ??? ???? ?? ??? ?? ?????.
??? ?? ??(TDD)?? ??????
??? ?? ??(TDD)? ??? ???? ?? ??? ?? ??? ???? ?? ??????. TDD? ?? ????? ?? ???? ? ???? ??? ??? ?? ??? ???? ??? ???? ?? ??? ???? ????, ???? ????? ??? ????, ????? ??? ?????? ???? ????. ?? ???. ??? ???? TDD? ??? ???? ??? ???? ??? ???? ???? ??? ???? ????? ?????.
Laravel ????? ??? ??
Laravel ?????? ?? ???, ?? ???, ???? ???? ??? ??? ??? ??? ?????. ? ? ?? ???? ?? ???? ??? ????? ? ????, ?? ???? ?? ???? ????? ????? ? ????, ???? ???? ?????? ???? ?? ??? ??????? ? ?????. ??? ??? ??? ?? ??? ????? ????? ??? ??? ???? ??? ? ????.
TDD? Laravel ????
??? TDD? Laravel? ???? ??? ???? ??? ????. ??? ??, ??, ?? ?? ??? ???? ??? ?? ???? ????? ??? ?????.
- ??? ??? ???
?? ???? ?? ??? ???? ???? ?? ??? ???? ???? ???. Laravel??? Artisan ???? ???? ??? ???? ??? ? ????. ???? ?? ??? ?????:
php artisan make:test UserTest --unit
? ??? ??? ? Laravel? tests/Unit
????? UserTest.php
?? ??? ??? ??? ?????. ? ??? ?? ??? ? ?? ??? ??? test_example
? ??? ? ? ????. ?? ?? ???? ??? ??? ? ?? ??? ???? ???? ??????. tests/Unit
目錄下生成一個(gè)名為UserTest.php
的測(cè)試類文件。打開這個(gè)文件,我們可以看到一個(gè)示例測(cè)試方法test_example
,可以將其刪除。然后,我們開始編寫用戶增加功能的測(cè)試方法:
public function test_can_create_user() { $user = [ 'name' => 'John Doe', 'email' => 'john@example.com', 'password' => 'password123' ]; $response = $this->post('/users', $user); $response->assertStatus(201) ->assertJson([ 'name' => 'John Doe', 'email' => 'john@example.com' ]); }
在上述代碼中,我們首先創(chuàng)建了一個(gè)待添加的用戶數(shù)組$user
,然后使用$this->post()
方法發(fā)送一個(gè)POST請(qǐng)求來模擬用戶的增加操作。最后,使用$response
對(duì)象的assertStatus()
和assertJson()
方法來驗(yàn)證接口返回的狀態(tài)碼和返回的JSON數(shù)據(jù)是否符合預(yù)期。
- 實(shí)現(xiàn)功能代碼
接下來,我們需要實(shí)現(xiàn)用戶增加功能的具體代碼。在Laravel中,我們可以使用Artisan命令來生成控制器和模型文件。在終端中輸入以下命令:
php artisan make:controller UserController --resource --model=User
執(zhí)行完上述命令后,Laravel會(huì)在app/Http/Controllers
目錄下生成一個(gè)名為UserController.php
的控制器文件,以及在app/Models
目錄下生成一個(gè)名為User.php
的模型文件。打開UserController.php
文件,可以看到一個(gè)store()
方法,我們需要在這個(gè)方法中實(shí)現(xiàn)用戶的增加邏輯。
public function store(Request $request) { $user = User::create([ 'name' => $request->input('name'), 'email' => $request->input('email'), 'password' => Hash::make($request->input('password')) ]); return response()->json($user, 201); }
在上述代碼中,我們使用create()
方法創(chuàng)建了一個(gè)新的用戶,并使用Hash::make()
方法來加密用戶密碼。最后,通過response()->json()
php artisan test? ????? ??
$user
? ??? ??? ??? ?? ?? $this? ??????. -> post()
???? ???? ?? ??? ??????? ?? POST ??? ????. ????? $response
??? assertStatus()
? assertJson()
???? ???? ??????? ??? ?? ??? ??? JSON ???? ??? ??? ?????. - ?? ?? ??
???? ???? ??? ??? ? ??? ?? ??? ???? ???. Laravel??? Artisan ???? ???? ????? ?? ??? ??? ? ????. ???? ?? ??? ?????:
rrreee? ??? ??? ? Laravel? app/Http/Controllers
???? ??? UserController.php
?? ????? ?????. app/Models
????? User.php
?? ?? ??? ?????. UserController.php
??? ?? store()
???? ? ? ????. ? ???? ??? ?? ??? ???? ???.
? ????? create()
???? ???? ? ???? ???? Hash::make()
???? ???? ??? ????? ??????. . ????? response()->json()
??? ?? ??? ??? ??? JSON ??? ?????.
? ??? Laravel ??? ?? ??(TDD): ?? ?? ? ?? ??? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP?? ?? ??? ???? ? ?? ?? ??? ????. 1. php.ini? ?? ??? ??; 2. ? ?? (? : Apache? Setenv ?? nginx? FastCGI_Param)? ??????. 3. PHP ?????? putenv () ??? ??????. ? ??? Php.ini? ????? ??? ???? ??? ???? ? ?? ??? ?? ???? ????? ???? Putenv ()? ?? ??? ?????. ?? ???? ?? ?? (? : php.ini ?? ? ?? ??)? ???? ????. ?? ?? ??? ??? ?? ??? ????? ???? ?? ????.

Laravel? ?? ??? ?? ?? ??? ?? ?? ??? ???? ??? ??????. ?? ???? ?? ??? ????? ? ???? I/O ?? ? ?? ?? ??? ???? ???? ??? ?? ? ????. 1. ?? ????? ?? ? ? ???????? ??? ????? ?? ???? ??????. 2. ??? ? ??? ?? ? ? PhPartisAnconfig? ?? ???????. 3. ?? ??? ??? ??? ???? ?? ?? ?? ???? ???? ????. 4. ?? ?? ??? ???? ?? ??? ??? .env ??? ???? ?? ???????.

PHP ????? ?? ??? ??? ? ??? ??? CI (Continuous Integration) ????? ???? ? ????. 1. DockerFile? ???? ?? ???, ?? ??, ??? ?? ? ?? ??? ???? PHP ??? ?????. 2. Gitlabci? ?? CI/CD ??? ???? .gitlab-ci.yml ??? ?? ??, ??? ? ?? ??? ???? ?? ??, ??? ? ??? ?????. 3. PHPUNIT? ?? ??? ??? ??? ???? ?? ?? ? ???? ???? ????????. 4. Kubernetes? ?? ?? ?? ??? ???? ?? .yaml ??? ?? ?? ??? ?????. 5. Dockerfile ??? ? ??? ??? ??????

Laravel? eloquentscopes? ?? ??? ??? ??? ?????? ?? ?? ??? ????? ?????. 1. ?? ??? ???? ???? ???? ???? Post :: published (); 2. ??? ??? ?? ??? ???? ???? ?? ??? ?? ?? ?? ??? ???? ???? ??? ?????? ??? ???? ???????. 3. ????? ?? ?? ?? ??? ??? ?? ?? ??? ?? ? ? ??? ?? ? ? ?? ?? ??? ?????. 4. ?? ??? ? ??? ?? ???? ? ??? ? ?? ??, ?? ??, ?? ???? ? ?? ?????????.

??? ?? ??? PHP ???? ?? ?? ??? ???? ?? ???????. RBAC (Role-Based Access Control) ??? ?? ???, ?? ? ??? ???? ??? ?? ?? ? ??? ?????. ?? ???? ??? ?????. 1. ???, ?? ? ??? ? ???? user_roles ? role_permissions? 3 ?? ?? ???; 2. $ user-> can ( 'edit_post')? ?? ???? ?? ?? ??? ?????. 3. ??? ???? ??? ??????. 4. ?? ??? ???? ?? ?? ?? ? ??? ? ???? ???? ?? ??? ? ?? ??? ?????. 5. ??? ??? ?? ?? ???? ?? ???? "??"? ??????.

PHP ??? ?? ???? ????? ??? ??, ??? ??, ?? ? ?? ?? ???? ???????. ??, ??? ?? ???? ???? JWT? ???? ??? ??? ??????. ??, ??? ?? ????? ? ?????? ??? ???? ?? ???? ?????. ??, Alipay ?? WeChat ??? ???? ???? ??? ?????. ??, ?? ?? ??? ?? ??? ??? ??? ?????. Laravel ??? ??? ???? ?? ???? ?????, ?? ?? ? ??? ??? ???? ??? ??? ????, ??? ????? ??, ??????, ?? ? ?? ??? ?????? ??? ??? ??????? ??? ? ??? ???????.

CreateAhelpers.phpfileInapp/helperswithCustOmFunctionsikeFormatPrice, isactiveroute, andisAdmin.2.addTheFileTothe "??"sectionOfcomposer.jsonUnderAutoLoad.3.runcomposerDump-AUTOLOADTOMAKETHINGTICTIONSGLOBELYAVAILABLE.4.USETHEHELPERFUNCUNTION

?? ?? ?? : ?? ????? PHP? ?? Error_Log ()? ??? ? ????. ????? ???? ??? ?? ??? ?????? ???? ?? ??? ? ?? ??? ???? ??? ?? ???, ??, ?? ? ?? ? ?? ?? ??? ???? ??? ??????. 2. ??? ?? ?? : ??? ??? ??? ??? ? ??? ?? ??? ??? ?? ??? ??? ??????? ??????. MySQL/PostgreSQL? ???? ??? ? ???? ??????. Elasticsearch Kibana? ? ???/? ???? ?????. ???, ??? ?? ? ??? ? ?? ??? ?? ??????. 3. ?? ? ?? ????? : ??, ???, ?? ? ??? ??? ??????. Kibana? ?? ????? PHP ??? ?? ?? ?????? ???? ???? ?????? ???? ??? ? ?? ??? ??? ? ????.
