packeto / rest-router
REST router extension for Nette Framework with simple annotation features.
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 1 370
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >= 7.1.0
- doctrine/annotations: ~1.6
- doctrine/cache: ^1.7.1
- league/json-guard: ^1.0
- nette/application: ^2.3
- nette/di: ^2.3
- nette/http: ^2.3
- nette/reflection: ^2.3
- nette/utils: ^2.3
README
Basic extension for REST API routing, with annotations helpers.
Installation
Install dependency over Composer:
composer require packeto/rest-router
Register extension in configuration file:
extensions: apirouter: Packeto\RestRouter\DI\RestRouterExtension
Configure JSON Schemas directory:
apirouter: jsonSchemasPath: %appDir%/../json-schema
Usage
ResourcePresenter
Routes are defined in Presenter which extends from ResourcePresenter
provided by extension:
/** * @ApiRoute( * "/api/v1/hello", * presenter="Api:Hello" * ) */ class HelloPresenter extends ResourcePresenter { /** * @Authenticated * @JsonSchema("users/hello.json") * @DTO("API\Commands\DTO\HelloDTO") */ public function actionCreate() { // Instance of API\Commands\DTO\HelloDTO $dto = $this->getCommandDTO(); $this->sendSuccessResponse(); } }
JSON Schema validation
Request (POST
, PUT
, PATCH
) can be validated with JSON schema. You must have cofingured jsonSchemasPath
in your config.
Then create JSON Schema file in this folder, e.g. test.json
and method annotation place this file name:
@JsonSchema("test.json")
And befero execution, input will be validated. Simple.
Request Authentication
Extensions provides you @Authenticated
annotation, which authenticate every request pointing on endpoind. You must implement your own logic by implementing IAuthenticator
.
Example of implementation:
use Nette\Http\IRequest; use Packeto\RestRouter\Exception\Api\AuthenticationException; use Packeto\RestRouter\Security\IAuthenticator; class Authenticator implements IAuthenticator { private const AUTHENTICATION_HEADER = 'X-Api-Key'; /** * @param IRequest $request * @return void * @throws AuthenticationException */ public function authenticate(IRequest $request): void { if (is_null($request->getHeader(self::AUTHENTICATION_HEADER, null))) { throw new AuthenticationException( sprintf('Header %s is empty, or is not provided.', self::AUTHENTICATION_HEADER), AuthenticationException::MISSING_HEADER_VALUE ); } return; } }
Then register your API authenticator as service in your configuration.