Laravel Best Practice Standard

Laravel Best Practice Standard

Laravel is web application framework dengan syntax which is expressive and elegant. web application framework provides a structure and starting point for creating your app, allowing you to focus on building something while thinking about the details.


#1. Prinsip Single Responsibility

Classes and methods should have only one responsibility.

#2. Fat Models, Skinny Controller

Insert all DB-related logic into the model eloquent atau ke dalam kelas repositori jika anda menggunakan Query Builder.

#3. Validation

Move validation from Controller ke Class Request.

#4. Don’t Repeat Yourself (DRY)

When programming, Avoid duplicating code as much as possibleThe Single Responsibility principle helps you avoid duplication by ensuring each piece of code has a single responsibility. Moreover Take advantage of features such as reuse of blade templates, use of scopes on Eloquent, and more to maximize efficiency and consistency in your app development.

#5. Use Eloquent, instead of Query Builder / Raw Query

Eloquent Allows you to write readable code and maintainable. Dan, Eloquent have built-in tools yang bagus seperti soft deleteseventsscopes, dll.

#6. Mass Assignment

Sending an array to model creation, is essentially setting multiple fields on the model at once, rather than one at a time.

#7. Follow the Laravel Naming Convention

follow Prinsip single responsibility and follow the naming conventions accepted by the Laravel community.

#8. Use Middleware instead of Repeating Code

Middleware in Laravel allows you to filter or change Request saat ini. Berikut beberapa kasus penggunaan:

  • Check the required permissions
  • Check the user's language and change the locale

And as you'd expect, Laravel comes with a bunch of ready-to-use middleware for authentication, speed limiting, and more.

Once your middleware is doing what it's supposed to do, you can block it Request tersebut atau membiarkannya lewat.

Middleware can be attached to any number of routes, which helps you prevent code duplication.

Berita Rekomendasi

ChatGPT & Programmer Akan Saling Membantu

12/11/2024

ChatGPT & Programmers Will Help Each Other

Can ChatGPT create computer programs? Right! But making "big" programming, which is a computer system, still feels like it can't be done. Challenges Faced Why? Because creating a computer system, the process is also long...

View
Flutter: Kerangka Kerja Aplikasi Mobile Multiplatform yang Efisien

12/11/2024

Flutter: An Efficient Multiplatform Mobile Application Framework

Flutter is a multiplatform mobile application framework created by Google. Flutter allows developers to build apps that can run on various platforms such as Android, iOS, websites, and desktops,...

View
Cegah Covid-19 dengan 10M

11/11/2024

Prevent Covid-19 with 10M

Recently, the number of covid-19 cases has continued to increase. In order to protect ourselves, our families and those closest to us so that everything remains protected, we must comply with health protocols. Therefore, the implementation of health protocols...

View