分享個人推薦的laravel或其它框架的編程規范

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

下面由laravel教程欄目給大家介紹個人推薦的 laravel 或其它框架的編程規范,希望對需要的朋友有所幫助!

前情提要

在開發的時候,許多同學在文件命名方面,容易出現絮亂,隨意性強,沒有統一性。此種情況,在多人協同時,尤為突出。各開發人員都要去適應每個人的開發習慣,諸多不便,阻礙了多人協同開發的效率。

統一規范

使用統一的開發規范,好處甚多。減少開發間的磨合,是其一,舉例:
app/Models/User.php

···/**  * @desc 獲取 users.username  * @param int $user_id users.id  * @return string   */public static function getUsername(int $user_id): string{     return self::where('id', $user_id)->value('username');}// getUsername() end/**  * @desc 獲取 users.age  * @param int $user_id users.id  * @return int   */public static function getAge(int $user_id): int{     return (int)self::where('id', $user_id)->value('age');}// getAge() end···

在行參 $user_id 的注釋里,我使用的是 users.id 的形式。此形式是我主推的,優點是直觀的知道此參數的由來(users 表中 id 字段)。
返回的參數也做了直觀的說明,取值為 users 表中 username 字段的值。
function 命名按照動作來區分命名,get + 字段 取值,set + 字段 更新值。

命名統一

下面,我通過 users 表舉例,列舉我推薦命名的邏輯。

table – users

以 users 表來作為藍本,向同學們推行此規范。

migrations – 數據庫遷移

database/migrations/xxxx_create_users_table.php

···use IlluminateSupportFacadesDB;···    Schema::create('balance_logs', function (Blueprint $table) {       $table->id();       $table->string('username', 32)->unique()->nullable(false)->comment('名稱');       $table->string('password', 128)->nullable(false)->comment('密碼');       $table->unsignedInteger('age', 3)->default(0)->comment('年齡');       $table->string('token', 128)->nullable(true)->comment('登錄態');       $table->dateTime('created_at')->useCurrent();       $table->dateTime('updated_at')->useCurrent();        $table->index('username', 'username_index');     });     DB::statement("ALTER TABLE `users` comment '用戶表'");···

model – 模型

app/Models/User.php

controller – 控制器

app/Http/Controllers/UserController.php

<?phpnamespace AppHttpControllersApiv1;use AppHttpControllersController;use IlluminateHttpRequest;use AppModelsUser;class UserController extends Controller{     public function index(Request $request)     {         // todo     }// index() end      public function show(Request $request)     {         // 變量命名,對應的是表字段的話,變量名建議以該字段為名,         // 注釋時采用 表名.字段 的形式         // users.username         $username = $request->post('username');     }// show() end      public function store(Request $request)     {         $user_id = $request->post('user_id');// users.id         $age     = $request->post('age');    // users.age         // 更新數據         User::where('id', $user_id)->update(['age' => $age]);     }// store() end}

request – 表單驗證

app/Http/Requests/UserRequest.php

observer – 觀察者

app/Observers/UserObserver.php

event – 事件系統

  • app/Events/UserEvent.php 事件
  • app/Listeners/UserListener.php 監聽器

console – 任務調度

app/Console/Commands/UserCommand.php

$ php artisan my:user

seeder – 數據填充

  • database/seeds/UserSeeder.php 生成假數據
  • database/factories/UserFactory.php 模型工廠

規范定義

我將上面此種規范定義為 以表規名,對此的解釋是,以表名為主線,規定其相關業務的文件,均以表名為關鍵字進行后續文件的命名。

命名 – 思維導圖

分享個人推薦的laravel或其它框架的編程規范

結語

希望我的個人建議,能在同學們間推行與流行起來。謝謝同學們的閱讀,記得幫我 點贊評論收藏轉發

以上就是分享個人推薦的

? 版權聲明
THE END
喜歡就支持一下吧
點贊13 分享