Home  >  Article  >  PHP Framework  >  Share your personal recommended programming specifications for laravel or other frameworks

Share your personal recommended programming specifications for laravel or other frameworks

藏色散人
藏色散人forward
2021-02-09 09:10:592039browse

The following tutorial column will introduce to you the personally recommended programming specifications of laravel or other frameworks. I hope it will be helpful to friends in need! Preliminary Summary

During development, many students are prone to confusion, randomness, and lack of uniformity in file naming sex. This situation is particularly prominent when multiple people collaborate. Each developer has to

adapt

to everyone's development habits. Many inconveniences hinder the efficiency of multi-person collaborative development. Unified specifications

Using unified development specifications has many benefits. One of them is to reduce the friction between developers. For example: 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···

In the comments of the line parameter

$user_id
, I use ## In the form of #users.id

. This form is my main recommendation. The advantage is that you can intuitively know the origin of this parameter (the

id field in the users table). The returned parameters are also intuitively explained, and the value is the value of the username field in the users table.
function The naming is distinguished according to the action. get field takes the value, and
set field updates the value. Unification of naming

Below, I will use the

users table as an example to list the logic of my recommended naming.

table - users

Use the users table as a blueprint to promote this standard to students.

migrations - Database migration

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('登录态');
      $table->dateTime('created_at')->useCurrent();
      $table->dateTime('updated_at')->useCurrent();

      $table->index('username', 'username_index');
    });
    DB::statement("ALTER TABLE `users` comment '用户表'");···

model - Model

app/Models/User.php

controller - Controller

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)
    {
        // 变量命名,对应的是表字段的话,变量名建议以该字段为名,
        // 注释时采用 表名.字段 的形式
        // 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 - Form Validation

app/Http/Requests/UserRequest.php

observer - Observer

app/Observers/UserObserver.php

event - Event System

app/Events/UserEvent.php Eventapp/Listeners/UserListener.php Listener

  • console - task scheduling
app/Console/Commands/UserCommand.php

$ php artisan my:user

seeder - data filling

database/seeds/UserSeeder.php Generate fake datadatabase/factories/UserFactory.php Model factory

  • Specification definition
I define the above specifications as

table specification name. The explanation is that the table name

is used as the main line to stipulate the documents related to its business. All subsequent files are named using

table name as the keyword. Naming-Mind Map

##Conclusion

Share your personal recommended programming specifications for laravel or other frameworksHope for my personal suggestions , can be promoted and popular among students. Thank you students for reading. Remember to

like

, comment,

collect

, and forward.

The above is the detailed content of Share your personal recommended programming specifications for laravel or other frameworks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete