PSR standards in Laravel Framework

By following PSR standards, Laravel ensures its codebase is both developer-centric and compatible with a wide range of PHP libraries and frameworks.

Introduction: PSR Standards in the Laravel Framework

PSR standards

Common PSR Standards:

  1. PSR-1: Basic Coding Standard
    Establishes basic coding practices such as file structure, naming conventions, and usage of namespaces.
  2. PSR-2: Coding Style Guide
    Provides a comprehensive coding style guide for consistent code formatting (replaced by PSR-12).
  3. PSR-3: Logger Interface
    It defines a common interface for logging libraries, making it easy to switch between different logging tools.
  4. PSR-4: Autoloading Standard
    Introduces a standardized autoloading mechanism for classes using namespaces, making code more modular and easier to manage.
  5. PSR-7: HTTP Message Interface
    Defines interfaces for HTTP messages, requests, and responses, facilitating better interoperability in web applications.
  6. PSR-12: Extended Coding Style Guide
    Expands on PSR-2, offering more detailed rules for code formatting and addressing modern PHP features.
  7. PSR-17: HTTP Factories
    Specifies interfaces for creating HTTP requests, responses, and streams, improving the flexibility of HTTP handling.
  8. PSR-18: HTTP Client
    Defines a standard HTTP client interface to ensure consistent usage of HTTP clients in libraries and frameworks.

Benefits of Using PSR:

  • Code Consistency: easier to read, maintain, and contribute to projects.
  • Interoperability: Simplifies integration between different PHP libraries and frameworks.
  • Adoption by Popular Frameworks: Widely used by frameworks like Laravel, Symfony, and Zend.

PSR standards application in Laravel

1. PSR-1: Basic Coding Standard

  • File Naming & Structure: Laravel follows the convention of <?php as the only opening tag in files, and class names match their file names.
  • Namespaces & Class Names: Classes are organized with namespaces that match directory structures, ensuring clear and logical organization.
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    // Controller methods
}

2. PSR-2 / PSR-12: Coding Style Guide

  • Laravel enforces consistent formatting using tools like PHP CS Fixer or Laravel Pint (built-in for Laravel 9+). You can configure and run Pint to automatically fix your code to PSR-12 standards:
vendor/bin/pint
public function show(int $id): JsonResponse
{
    return response()->json(['id' => $id]);
}

3. PSR-3: Logger Interface

  • Laravel’s logging system is built on the PSR-3 Logger Interface. You can log messages using the Log facade, which is compatible with different logging libraries like Monolog.
use Illuminate\Support\Facades\Log;

Log::info('User logged in.', ['user_id' => $user->id]);

4. PSR-4: Autoloading Standard

  • Laravel follows PSR-4 for class autoloading, as defined in its composer.json file. This ensures that classes are loaded based on their namespaces and file paths.
"autoload": {
    "psr-4": {
        "App\\": "app/"
    }
}
composer dump-autoload

5. PSR-7: HTTP Message Interface

  • Laravel provides native support for PSR-7 requests and responses via its integration with packages like Nyholm/psr7 or Guzzle.
use Psr\Http\Message\ServerRequestInterface;
use Illuminate\Support\Facades\Route;

Route::post('/psr7', function (ServerRequestInterface $request) {
    return $request->getParsedBody();
});

6. PSR-18: HTTP Client

  • Laravel’s HTTP client is PSR-18 compliant through packages like Guzzle, which Laravel wraps for an elegant syntax:
use Illuminate\Support\Facades\Http;

$response = Http::get('https://api.example.com/data');