October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

SOLID Principles in PHP and Laravel, Part 1: Single Responsibility Principle

SRP groups behavior by who or what policy drives its changes. See how to apply that idea to Laravel controllers without splitting every method into a class.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The Single Responsibility Principle (SRP) is a way to decide what a class should own: group behavior that changes for the same stakeholder or policy, and separate behavior that changes for different reasons. In Laravel, that does not mean every controller must have one method. Resource controllers are a deliberate framework convention; split an action or class when the separation makes a real change boundary clearer.

What the Single Responsibility Principle means

Robert C. Martin’s 2014 explanation puts SRP in terms of actors: “A module should be responsible to one, and only one, actor.” An actor is a stakeholder, or group of stakeholders, whose needs can cause a module to change. His companion formulation is: “Gather together the things that change for the same reasons. Separate those things that change for different reasons.” (Robert C. Martin, “The Single Responsibility Principle”.)

As an Amazon Associate I earn from qualifying purchases.

The familiar phrase “one reason to change” is about coherent ownership of change, not a literal limit of one method or operation per class. A class can have several methods and still serve one coherent purpose. Conversely, a class with only a few methods may combine concerns owned by different stakeholders.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to tell whether a class has too many responsibilities

In a review, ask: Which stakeholder or policy change would make us edit this class? Consider a class that handles HTTP request mapping, enforces a business rule, and formats an external report. If those areas change for unrelated reasons or at the request of different stakeholders, the class may be combining responsibilities.

Use these questions to decide whether a boundary would help:

  • Change driver: Do these behaviors change for the same actor or policy, or for different ones?
  • Cohesion: Do the operations form one understandable responsibility in the domain?
  • Independent complexity: Has one operation become complex enough to understand or evolve on its own?
  • Refactor cost: Would extraction make changes and testing clearer, or merely add indirection?

These are practical prompts derived from SRP, not a Laravel checklist. Extract a boundary when it improves comprehension or lets concerns change independently. Do not extract code solely to reduce a method count.

Applying SRP to Laravel controllers

Laravel describes controllers as a way to organize request handling. A controller may group methods for related requests, and resource controllers provide conventional actions for creating, reading, updating, and deleting a resource. These are framework-supported patterns, not automatic SRP violations. Judge whether the actions concern the same resource and change for related reasons. Laravel also notes that a particularly complex action may merit an entire single-action controller (Laravel 12.x controller documentation).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Laravel’s routing documentation describes route files as the place where routes are defined and loaded through application configuration. The web route file handles browser-facing routes; optional API routing can be enabled for stateless API routes (Laravel 13.x routing documentation). This gives route declarations and controller request handling distinct places in the framework, but SRP does not prescribe a particular directory tree or require a specific split.

A measured refactor: a crowded OrderController

Imagine an OrderController that validates an HTTP request, applies pricing rules, saves an order, generates an invoice PDF, and sends a customer email. This is an illustrative design example, not a report of tested code. The useful question is not how many jobs appear in the list, but whether they have different change owners: request requirements may change with the API or form, pricing with business policy, invoice layout with presentation needs, and email behavior with notification policy.

  1. Keep request coordination in the controller. Let it receive and validate the request, coordinate the use case, and return an appropriate response. Avoid putting unrelated document-formatting or notification details there just because the request begins there.
  2. Move pricing only when it is a meaningful boundary. If pricing rules form a business policy that changes independently, give them a focused policy or service. If extraction would only wrap a trivial operation and obscure the flow, it may not help.
  3. Give invoice output its own owner when appropriate. A dedicated invoice component can own PDF generation and presentation details if those concerns evolve separately from order handling.
  4. Separate notification policy when it merits independence. A notification component can own decisions and behavior about customer email without making the controller responsible for the email’s full implementation.

These are design choices, not required Laravel classes or proof that every controller should be thin. The right split is the one that makes the flow and the likely changes easier to understand without adding needless indirection.

SRP and PHP file standards are different ideas

PHP-FIG’s PSR-1 is a coding standard, not a definition of SRP. It says files “SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.” It also provides class and method naming conventions. Those guidelines can support clear PHP file organization; they do not require one responsibility per class (PHP-FIG PSR-1: Basic Coding Standard).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A practical rule for your next review

Start with the likely change, not a target number of classes. Identify who or what policy would drive the change, then see whether the affected behavior has a clear, cohesive home. Keep related Laravel resource actions together when they share a purpose; consider a dedicated action or collaborator when complexity or independent change makes the boundary useful. SRP is a tool for reasoning about change ownership, not a mandate to fragment code.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.