tourze / json-rpc-http-direct-call-bundle
JsonRPC另外一种调用方式
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/json-rpc-http-direct-call-bundle
Requires
- php: ^8.1
- ext-json: *
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/routing: ^6.4
- symfony/uid: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/backtrace-helper: 0.1.*
- tourze/json-rpc-core: 0.0.*
- tourze/json-rpc-encrypt-bundle: 0.1.*
- tourze/json-rpc-endpoint-bundle: 0.1.*
- tourze/symfony-routing-auto-loader-bundle: 0.0.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
README
A Symfony bundle that provides HTTP direct call functionality for JSON-RPC services. This bundle offers an alternative approach to JSON-RPC invocation through HTTP endpoints.
Features
- HTTP direct calls to JSON-RPC services
- Request/response encryption and decryption support
- Multiple calling interface forms
- Automatic route loading with attributes
- Built-in error handling and logging
- Support for prefixed and non-prefixed endpoints
Requirements
- PHP 8.2 or higher
- Symfony 7.3 or higher
- ext-json
Installation
composer require tourze/json-rpc-http-direct-call-bundle
Configuration
Add the bundle to your config/bundles.php file:
return [ // ... Tourze\JsonRPCHttpDirectCallBundle\JsonRPCHttpDirectCallBundle::class => ['all' => true], ];
Quick Start
Direct Call Endpoint
Send POST requests to JSON-RPC methods using these endpoints:
# With prefix POST /json-rpc/{prefix}/{method}.aspx # Without prefix (CP endpoint) POST /cp/json-rpc/{method}.aspx
Example request:
curl -X POST http://localhost/json-rpc/user/getInfo.aspx \ -H "Content-Type: application/json" \ -H "request-id: unique-request-id" \ -d '{"userId": 123}'
Direct POST Endpoint
Send POST requests directly to methods:
POST /json-rpc/call/{method}
Example request:
curl -X POST http://localhost/json-rpc/call/getUser \
-d "userId=123&includeProfile=true"
Usage
Controllers
The bundle provides two main controllers:
- DirectCallController - Handles requests to
/json-rpc/{prefix}/{method}.aspxand/cp/json-rpc/{method}.aspx - DirectPostController - Handles requests to
/json-rpc/call/{method}
Encryption Support
The bundle supports request/response encryption when the encryptor service detects encrypted endpoints:
// Automatic encryption detection based on request path if ($this->encryptor->shouldEncrypt($request)) { $decryptedContent = $this->encryptor->decryptByRequest($request, $content); // Process decrypted content... }
Request ID Handling
The bundle automatically handles request IDs:
- Uses
request-idheader if provided - Generates UUID v4 if not provided
- Prefixes with endpoint prefix for tracking
Architecture
src/
├── Controller/
│ ├── DirectCallController.php # Main JSON-RPC endpoint controller
│ └── DirectPostController.php # Direct POST endpoint controller
├── DependencyInjection/
│ └── JsonRPCHttpDirectCallExtension.php # Service configuration
├── Exception/
│ └── UnexpectedControllerException.php # Custom exceptions
├── Service/
│ └── AttributeControllerLoader.php # Route auto-loader
└── JsonRPCHttpDirectCallBundle.php # Bundle class
Testing
Run tests from the project root directory:
./vendor/bin/phpunit packages/json-rpc-http-direct-call-bundle/tests
Test coverage includes:
- Unit tests for all components
- Integration tests for complete workflows
- Mock services for dependencies
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
The MIT License (MIT). Please see License File for more information.