Rules For Routing:
- Always must use route model binding
- Always group related routes
- Always use Middleware for inspecting and filtering HTTP requests
- 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:
- Always create
FormValidation
Request for validationfor 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'],
];
}
} - For mass assignment always use validated data i.e
...->create($request->validated())
- Always check the file types and input type in validation
Rules For Code:
- Always choose well descriptive variable names. i.e $user_name, $user_email.
- Variable names must always be relate to its type. i.e if variable has array name must be plural userMajors, userHobbies.
- Always use well descriptive CONSTANTS for raw value like,
status = 1 or where('status',FAIR_ACTIVE)
. - Always Define Contents in App/Helpers/Interface/.../ and implement on app/Helpers/AppContents class
- Your method or function must always have one job. i.e createUser must always have one job to create user only.
- Method or function must not be longer than 10 lines.
- for longer methods or function try to create Laravel Actions. Action Names Must be descriptive i.e createUser, updateUserPassword
- Your methods or function arguments must always have type i.e
count(Int i)
orcountWords(string $str)
ordoSomething(mixed $arg)
or use union types for two typedoSomething(?mixed $args,?int args, int|string $args )
- Your methods must always have a return type.i.e count():int, count():void
- Your method names must be descriptive.i.e countArray()
- Never use raw query for insertion, and avoid raw as much as you can for getting data from DB
- Always Use Model to handle database operations
- Always try to insert model related tables data using model relation for example $user->majors()->attach([...]), $user->roles()->attach([...]) or $fair->sessions()->createMany([...])
- Always use
$request->file->upload('path')
to upload files i.e images.