Modern application architecture increasingly prioritizes flexibility and scalability, making embedded APIs and microservices the go-to design patterns. In the PHP ecosystem, Symfony stands out as a powerful and mature framework that supports these architectures robustly. One of its key features—middleware—empowers developers to efficiently integrate API layers and service logic into applications. This article introduces the concept of middleware in Symfony and demonstrates how to build embedded APIs and microservice-ready structures.
Middleware in Symfony operates on the chain-of-responsibility pattern: each middleware can modify the request object and then pass it along the chain, culminating in a response. Symfony’s HttpKernel component forms the foundation for this process. By customizing the kernel, developers can insert their own logic into the request-response lifecycle.
An embedded API allows applications to expose endpoints internally or externally for third-party integration. Symfony simplifies API development with tools like API Platform. Here's a basic example:
// UserController.php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\User;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @Route("/api/users")
* @ApiResource
*/
class UserController extends AbstractController
{
/**
* @Route("/{id}", methods={"GET"})
*/
public function getUser(User $user)
{
return $this->json($user);
}
}
In this example, the @ApiResource annotation defines a new API resource. The @Route annotation specifies the endpoint, and the getUser() method returns a JSON representation of the user object—demonstrating a clean RESTful API implementation.
Microservices split an application into independent components, each handling a specific business function. Symfony facilitates microservice development through its middleware support and service container architecture.
Below is an example of a user service that processes a request:
// UserService.php
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class UserService
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function handleRequest(Request $request): Response
{
$userId = $request->get('userId');
// Fetch user data from the database using userId
$userRepository = $this->container->get(UserRepository::class);
$user = $userRepository->find($userId);
// Return JSON response
return new Response(json_encode($user));
}
}
Here, the service uses dependency injection to access the container and retrieve a user from the database, processing the request in a loosely coupled manner aligned with microservice principles.
Symfony’s middleware capabilities offer developers powerful tools to integrate embedded APIs and microservices efficiently. Whether you're exposing services to third-party consumers or decomposing your application for better scalability, middleware in Symfony provides a solid foundation. The examples provided in this article should help guide your implementation in real-world PHP projects.