-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4032c79
commit c8689de
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Json | ||
==== | ||
|
||
The plugin provides easy work with json. | ||
Method `encode()` returns the instance of `Psr\Http\Message\ResponseInterface` | ||
with encoded data in body of the response: | ||
``` | ||
use Es\ControllerPlugins\PluginsTrait; | ||
class ExampleController | ||
{ | ||
use PluginsTrait; | ||
public function fooAction() | ||
{ | ||
$data = [ | ||
'foo' => 'bar', | ||
'bat' => 'baz', | ||
]; | ||
return $this->json()->encode($data); | ||
} | ||
} | ||
``` | ||
|
||
The `decode` method returns the decoded data. On error is thrown exception. | ||
``` | ||
use Es\ControllerPlugins\PluginsTrait; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
class ExampleController | ||
{ | ||
use PluginsTrait; | ||
public function barAction(Request $request) | ||
{ | ||
$body = (string) $request->getBody(); | ||
/* if unable to decode the content of request, the exception will be | ||
* thrown. In this case, the system itself must handle the exception | ||
* standard manner. | ||
*/ | ||
$data = $this->json()->decode($body); | ||
} | ||
} | ||
``` |