亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

前情提要

在開發(fā)的時候,許多同學(xué)在文件命名方面,容易出現(xiàn)絮亂,隨意性強,沒有統(tǒng)一性。此種情況,在多人協(xié)同時,尤為突出。各開發(fā)人員都要去適應(yīng)每個人的開發(fā)習(xí)慣,諸多不便,阻礙了多人協(xié)同開發(fā)的效率。

統(tǒng)一規(guī)范

使用統(tǒng)一的開發(fā)規(guī)范,好處甚多。減少開發(fā)間的磨合,是其一,舉例:
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 的形式。此形式是我主推的,優(yōu)點是直觀的知道此參數(shù)的由來(users 表中 id 字段)。

返回的參數(shù)也做了直觀的說明,取值為 users 表中 username 字段的值。

function 命名按照動作來區(qū)分命名,get + 字段 取值,set + 字段 更新值。

命名統(tǒng)一

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

table - users

以 users 表來作為藍(lán)本,向同學(xué)們推行此規(guī)范。

migrations - 數(shù)據(jù)庫遷移

database/migrations/xxxx_create_users_table.php

···

use Illuminate\Support\Facades\DB;

···    
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('登錄態(tài)');
    $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

<?php 
namespace App\Http\Controllers\Api\v1;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller{
    public function index(Request $request)
    {
        // todo
    }
    // index() end
    
    public function show(Request $request)
    {
        // 變量命名,對應(yīng)的是表字段的話,變量名建議以該字段為名,
        // 注釋時采用 表名.字段 的形式
        // 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
        // 更新數(shù)據(jù)
        User::where('id', $user_id)->update(['age' => $age]);
    }
    // store() end
}

request - 表單驗證

app/Http/Requests/UserRequest.php

observer - 觀察者

app/Observers/UserObserver.php

event - 事件系統(tǒng)

app/Events/UserEvent.php 事件

app/Listeners/UserListener.php 監(jiān)聽器

console - 任務(wù)調(diào)度

app/Console/Commands/UserCommand.php

$ php artisan my:user

seeder - 數(shù)據(jù)填充

database/seeds/UserSeeder.php 生成假數(shù)據(jù)

database/factories/UserFactory.php 模型工廠

規(guī)范定義

我將上面此種規(guī)范定義為 以表規(guī)名,對此的解釋是,以表名為主線,規(guī)定其相關(guān)業(yè)務(wù)的文件,均以表名為關(guān)鍵字進(jìn)行后續(xù)文件的命名。

命名 - 思維導(dǎo)圖

602235cdcdce2.jpg

結(jié)語

希望我的個人建議,能在同學(xué)們間推行與流行起來。


分享到:
標(biāo)簽:Laravel 框架 編程規(guī)范
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達(dá)人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定