小編給大家分享一下laravel的編程規(guī)范,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司一直秉承“誠信做人,踏實(shí)做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個(gè)客戶多一個(gè)朋友!專注中小微企業(yè)官網(wǎng)定制,網(wǎng)站建設(shè)、成都做網(wǎng)站,塑造企業(yè)網(wǎng)絡(luò)形象打造互聯(lián)網(wǎng)企業(yè)效應(yīng)。
在開發(fā)的時(shí)候,許多同學(xué)在文件命名方面,容易出現(xiàn)絮亂,隨意性強(qiáng),沒有統(tǒng)一性。此種情況,在多人協(xié)同時(shí),尤為突出。各開發(fā)人員都要去適應(yīng)每個(gè)人的開發(fā)習(xí)慣,諸多不便,阻礙了多人協(xié)同開發(fā)的效率。
使用統(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)點(diǎn)是直觀的知道此參數(shù)的由來(users 表中 id 字段)。
返回的參數(shù)也做了直觀的說明,取值為 users 表中 username 字段的值。function 命名按照動(dòng)作來區(qū)分命名,get + 字段 取值,set + 字段 更新值。
下面,我通過 users 表舉例,列舉我推薦命名的邏輯。
以 users 表來作為藍(lán)本,向同學(xué)們推行此規(guī)范。
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 '用戶表'");···app/Models/User.php
app/Http/Controllers/UserController.php
<?phpnamespace 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)的是表字段的話,變量名建議以該字段為名,
// 注釋時(shí)采用 表名.字段 的形式
// 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}app/Http/Requests/UserRequest.php
app/Observers/UserObserver.php
app/Events/UserEvent.php 事件
app/Listeners/UserListener.php 監(jiān)聽器
app/Console/Commands/UserCommand.php
$ php artisan my:user
database/seeds/UserSeeder.php 生成假數(shù)據(jù)
database/factories/UserFactory.php 模型工廠
我將上面此種規(guī)范定義為 以表規(guī)名,對此的解釋是,以表名為主線,規(guī)定其相關(guān)業(yè)務(wù)的文件,均以表名為關(guān)鍵字進(jìn)行后續(xù)文件的命名。

以上是“l(fā)aravel的編程規(guī)范”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享名稱:laravel的編程規(guī)范
文章起源:http://www.chinadenli.net/article6/gpdsog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站改版、品牌網(wǎng)站制作、網(wǎng)站營銷、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)