Skip to content

Programming Rules & Standard

Esteban Alvarez Zuluaga edited this page Nov 10, 2025 · 22 revisions

Laravel

PHP Code

PHP code must adhere to standard PSR-12.

Key points

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

Controllers

  • Do not commit code containing "echo" or dump and die "dd()" operations.
  • 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 views in a single associative array $viewData eg. view(example.index)->with('viewData',$viewData).
  • Use lang for all visual layer information such as titles, subtitles and paragraphs. Do not send this information from the controller.
  • Lang is to be used for all text the user sees except when receiving variable information from controllers.
  • 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.

Models

  • Each migration file must correspond to just one table.
  • 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.
  • Attributes must be accesed through getters and modified through setters.
  • 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.
  • Always use @csrf in forms.
  • Always use lang for text in a view, except when receiving variable 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