Skip to content

Coding standards

Luis Torres edited this page Nov 10, 2025 · 8 revisions

General

  • Code and comments strictly written in English.
  • Locale-dependent strings must go in the lang directory.

Specific

Controllers

  • Use an associative array named viewData to pass data to the view.
  • Using named routes to redirect instead of the route's relative path.
  • Add type annotations to parameters and return type of controller methods.
  • Put imports at the top of the file. Never use absolute import.
  • Controller methods that take in form data to modify a model should call a static method of the model that validates the form data instead of doing it themselves.
  • Use getter to retrieve models' attributes.
  • Use setter to change models' attributes.

Views

  • Don't hardcode locale-dependent strings. Instead, use __('string.key.here') and create the corresponding .php files in resources/lang/ so that the application can be further translated into other languages.
  • Extend from layouts/app.blade.php instead of writing your own header and footer.
  • Use the model's getters and setters for primitive attributes.
  • Don't use the Blade @php directive to embed PHP code into the views. Delegate that responsibility to controllers or utils.
  • Don't use inline CSS (i.e. don't use the style attribute). Create custom CSS files on their own right instead.
  • Don't use inline JavaScript. Just as the rule above, create a JS file instead.

Models

  • Don't create a setId method.

  • Define getters and setters for primitive attributes that do not entail any reasonably foreseeable security concern.

  • Define the model's attributes using a block comment right after the definition. For example:

    class Order extends Model
    {
      /**
       * ORDER ATTRIBUTES
       *
       * $this->attributes['id'] - int - contains the order's primary key (id)
       * $this->attributes['total'] - int - contains the order's total price
       * $this->attributes['created_at'] - timestamp - contains the order creation date
       * $this->attributes['updated_at'] - timestamp - contains the order update date
       *
       * $this->user - User - contains the associated User
       * $this->item - Item - contains the associated Item
       */
    }
  • Handle model relations using the functions provided by Laravel's Eloquent object-relational mapper.

  • Always add timestamp attributes to the models for future traceability.

  • Define the $fillable class attribute for mass-assignable attributes.

Routes

  • Associate routes with a name so that they may be referred to throughout the program in a simple way. E.g. / should be named home.index.
  • If a controller handles several routes, group them together using a route group. It's not necessary to use either route prefixes or route name prefixes. We can tolerate that kind of duplication.
  • Name routes using the plural form. For example, /products instead of /product.

Authorship

Each file must have the author listed at the top in this format:

/**
 * @author Name
 */

Full name is not required, but consistency across files is encouraged.

Clone this wiki locally