PHP and Laravel Best Practices

Ashwani Garg
2 min readMar 12, 2023

--

Front End

1) HTML, CSS, Javascript, ES6, ESlint

2) JS and CSS Bundle

3) Laravel Blade

Back end

1) Core PHP

2) PHP OOPS

3) Laravel-

4) Laravel MMVC using nwidart (in case of large project with multiple modules)

5) Eloquent ORM

6) Facades, Service Containers and Service Providers

7) Artisan Console command creation

8) Queue and Jobs

9) Laravel channels for logging

10) Event and Listeners with Queues for async operations

11) Broadcasting using sockets and pusher

12) Laravel Sanctum

13) Use the Laravel Debugbar

Code Reusability

  1. Dependency injections helper Files using composer.json
  2. SOLID principles.
  3. Design Pattern: Singleton, Factory,Observer,Proxy,Adapter,Builder,flyweight

Coding Standards

Laravel is flexible in coding. If your variables are compatible with the composer, you’re good. Adhering to PSR-2 and PSR-4 coding standards will keep your code clean and free of confusion.

Here’s what PSR-2 and PSR-4 coding standards mean:

PSR-2: Have a single style guide for your PHP code to keep a uniform format

PSR-4: Specify autoloading classes from the file path to ensure files autoload in your code where you want them to.

Database

1) Creating procedure/triggers/functions

2) Redis/Memcache database for fast rendering

3) Creating Non Clustered Indexes

4) Clustering, Table Partitioning

5) Sharding,

6) Replication,

7) Backup and restore

8) Optimising SQL Queries.

9) Database engine tuning.

10) No-SQL Databases

Code Review

  1. Sonarqube
  2. PHPMD
  3. XDebug

Code Analysis

  1. PHPMD
  2. SonarQube
  3. PHP Unit for TDD

Code Coverage

  1. Xdebug, a PHP extension project, that is the de-facto tool that helps in step debugging, profiling, and of code in code coverage.
  2. PCOV, a recent PHP extension by Joe Watkins aims to be a minimal and fast solution for code coverage, which solves the often slow Xdebug version 2 series.
  3. phpdbg is a PHP Debugger shipped with PHP itself. It has features similar to Xdebug, albeit relatively less adoption in community and IDEs. phpdbg is implemented as a server API, which limits its usefulness, but it provides an easy approach to code coverage without having to install PECL extensions.

Background Services

  1. Crontabs for Task Schedules
  2. Linux SuperVisor for QueueJobs in back ground
  3. Nginx web server for easy route blocking, ip blocking

--

--

Ashwani Garg
Ashwani Garg

Written by Ashwani Garg

FullStack Web, App Developer and also Full Time Blogger

No responses yet