Skip to content

Programming Rules & Standard

Esteban Alvarez Zuluaga edited this page Sep 23, 2025 · 22 revisions

Laravel

PHP Code

PHP code must adhere when possible to standard PSR-12.

Key points

  • There should be type annotations where possible, for parameters and return values in functions, classes and methods
  • Utilize dependency inversion where possible
  • There should be only one class per file, and where possible have the file name match the class name

Controllers

  • Do not carry out "echo" operations inside controllers
  • Limit controller functionality to receiving information from the views and redirecting to other views. Make sure as much logic as possible is done in external utils.
  • Do not import controllers in other controllers
  • Send information to the front end in a single associative array $viewData eg. view(example.index)->with('viewData',$viewData)
  • Use lang for visual layer information such as titles, subtitles and paragraphs. Do not send this information from the controller.
  • Only store primitive types in session, never objects
  • Always use getters & setters when using objects
  • Always do eager loading when possible to minimize database querying
  • Try to minimize the amount of results from a db query, that is, if only products with id=3 are needed, don't load all products
  • Ensure each controller has functions related exclusively to that controller's role. Make sure controller names are clear in their roles and that there is no confusion with other controller names.
  • Carry out all db queries inside the controller rather than in utils

Models

  • Only one table is to be created per migration file
  • There should be a comment in each Model file with information pertaining to the attributes as follows:
  /**
     * PRODUCT ATTRIBUTES
     * $this->attributes['id'] - int - contains the product primary key (id)
     * $this->attributes['name'] - string - contains the product name
  • Fillable attributes must be specified after the comment in a single line
  • Validations for creating the class are to be done in a static function inside the model file for the class
  • Getters and setters must be included for all attributes, except setters for id and timestamps
  • For one to many relations, foreign keys are to be specified using "constrained" as such:
$table->foreignId('other_id')->constrained()->onDelete('cascade');
  • Use Eloquent's HasMany, HasOne, belongsTo when implementing model relations

Views

  • All views must have Blade templating that extends the base layout.
  • There is to be no mixing of raw PHP with HTML in the views
  • Store all text in resources/lang
  • Always use getters for parameter acquisition in classes
  • Use @csrf in forms
  • Always use lang for text in a view, except when receiving varaible information from controllers

Routes

  • All routes must be associated with a controller, rather than specifying the function in the routes file.
  • All routes must have a single resposibility, and there cannot be two routes with the same name but different method (GET, POST)
  • The paths to each controller must be specified after imports as constants
  • Route names must be associated to controller names

References

Clone this wiki locally