Installation:
composer require bankiru/rpc-server-bundle
Add to config/app.php under providers:
Bankiru\RpcServerBundle\BankiruRpcServerBundle::class,
Register the bundle in config/bundles.php (Symfony 4+):
return [
// ...
Bankiru\RpcServerBundle\BankiruRpcServerBundle::class => ['all' => true],
];
First Use Case:
Define a controller extending Bankiru\RpcServerBundle\Controller\RpcController:
namespace App\Controller;
use Bankiru\RpcServerBundle\Controller\RpcController;
use Symfony\Component\HttpFoundation\Request;
class MyRpcController extends RpcController
{
public function indexAction(Request $request)
{
$method = $request->query->get('method');
$params = $request->query->get('params');
return $this->handleRpcRequest($method, $params);
}
}
Route it in routes.yaml:
rpc:
path: /api/rpc
controller: App\Controller\MyRpcController::indexAction
First Request: Send a JSON-RPC request via HTTP:
curl -X POST http://your-app/api/rpc \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"myMethod","params":{"key":"value"},"id":1}'
Request Handling:
RpcController and override handleRpcRequest() to define custom logic.$request->getContent() to parse incoming JSON/XML payloads.JsonSchema or manual checks.Method Routing:
/api/rpc?method=foo).public function handleRpcRequest($method, $params)
{
switch ($method) {
case 'getUser':
return $this->getUser($params['id']);
case 'createOrder':
return $this->createOrder($params);
default:
throw new \RuntimeException('Method not found');
}
}
Response Formatting:
return [
'jsonrpc' => '2.0',
'result' => $data,
'id' => $request->get('id'),
];
return [
'jsonrpc' => '2.0',
'error' => [
'code' => -32601,
'message' => 'Method not found',
],
'id' => $request->get('id'),
];
Integration with Laravel Services:
UserRepository) into the controller:
public function __construct(private UserRepository $users) {}
public function getUser($id) {
return $this->users->find($id);
}
Middleware Integration:
Route::middleware(['auth:sanctum'])->post('/api/rpc', [MyRpcController::class, 'index']);
Deprecation Risk:
No Built-in Validation:
$method and $params to avoid injection risks:
if (!is_string($method) || empty($method)) {
throw new \InvalidArgumentException('Invalid method');
}
HTTP vs. RPC Mismatch:
StreamedResponse for large payloads.Configuration Quirks:
$this->setRpcNamespace('App\\Rpc'); // Custom namespace for methods.
Logging:
\Log::debug('RPC Request', ['method' => $method, 'params' => $params]);
Error Handling:
try {
return $this->getUser($id);
} catch (\Exception $e) {
return [
'error' => ['code' => -32000, 'message' => $e->getMessage()],
];
}
Testing:
RpcController in PHPUnit:
$controller = $this->getMockBuilder(MyRpcController::class)
->onlyMethods(['getUser'])
->getMock();
$controller->method('getUser')->willReturn(['id' => 1]);
Custom Protocols:
Bankiru\RpcServerBundle\Rpc\RpcInterface to support SOAP/XML-RPC:
class CustomRpcProtocol implements RpcInterface {
public function parse(Request $request) { /* ... */ }
public function format($data) { /* ... */ }
}
Authentication:
handleRpcRequest():
if (!$this->isAuthenticated($request)) {
throw new \RuntimeException('Unauthorized');
}
Performance:
getUser):
$cacheKey = "rpc_user_{$id}";
return Cache::remember($cacheKey, 3600, function() use ($id) {
return $this->users->find($id);
});
Documentation:
/**
* @OA\Post(
* path="/api/rpc",
* summary="Get user by ID",
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(ref="#/components/schemas/RpcRequest")
* )
* )
*/
How can I help you explore Laravel packages today?