Skip to content

Commit

Permalink
add Json.md
Browse files Browse the repository at this point in the history
  • Loading branch information
easy-system committed Jun 14, 2016
1 parent 4032c79 commit c8689de
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/005.Json.md
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);
}
}
```

0 comments on commit c8689de

Please sign in to comment.