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
  • Functionality other than transfering information to render the view, such as validation, or reusable logic should be done in external utils
  • Do not import controllers in other controllers
  • Send information to the front end in a single associative array bash $viewData eg. view(example.index)->with('viewData',$viewData)
  • Do not send visual layer information through a controller, eg. "title", "subtitle" CHECK
  • 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, dont load all products

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');

Views

  • All views must have Blade templating to extend the base layout.
  • There is to be no mixing of PHP with HTML in the views
  • Store all text in resources/lang
  • Always use getters for parameter acquisition in classes
  • Use @csrf in forms

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