laminas/laminas-server
Laminas Server provides server-side components for building web service APIs in PHP, including SOAP and JSON-RPC. Define services, handle requests, and generate responses with flexible dispatching and integration options for legacy and modern applications.
Installation
composer require laminas/laminas-server:^2.19.0
Add to composer.json under require-dev if only for testing.
Note: PHP 8.5 is now required; PHP 8.1 support has been dropped.
First Use Case: Basic RPC Server Define a service class with annotated methods (unchanged):
namespace App\Services;
use Laminas\Server\Annotation\Rpc;
class CalculatorService
{
/**
* @Rpc
*/
public function add(int $a, int $b): int
{
return $a + $b;
}
}
Bootstrap the Server (unchanged)
use Laminas\Server\Server;
use Laminas\Server\Reflection\ReflectionService;
$server = new Server();
$reflectionService = new ReflectionService();
$server->addService($reflectionService->getService('App\Services\CalculatorService'));
$server->run();
Test via CLI or HTTP (unchanged)
curl -X POST http://localhost:8080/rpc -d '{"method":"add","params":[2,3]}'
Service Definition (unchanged)
@Rpc for public methods; @Rpc\Param and @Rpc\Return for type hints.Services/ directory with clear namespaces.Request Handling (unchanged)
Laminas\Server\JsonRpc\JsonRpcServer for customization.laminas/laminas-http for middleware/routing:
$request = new \Laminas\HttpPhpEnvironment\Request();
$response = new \Laminas\HttpPhpEnvironment\Response();
$server->handle($request, $response);
Authentication/Authorization (unchanged)
Laminas\Server\Server to validate requests:
$server->setAuthService(new CustomAuthService());
Error Handling (unchanged)
Laminas\Server\Exception\ExceptionStrategy for custom error responses.$app->singleton(Server::class, function ($app) {
$server = new Server();
$server->addService(app(ReflectionService::class)->getService('App\Services\*'));
return $server;
});
public function __construct(private LoggerInterface $logger) {}
PHP Version Compatibility
composer update laminas/laminas-server and verify PHP version:
php -v
Reflection Overhead (unchanged)
ReflectionService instances.ReflectionService::setCacheDir(__DIR__.'/cache').Type Safety (unchanged)
@Rpc\Param(type="int") explicitly.Circular Dependencies (unchanged)
$server->setLogger(new \Monolog\Logger('rpc', [new \Monolog\Handler\StreamHandler('php://stderr')]));
laminas/laminas-code to validate annotations pre-deployment:
./vendor/bin/laminas-code validate --annotations App\Services
Custom Protocols (unchanged)
Extend Laminas\Server\Server to support GraphQL, gRPC, etc.:
class CustomServer extends Server {
public function handleGraphQL(\Psr\Http\Message\RequestInterface $request) { ... }
}
Middleware (unchanged) Integrate with Laravel middleware:
$server->pipe(new class implements \Psr\Http\Server\RequestHandlerInterface {
public function handle(\Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response) {
// Pre-process request
return $next($request, $response);
}
});
Performance (unchanged)
Laminas\Server\Batch\BatchServer for bulk RPC calls.$reflectionService->setCacheEnabled(true);
How can I help you explore Laravel packages today?