Introduction: Why Your PHP Project Needs an API Gateway
\n\nImagine 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\nSound 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\nIn 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\nWhat is an API Gateway and Why Does a PHP Developer Need It
\n\nAn 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.
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
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\nDesigning a Simple API Gateway in PHP
\n\nWe 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\nOur 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/profilewill collect information from Users and Orders \n
Step 1. Route Configuration
\n\nLet's create a file config.php where we describe which URLs go where:
<?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