You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+60-45
Original file line number
Diff line number
Diff line change
@@ -12,15 +12,15 @@
12
12
13
13
## Why Nette-Api
14
14
15
-
This library provides out-of-the box API solution for Nette framework. You can register API endpoints and connect it to specified handlers. You need only implement you custom business logic. Library provide authorisation, validation and formatting services for you api.
15
+
This library provides out-of-the box API solution for Nette framework. You can register API endpoints and connect it to specified handlers. You need only implement you custom business logic. Library provide authorization, validation and formatting services for you API.
16
16
17
17
## Installation
18
18
19
-
This library requires PHP 5.4 or later. It works also on PHP 7.0.
19
+
This library requires PHP 7.1 or later.
20
20
21
21
Recommended installation method is via Composer:
22
22
23
-
```bash
23
+
```bash
24
24
$ composer require tomaj/nette-api
25
25
```
26
26
@@ -35,51 +35,52 @@ Library is compliant with [PSR-1][], [PSR-2][], [PSR-3][] and [PSR-4][].
35
35
36
36
First you have register library presenter for routing. In *config.neon* just add this line:
37
37
38
-
```yaml
38
+
```neon
39
39
application:
40
40
mapping:
41
41
Api: Tomaj\NetteApi\Presenters\*Presenter
42
42
```
43
43
44
44
And add route to you RouterFactory:
45
45
46
-
```php
46
+
```php
47
47
$router[] = new Route('/api/v<version>/<package>[/<apiAction>][/<params>]', 'Api:Api:default');
48
48
```
49
49
50
50
After that you need only register your api handlers to *apiDecider*[ApiDecider](src/ApiDecider.php) and register [ApiLink](src/Link/ApiLink.php) and [Tomaj\NetteApi\Misc\IpDetector](src/Misc/IpDetector.php). This can be done also with *config.neon*:
As you can see in example, you can register as many endpoints as you want with different configurations. Nette-Api support api versioning from the beginning.
64
-
This example will prepare this api calls:
63
+
As you can see in example, you can register as many endpoints as you want with different configurations. Nette-Api supports API versioning from the beginning.
64
+
This example will prepare these API calls:
65
65
66
66
1.`http://yourapp/api/v1/users` - available via GET
67
67
2.`http://yourapp/api/v1/users/send-email` - available via POST
68
68
69
69
70
-
Core of the Nette-Api is handlers. For this example you need implement 2 classes:
70
+
Core of the Nette-Api are handlers. For this example you need implement 2 classes:
71
71
72
72
1. App\MyApi\v1\Handlers\UsersListingHandler
73
73
2. App\MyApi\v1\Handlers\SendEmailHandler
74
74
75
-
This handlers implements interface *[ApiHandlerInterface](src/Handlers/ApiHandlerInterface.php)* but for easier usage you can extends your handler from [BaseHandler](src/Handlers/BaseHandler.php).
76
-
When someone reach your api this handlers will be triggered and *handle()* method will be called.
75
+
These handlers implement interface *[ApiHandlerInterface](src/Handlers/ApiHandlerInterface.php)* but for easier usage you can extends your handlers from [BaseHandler](src/Handlers/BaseHandler.php).
76
+
When someone reach your API this handlers will be triggered and *handle()* method will be called.
77
77
78
-
```php
78
+
```php
79
79
namespace App\MyApi\v1\Handlers;
80
80
81
81
use Tomaj\NetteApi\Handlers\BaseHandler;
82
82
use Tomaj\NetteApi\Response\JsonApiResponse;
83
+
use Tomaj\NetteApi\Response\ResponseInterface;
83
84
84
85
class UsersListingHandler extends Basehandler
85
86
{
@@ -91,7 +92,7 @@ class UsersListingHandler extends Basehandler
91
92
$this->userRepository = $userRepository;
92
93
}
93
94
94
-
public function handle($params)
95
+
public function handle(array $params): ResponseInterface
95
96
{
96
97
$users = [];
97
98
foreach ($this->userRepository->all() as $user) {
@@ -115,7 +116,7 @@ Example with fractal:
115
116
116
117
1. You will need Formater
117
118
118
-
```php
119
+
```php
119
120
namespace App\MyApi\v1\Transformers;
120
121
121
122
use League\Fractal\TransformerAbstract;
@@ -135,11 +136,12 @@ class UserTransformer extends TransformerAbstract
135
136
136
137
2. And this will be your handler:
137
138
138
-
```php
139
+
```php
139
140
namespace App\MyApi\v1\Handlers;
140
141
141
142
use Tomaj\NetteApi\Handlers\BaseHandler;
142
143
use Tomaj\NetteApi\Response\JsonApiResponse;
144
+
use Tomaj\NetteApi\Response\ResponseInterface;
143
145
144
146
class UsersListingHandler extends Basehandler
145
147
{
@@ -151,7 +153,7 @@ class UsersListingHandler extends Basehandler
151
153
$this->userTransformer = $userTransformer;
152
154
}
153
155
154
-
public function handle($params)
156
+
public function handle(array $params): ResponseInterface
155
157
{
156
158
$users = $this->useRepository->all();
157
159
@@ -173,12 +175,13 @@ Each handler can describe which input is required. It could be GET or POST param
173
175
174
176
Example with user detail:
175
177
176
-
```php
178
+
```php
177
179
namespace App\MyApi\v1\Handlers;
178
180
179
181
use Tomaj\NetteApi\Handlers\BaseHandler;
180
182
use Tomaj\NetteApi\Response\JsonApiResponse;
181
183
use Tomaj\NetteApi\Params\InputParam;
184
+
use Tomaj\NetteApi\Response\ResponseInterface;
182
185
183
186
class UsersDetailHandler extends Basehandler
184
187
{
@@ -190,14 +193,14 @@ class UsersDetailHandler extends Basehandler
190
193
$this->userRepository = $userRepository;
191
194
}
192
195
193
-
public function params()
196
+
public function params(): array
194
197
{
195
198
return [
196
199
new InputParam(InputParam::TYPE_GET, 'id', InputParam::REQUIRED),
197
200
];
198
201
}
199
202
200
-
public function handle($params)
203
+
public function handle(array $params): ResponseInterface
@@ -231,18 +234,18 @@ This is table with support input types:
231
234
232
235
## Security
233
236
234
-
Protecting your api is easy with Nette-Api. You have to implement your [Authorization](src/Authorization/ApiAuthorizationInterface.php) (Tomaj\NetteApi\Authorization\ApiAuthorizationInterface) and add it as third argument to *addApiHandler()* method in *config.neon*.
237
+
Protecting your api is easy with Nette-Api. You have to implement your [Authorization](src/Authorization/ApiAuthorizationInterface.php) (Tomaj\NetteApi\Authorization\ApiAuthorizationInterface) and add it as third argument to *addApi()* method in *config.neon*.
235
238
236
239
For simple use, if you want to use Bearer token authorisation with few tokens, you can use [StaticBearerTokenRepository](src/Misc/StaticBearerTokenRepository.php) (Tomaj\NetteApi\Misc\StaticBearerTokenRepository).
0 commit comments