This guide helps you migrate your Restler v5 application to v6.
Restler v6 is a major upgrade with significant improvements:
- PHP 8.0+ required
- Modern async server support (ReactPHP, Swoole, Workerman)
- Enhanced security
- Better performance
- Improved type safety
v5: PHP 7.4+ v6: PHP 8.0+
Action Required:
- Upgrade your PHP version to 8.0 or higher
- Update server configuration
Update composer.json:
{
"require": {
"luracast/restler": "^6.0",
"php": "^8.0"
}
}Then run:
composer updatev6 enforces stricter type safety.
v5 Code:
class UserAPI
{
public function getUser($id)
{
return ['id' => $id];
}
}v6 Code:
class UserAPI
{
public function getUser(int $id): array
{
return ['id' => $id];
}
}Action Required:
- Add parameter type hints
- Add return type declarations
- Ensure types match method signatures
Session serialization changed from serialize/unserialize to json_encode/json_decode.
Impact: Existing sessions will need to be regenerated.
Action Required:
- Plan for session regeneration on deployment
- Or implement a migration script:
// Migration script (one-time)
session_start();
if (isset($_SESSION['old_data'])) {
// Re-save session data to trigger new JSON encoding
$_SESSION['old_data'] = $_SESSION['old_data'];
}Internal reflection code updated for PHP 8 compatibility.
Impact: Should be transparent unless you extended internal classes.
Action Required:
- Test thoroughly if you extended
Container,Routes, or related classes
v6 supports modern PHP 8 attributes alongside traditional PHPDoc annotations.
Traditional (still supported):
/**
* @url GET users/{id}
* @access protected
*/
public function getUser(int $id): array { }New Attribute Style (optional):
#[Get('users/{id}')]
#[Protected]
public function getUser(int $id): array { }Choose your server runtime:
Traditional PHP-FPM:
// public/index.php
use Luracast\Restler\Restler;
use Luracast\Restler\Routes;
Routes::mapApiClasses([UserAPI::class]);
(new Restler)->handle();ReactPHP (Async):
php public/index_react.phpSwoole (High Performance):
php public/index_swoole.phpWorkerman (Event-Driven):
php public/index_workerman.php startMore granular CORS configuration:
use Luracast\Restler\Defaults;
Defaults::$accessControlAllowOrigin = '*';
Defaults::$accessControlAllowCredentials = true;
Defaults::$accessControlMaxAge = 3600; // Now properly configurable
Defaults::$accessControlExposeHeaders = 'X-Custom-Header';Better error messages with more context:
try {
$r->handle();
} catch (\Throwable $e) {
// v6 provides better stack traces and error details
error_log($e->getMessage());
}# Update composer.json
composer require luracast/restler:^6.0
# Install dependencies
composer updateRun through your API classes and add type hints:
# Use PHP_CodeSniffer or similar to find missing types
vendor/bin/phpcs --standard=PSR12 src/Common patterns:
// Before
public function create($data) { }
public function list($page = 1) { }
public function delete($id) { }
// After
public function create(array $data): array { }
public function list(int $page = 1): array { }
public function delete(int $id): bool { }Ensure PHPDoc comments are accurate:
/**
* Create a new user
*
* @param array $data User data
* @return array Created user
*
* @url POST users
* @access protected
*/
public function create(array $data): array
{
// Implementation
}# Run your test suite
vendor/bin/phpunit
# Or use Behat if you have BDD tests
vendor/bin/behatv6 has improved error handling. Update catch blocks:
// Before (v5)
catch (RestException $e) {
// Handle
}
// After (v6)
catch (HttpException $e) {
// More specific exception types
}- Test in staging environment first
- Clear cache directory:
rm -rf cache/* - Regenerate routes in production mode
- Monitor for any runtime errors
- Plan for session migration (users may need to re-login)
Cause: Autoloader cache not updated
Solution:
composer dump-autoloadCause: Missing type declarations
Solution: Add proper type hints to all parameters and return types
Cause: Session format changed from serialize to JSON
Solution: Clear session storage or implement migration
Cause: CORS configuration changed
Solution: Update Defaults configuration:
Defaults::$accessControlAllowOrigin = 'your-domain.com';
Defaults::$accessControlAllowCredentials = true;Cause: Production mode cache not cleared
Solution:
rm cache/routes.php$r = new Restler(true); // Enable production mode; php.ini
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=20000For high-traffic APIs, consider Swoole or ReactPHP:
# Swoole can handle 10x more requests
php public/index_swoole.phpDefaults::$compressionEnabled = true;| Feature | v5 | v6 |
|---|---|---|
| PHP 7.4 | ✅ | ❌ |
| PHP 8.0 | ✅ | ✅ |
| PHP 8.1 | ✅ | ✅ |
| PHP 8.2 | ✅ | |
| PHP 8.3 | ❌ | ✅ |
| Attributes | ❌ | ✅ |
| ReactPHP | ✅ | |
| Swoole | ✅ | |
| Workerman | ❌ | ✅ |
| AWS Lambda | ❌ | ✅ |
| GraphQL | ❌ | ✅ |
| Excel Export | ❌ | ✅ |
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: See other .md files in this repository
If you need to rollback to v5:
# In composer.json
{
"require": {
"luracast/restler": "^5.0"
}
}
composer updateClear cache and restart servers.
Note: We recommend thorough testing in a staging environment before migrating production systems.