Zero-boilerplate REST API framework for PHP 8+. Auto-generates routes, docs, validation.
Zero-boilerplate REST API framework for PHP 8+. Auto-generates routes, docs, validation.
Production-ready since 2010. Battle-tested. Now rebuilt for modern PHP 8+ with async superpowers.
use Luracast\Restler\Restler;
use Luracast\Restler\Routes;
class Products {
function get(int $id): array {
return Database::findProduct($id);
}
}
Routes::mapApiClasses([Products::class]);
(new Restler)->handle();That's it. You just created a REST API that:
$id as an integerGET /products/123)No controllers. No routing files. No configuration. Just PHP.
While other frameworks make you write controllers, routes, DTOs, validation rules, and transformers—Restler uses PHP reflection to do it all automatically. Write business logic, not plumbing.
// Laravel/Symfony: 50+ lines of controllers, routes, requests, resources
// Restler: 5 lines
class Users {
function create(string $email, string $name): User {
return User::create(compact('email', 'name'));
}
}Run on Swoole/OpenSwoole for 10-20x throughput vs traditional PHP-FPM. Or use ReactPHP for true async I/O. Deploy to AWS Lambda for serverless scale.
# Traditional PHP-FPM: ~1,000 req/sec
# Swoole/OpenSwoole: ~15,000 req/sec (same code!)
composer swoole-serverJSON is the default format. Configure additional formats (XML, CSV, Excel, HTML) via Routes::setOverridingResponseMediaTypes(). Perfect for:
curl api.example.com/products/123 # JSON (default)
curl api.example.com/products/123.xml # XML (if configured)
curl api.example.com/products.csv # CSV (if configured)
curl api.example.com/products.xlsx # Excel (if configured)Built for PHP 8 with attributes, union types, named arguments, and strict typing. PSR-7 and PSR-11 compliant.
use Luracast\Restler\Attribute\{Get, Post};
class Orders {
#[Get('orders/{id}')]
function getOrder(int $id): Order|null {
return Order::find($id);
}
#[Post('orders')]
function createOrder(string $product, int $quantity = 1): Order {
return Order::create(compact('product', 'quantity'));
}
}OpenAPI 3.0 (Swagger) docs generated from your PHPDoc comments. Interactive API explorer included.
class Products {
/**
* Get product details
*
* @param int $id Product ID
* @return Product product information
* @throws 404 Product not found
*/
function get(int $id): Product {
return Product::findOrFail($id);
}
}
// Visit /explorer for interactive Swagger UIPerfect for building internal APIs that need to integrate with various systems. Multi-format support means you can serve JSON to your React app and XML to that ancient CRM system—from the same endpoint.
Low latency on Swoole, automatic validation, built-in rate limiting, and OAuth2 support. Everything you need for a production mobile backend.
Built-in CSV and Excel streaming support. Export millions of rows without running out of memory using generators.
function exportUsers(): Generator {
foreach (User::cursor() as $user) {
yield $user->toArray();
}
}
// GET /users.csv streams all users as CSV
// GET /users.xlsx downloads Excel fileNeed to modernize an old PHP app? Add Restler to get a REST API instantly. Works alongside existing code—no rewrite needed.
composer require luracast/restler:^6.01. API Class (api/Hello.php)
<?php
class Hello {
function sayHello(string $name = 'World'): string {
return "Hello, $name!";
}
function getTime(): array {
return ['time' => date('Y-m-d H:i:s'), 'timezone' => date_default_timezone_get()];
}
}2. Gateway (public/index.php)
<?php
require __DIR__ . '/../vendor/autoload.php';
Luracast\Restler\Routes::mapApiClasses([Hello::class]);
(new Luracast\Restler\Restler)->handle();3. URL Rewriting (.htaccess or nginx.conf)
# Apache
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]# Start server
php -S localhost:8080 -t public
# Try your API
curl http://localhost:8080/hello/sayHello/Restler
# Output: "Hello, Restler!"
curl http://localhost:8080/hello/getTime
# Output: {"time":"2025-01-15 10:30:45","timezone":"UTC"}
curl http://localhost:8080/hello/getTime.xml
# Output: <?xml version="1.0"?><response><time>2025-01-15 10:30:45</time>...That's it! You have a working REST API with automatic routing, validation, and multi-format support.
Standard deployment. Works everywhere. ~1,000-2,000 req/sec depending on hardware.
// Enable production mode for route caching
$api = new Restler(productionMode: true);10-20x faster than PHP-FPM. Persistent connections, coroutines, built-in HTTP server.
# Install extension (choose one)
pecl install swoole # Original
pecl install openswoole # Fork with same API
# Run server
php public/index_swoole.phpBenchmarks: 15,000+ req/sec on modest hardware (vs ~1,000 for PHP-FPM)
True non-blocking async operations. Perfect for I/O-heavy workloads (database, HTTP calls, etc.).
composer react-serverZero-downtime deploys, automatic scaling, pay-per-request pricing.
# Uses Bref for Laravel-style Lambda deployment
vendor/bin/bref deploySee public/index_lambda.php for complete example.
Alternative to Swoole with pure PHP implementation (no extension required).
php public/index_workerman.php start#[Get], #[Post], etc.)string|int, User|null)unserialize())All formats work automatically—just add file extension to URL:
// v1/Users.php
namespace v1;
class Users {
function get(int $id): array {
return ['id' => $id, 'name' => 'John'];
}
}
// v2/Users.php
namespace v2;
class Users {
function get(int $id): User {
return User::with('profile')->find($id);
}
}
// index.php
use Luracast\Restler\Routes;
Routes::mapApiClasses([
'v1/users' => 'v1\\Users',
'v2/users' => 'v2\\Users'
]);Usage:
curl api.example.com/v1/users/123 # Old format
curl api.example.com/v2/users/123 # New format with profileuse Luracast\Restler\Contracts\AuthenticationInterface;
class ApiKeyAuth implements AuthenticationInterface {
public function __isAuthenticated(): bool {
$key = $_SERVER['HTTP_X_API_KEY'] ?? null;
return $key && ApiKey::validate($key);
}
}
use Luracast\Restler\Restler;
use Luracast\Restler\Routes;
Routes::addAuthenticator(ApiKeyAuth::class);
Routes::setFilters(RateLimit::class); // Built-in rate limiting
Routes::mapApiClasses([ProtectedAPI::class]);
(new Restler)->handle();…Automatic REST endpoints:
GET /products → index()POST /products → post()GET /products/123 → (auto-routes to index with $id)PUT /products/123 → put()DELETE /products/123 → delete()class Media {
/**
* Upload file
*
* @param array $file Upload file {@from body} {@type file}
*/
function post(array $file): array {
$path = Storage::put('uploads', $file['tmp_name']);
return [
'filename' => $file['name'],
'url' => Storage::url($path),
'size' => $file['size']
];
}
}class Reports {
/**
* Export all users (memory efficient)
*/
function exportUsers(): Generator {
// Processes millions of rows without memory issues
foreach (User::cursor() as $user) {
yield [
'id' => $user->id,
'email' => $user->email,
'created' => $user->created_at
];
}
}
}
// GET /reports/exportUsers.csv → streams CSV
// GET /reports/exportUsers.xlsx → streams Excel…use Luracast\Restler\Restler;
use Luracast\Restler\Routes;
use Luracast\Restler\OpenApi3\Explorer;
Routes::mapApiClasses([
'graphql' => GraphQLEndpoint::class,
'explorer' => Explorer::class
]);
(new Restler)->handle();
// POST /graphql
// Query and mutation support built-inuse Luracast\Restler\Defaults;
Defaults::$accessControlAllowOrigin = 'https://app.example.com';
Defaults::$accessControlAllowMethods = 'GET, POST, PUT, DELETE';
Defaults::$accessControlAllowHeaders = 'Content-Type, Authorization';
Defaults::$accessControlMaxAge = 86400;
use Luracast\Restler\Restler;
use Luracast\Restler\Routes;
Routes::mapApiClasses([MyAPI::class]);
(new Restler)->handle();Restler includes a built-in Swagger UI explorer:
use Luracast\Restler\Restler;
use Luracast\Restler\Routes;
use Luracast\Restler\OpenApi3\Explorer;
Routes::mapApiClasses([
Products::class,
Users::class,
'explorer' => Explorer::class // Add explorer
]);
(new Restler)->handle();Visit http://localhost:8080/explorer to:
The explorer is auto-generated from your PHPDoc comments—no manual work needed.
Restler includes 18+ working examples and a full Behat test suite.
No open issues yet, or sync has not completed.