API Gateway for PHP Services: How to Unite Microservices Without a Headache

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Your PHP Project Needs an API Gateway

\n\n

Imagine you are building a modern web application. You have a user service on Laravel, an order service on Symfony, a payment service on plain PHP, and also external integrations with a CRM and a Telegram bot. Clients (a mobile app, an SPA on React, and a good old jQuery website) hit different endpoints, get confused with tokens, and you fix CORS every week.

\n\n

Sound familiar? If so, you need an API Gateway — a single entry point for all your services. This is not just a proxy, but a real dispatcher that handles routing, authentication, caching, and data aggregation.

\n\n

In this article, we will break down how to write a simple but functional API Gateway in PHP that can be deployed on any hosting. No magic, just clean code and best practices for a microservice architecture in PHP.

\n\n

What is an API Gateway and Why Does a PHP Developer Need It

\n\n

An API Gateway is a server that receives all incoming requests from clients and forwards them to the appropriate microservices. The client knows nothing about the internal structure: it simply sends a request to /api/users, and the Gateway itself decides where to send it.

\n\n

Main tasks of the gateway:

\n
    \n
  • Routing — forwarding requests to different services based on URL or headers.
  • \n
  • Authentication and Authorization — verifying JWT tokens, API keys, restricting access.
  • \n
  • Aggregation — combining data from multiple services into a single response (e.g., user profile + their recent orders).
  • \n
  • Caching — reducing backend load by caching frequently requested data.
  • \n
  • Rate limiting — protecting against DDoS and service overload.
  • \n
  • Response Transformation — converting data to a unified format for different clients.
  • \n
\n\n

The advantages are obvious: simplifying the client side, centralized security management, and the ability to change the backend without notifying clients. The downside is an additional point of failure, but this is solved by replicating the Gateway.

\n\n

Designing a Simple API Gateway in PHP

\n\n

We will write a Gateway in plain PHP without frameworks so you understand the essence. But you can easily adapt the code for Laravel, Symfony, or Slim.

\n\n

Our gateway will:

\n
    \n
  • Accept requests to /api/users/* and forward them to the Users service (port 8081)
  • \n
  • Accept requests to /api/orders/* and forward them to the Orders service (port 8082)
  • \n
  • Check the JWT token before allowing the request through
  • \n
  • Cache GET requests for 60 seconds
  • \n
  • Aggregate data: /api/profile will collect information from Users and Orders
  • \n
\n\n

Step 1. Route Configuration

\n\n

Let's create a file config.php where we describe which URLs go where:

\n\n
<?php\n// config.php\nreturn [\n    'routes' => [\n        '/api/users' => [\n            'target' => 'http://127.0.0.1:8081',\n            'auth' => true,        // JWT required\n            'cache' => false       // do not cache (personal data)\n        ],\n        '/api/orders' => [\n            'target' => 'http://127.0.0.1:8082',\n            'auth' => true,\n            'cache' => 60          // cache for 60 seconds\n        ],\n        '/api/public' => [\n            'target' => 'http://127.0.0.1:8083',\n            'auth' => false,\n            'cache' => 120\n        ],\n        '/api/profile' => [        // aggregated endpoint\n            'aggregate' => true,\n            'auth' => true,\n            'services' => [\n                'user' => 'http://127.0.0.1:8081/api/user',\n                'orders' => 'http://127.0.0.1:8082/api/order                

Blogs

Book Recommendations