Backend Development Rules

Rules For Routing:

  1. Always must use route model binding
  2. Always group related routes
  3. Always use Middleware for inspecting and filtering HTTP requests
  4. Always create named routes.must use prefix for groped routed i.e
    //This is an exmple how you must define the groued routes
    
    Route::name('elite.')->prefix('elite')->group(function () {
                Route::controller(EliteUniversityController::class)->group(function () {
                    Route::get("", 'show')->name('show');
                    Route::post("", 'import')->name('import');
                    Route::post("create", 'create')->name('create');
                    Route::post("update/{elite_university}", 'update')->name('update');
                    Route::delete("delete/{elite_university}", 'delete')->name('delete');
                });
    
                Route::name('packages.')->prefix('packages')
                    ->controller(ElitePackageController::class)->group(function () {
                        Route::get("", 'show')->name('show');
                        Route::get("list", 'list')->name('list');
                        Route::post("create", 'create')->name('create');
                        Route::post("update/{package}", 'update')->name('update');
                        Route::delete("delete/{package}", 'delete')->name('delete');
                    });
            });​

Rules For Form Validation:

  1. Always create FormValidation Request for validation for exmaple:
    class SaveSchoolBasicInfoRequest extends FormRequest
    {
    /**
    * Determine if the user is authorized to make this request.
    *
    * @return bool
    */
    public function authorize()
    {
    return \Auth::check();
    }

    /**
    * Get the validation rules that apply to the request.
    *
    * @return array<string, mixed>
    */
    public function rules()
    {
    return [
    'school_name'=>['required'],
    'phone'=>['required'],
    'email'=>['email',Rule::unique(School::class,'email')->ignore(\Auth::user()->campus_id)],
    'website'=>['required'],
    'facebook_url'=>['required'],
    'linkedin_url'=>['required'],
    ];
    }
    }
  2. For mass assignment always use validated data i.e ...->create($request->validated())
  3. Always check the file types and input type in validation

Rules For Code:

  1. Always choose well descriptive  variable names. i.e $user_name, $user_email. 
  2. Variable names must always be relate to its type. i.e if variable has array name must be plural userMajors, userHobbies.
  3. Always use well descriptive CONSTANTS for raw value like, status = 1 or where('status',FAIR_ACTIVE).
  4. Always Define Contents in App/Helpers/Interface/.../ and implement on app/Helpers/AppContents class
  5. Your method or function must always have one job. i.e createUser must always have one job to create user only.
  6. Method or function must not be longer than 10 lines.
  7. for longer methods or function try to create Laravel Actions. Action Names Must be descriptive i.e createUser, updateUserPassword
  8. Your methods or function arguments must always have type i.e count(Int i) or countWords(string $str) or doSomething(mixed $arg) or  use union types for two type  doSomething(?mixed $args,?int args, int|string $args )
  9. Your methods must always have a return type.i.e count():int, count():void
  10. Your method names must be descriptive.i.e countArray()
  11. Never use raw query for insertion, and avoid raw as much as you can for getting data from DB
  12. Always Use Model to handle database operations
  13. Always try to insert model related tables data using model relation for example $user->majors()->attach([...]), $user->roles()->attach([...]) or $fair->sessions()->createMany([...])
  14. Always use $request->file->upload('path') to upload files i.e images.

 

Did you find this article useful?