Package Data | |
---|---|
Maintainer Username: | kbaryshnikov |
Package Create Date: | 2015-11-02 |
Package Last Update: | 2024-06-26 |
Home Page: | |
Language: | PHP |
License: | BSD-2-Clause |
Last Refreshed: | 2024-11-22 03:04:01 |
Package Statistics | |
---|---|
Total Downloads: | 22,640 |
Monthly Downloads: | 317 |
Daily Downloads: | 7 |
Total Stars: | 50 |
Total Watchers: | 4 |
Total Forks: | 10 |
Total Open Issues: | 0 |
composer require upgate/laravel-jsonrpc
// ...
use Upgate\LaravelJsonRpc\Contract\ServerInterface as JsonRpcServerContract;
class RouteServiceProvider extends ServiceProvider
{
// ...
public function map(Router $router)
{
$router->group(
['namespace' => $this->namespace],
function (Router $router) {
// Create an instance of JsonRpcServer
$jsonRpcServer = $this->app->make(JsonRpcServerContract::class);
// Set default controller namespace
$jsonRpcServer->setControllerNamespace($this->namespace);
// Register middleware aliases configured for Laravel router
$jsonRpcServer->registerMiddlewareAliases($router->getMiddleware());
require app_path('Http/routes.php');
}
);
}
}
$router->post('/jsonrpc', function (Illuminate\Http\Request $request) use ($jsonRpcServer) {
$jsonRpcServer->router()
->addMiddlewares(['fooMiddleware', 'barMiddleware']) // middleware alias names or class names
->bindController('foo', 'FooController') // for 'foo.$method' methods invoke FooController->$method(),
// for 'foo' method invoke FooConroller->index()
->bind('bar', 'MyController@bar') // for 'bar' method invoke MyController->bar()
->group(
['bazMiddleware'], // add bazMiddleware for methods in this group
function ($jsonRpcRouter) {
// for 'bar.baz' method invoke MyController->bazz()
$jsonRpcRouter->bind('bar.baz', 'MyController@bazz');
}
);
// Run json-rpc server with $request passed to middlewares as a handle() method argument
return $jsonRpcServer->run($request);
});
See ServerTest
and RouterTest
for more examples.
Descendants of Upgate\LaravelJsonRpc\Exception\JsonRpcException
represent JSON-RPC errors according to the spec (see Upgate\LaravelJsonRpc\Server\ErrorCode
).
Other exceptions are logged and converted to INTERNAL_ERROR responses by default.
Use Upgate\LaravelJsonRpc\Server\Server::onException()
to register custom exception handlers:
use Upgate\LaravelJsonRpc\Server;
$jsonRpcServer->onException(
SomeExceptionClass::class,
function (SomeExceptionClass $e, Server\Request $request) {
$message = "Some Message";
$code = -32099;
return Server\RequestResponse::constructErrorResponse($request->getId(), $message, $code);
}
);
$jsonRpcServer->onException(
\Exception::class, // catch-all
function (\Exception $e, Server\Request $request) {
$message = "Some Other Message";
$code = -32098;
return Server\RequestResponse::constructErrorResponse($request->getId(), $message, $code);
}
);
See ServerTest:: testSingleRequestWithExceptionHandler()
and ServerTest::testExceptionHandlersPriority()
for more examples.
(since v0.3.0)
Upgate\LaravelJsonRpc\Server\FormRequest
is similar to Laravel form requests, but validates JSON-RPC parameters instead.
Example:
use Upgate\LaravelJsonRpc\Server\FormRequest as JsonRpcFormRequest;
class MyJsonRpcFormRequest extends JsonRpcFormRequest
{
public function rules(): array
{
return [
'id' => 'required|numeric',
'email' => 'required|email',
];
}
}
class MyController
{
public function myAction(MyJsonRpcFormRequest $jsonRpcRequest)
{
$email = $jsonRpcRequest->email; // or $jsonRpcRequest->get('email');
$allParams = $jsonRpcRequest->all();
// ...
}
}
If validation fails, the INVALID_PARAMS error response is returned, with validation error details in the (non-standard) validation_errors
object member.
See the ServerFormRequestTest
for more examples.
The library requires PHP 7.1 since v0.2.0. Unfortunately, PHP version constraint in composer.json was wrong in versions 0.2.0 - 0.2.1.
If you're using PHP 7.0, require "upgate/laravel-jsonrpc": "^0.1"
in your composer.json.