Single Action Controllers

Introduction to Single Action Controllers & Webhook Handling in Laravel

Webhooks enable real-time communication between systems by sending HTTP requests when an event occurs. In this lesson, we’ll set up a basic webhook handler in Laravel using a single action controller—a clean and efficient way to handle incoming webhook requests.

What Are Single Action Controllers?

A single action controller is a controller that has only one method, __invoke(). Laravel automatically calls this method when the route is hit, meaning we don’t need to specify a method in our route definition. This makes our webhook handling simpler and more focused, especially since it’s dedicated to processing a single type of request.

Instead of defining multiple methods, we register our controller like this:

Route::post('/webhook', WebhookController::class);

Laravel detects that WebhookController has an __invoke() method and automatically executes it when the /webhook route is hit.

Why Use a Single Action Controller for Webhooks?

  • Simplicity & Clarity – The controller has a single responsibility: handling webhook requests.
  • Cleaner Route Definitions – No need to specify a method name in the route file.
  • Encourages SRP (Single Responsibility Principle) – Each controller does one thing and does it well.

Returning a 204 No Content Response

At this stage, our goal is to simply receive webhook requests and return a response. We’ll implement a handler that sends back an HTTP 204 No Content status. This tells the webhook publisher that the request was received successfully, even though there’s no response body.

Returning a 204 is a best practice in webhook handling, as it ensures minimal response payloads while clearly signaling success.

Why Start with a Simple Handler?

Right now, we’re only setting up the endpoint to receive webhooks. The actual processing, validation, and forwarding of data will come later in the course. This lesson ensures we have a stable entry point before implementing more complex logic.

Key Takeaways

Single Action Controllers – Using the __invoke() method simplifies routing and keeps the controller focused.

Minimal but Functional – The handler returns a 204 status to confirm the webhook was received.
Forwarding Data Comes Later – We’ll later validate and process webhook payloads, forwarding them to services like AudienceGrid.
Simplicity First – Setting up a working endpoint is the first step before adding complexity.

Branch: https://github.com/GaryClarke/laravel-microservice/tree/5-webhook-controller




If you would like to learn more about PHP magic methods like __invoke then you should check out my OOPHP course.

This course is a must have for any PHP developer.

Check it out here 👉 https://www.garyclarke.tech/p/learn-object-oriented-php

Complete and Continue  
Discussion

0 comments