-
Notifications
You must be signed in to change notification settings - Fork 0
Coding standards
Luis Torres edited this page Nov 10, 2025
·
8 revisions
- Code and comments strictly written in English.
- Locale-dependent strings must go in the
langdirectory.
- Use an associative array named
viewDatato 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.
- Don't hardcode locale-dependent strings. Instead, use
__('string.key.here')and create the corresponding.phpfiles inresources/lang/so that the application can be further translated into other languages. - Extend from
layouts/app.blade.phpinstead of writing your own header and footer. - Use the model's getters and setters for primitive attributes.
- Don't use the Blade
@phpdirective 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
styleattribute). Create custom CSS files on their own right instead. - Don't use inline JavaScript. Just as the rule above, create a JS file instead.
-
Don't create a
setIdmethod. -
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
$fillableclass attribute for mass-assignable attributes.
- Associate routes with a name so that they may be referred to throughout the
program in a simple way. E.g.
/should be namedhome.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,
/productsinstead of/product.
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.
𝔄𝔯𝔰 𝔩𝔬𝔫𝔤𝔞, 𝔳𝔦𝔱𝔞 𝔣𝔢𝔩𝔦𝔵