Download the PHP package kelemen/api-nette without Composer
On this page you can find all versions of the php package kelemen/api-nette. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package api-nette
Api Nette
Highly customizable and easy to setup REST api handling for Nette framework.
Installation
Prepare to use
- First of all you need an Api presenter for handle api requests. You can use
Kelemen\ApiNette\Presenter\ApiPresenter
or write you own. -
Register new mapping in config.neon
-
Add api route to router. We use keyword api for identify api requests.
- Configure Api (example from config.neon)
Api routes
Add routes to api
REST api routes can be defined with shortcut functions (for most used HTTP methods):
- get
- post
- put
- patch
- delete
- options
Or you can add any HTTP method processing with add($method, $pattern, $handler, $params = [])
function.
Route patterns
In route definition you can use regular patterns or define parameters closed in < and >. Routes are evaluated in order they was added. So define specific routes first.
Route handlers
Route definition use lazy loading from Nette DI Container. Handlers are defined by type or by name registered in config.neon file. For name definition, prefix service name with #.
Route parameters
As parameter is now accepted only middleware key. Middleware definition use same lazy loading logic as handlers.
Handler
Handler provide business logic for resolved api route.
Validations
IMPORTANT! Every parameter is parsed as string! So use numeric validation rule instead of integer or float.
Primary validation is handled by Nette Validators.
Validations are registered in handlers validate()
function. All validated input parameters are accessible in handler via $this->values
array.
If any validation failed, api automatically send response with 400 HTTP code with all validation errors.
Custom validation rules
By default you don't need to register new Validator instance to Api. But if you want register new validations or override existing validations you need to create and configure your own Validator instance.
Inputs
Validator has defined set of default inputs
Keyword | Description |
---|---|
get | $_GET |
post | $_POST |
cookie | $_COOKIES |
file | $_FILES |
postRaw | file_get_contents("php://input") |
json | json_decode(file_get_contents("php://input"), true) |
path | Parsed params from matched route |
If you want some special input you can add this input to Validator with setInput($name, InputInterface $input)
function.
Middleware
Api flow can be extended by middleware. Middleware interface has only one function __invoke(Request $request, Response $response, callable $next)
.
How middleware works:
If we have 3 middlewares registered as
Flow will look like:
Flow and Exceptions
Library is shipped with default api presenter Kelemen\ApiNette\Presenter\ApiPresenter
. This presenter running api and handle all exceptions (create response depends on catched exception).
If you want custom error responses, create and register your own presenter.
Api throws this exceptions
Exception | Description |
---|---|
Kelemen\ApiNette\Exception\ApiNetteException | Base parent exception |
Kelemen\ApiNette\Exception\UnresolvedHandlerException | Handler registered for resolved route doesn't exists in container |
Kelemen\ApiNette\Exception\UnresolvedMiddlewareException | Middleware registered for resolved route doesn't exists in container |
Kelemen\ApiNette\Exception\UnresolvedRouteException | None of the registered routes match given url |
Kelemen\ApiNette\Exception\ValidationFailedException | Some of registered validations failed |
Kelemen\ApiNette\Exception\ValidatorException | Input type use in validation is not registered in validator |
Kelemen\ApiNette\Exception\MiddlewareException | Middleware configuration/processing exception |
Base Implementations
Middleware
CORSMiddleware
Setup Access-Control-Allow-Origin and Access-Control-Allow-Credentials headers. Middleware has 3 modes:
- all - returns allow-origin as "*". Credentials header is disabled by standard.
- mirror - returns request header "Origin" in allow-origin and credentials header can be configured manually.
- custom - allow-origin header has to be configured and credentials header can be configured manually.
Handler
OptionsPreflightHandler
Configurable handler for browser CORS preflight request. Configurable response headers:
- Access-Control-Max-Age
- Access-Control-Expose-Headers
- Access-Control-Allow-Methods
- Access-Control-Allow-Headers