PHPackages                             alex-kalanis/restful - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. alex-kalanis/restful

ActiveLibrary[HTTP &amp; Networking](/categories/http)

alex-kalanis/restful
====================

Nette REST API bundle

v1.0.0(1y ago)03BSD-3-ClausePHPPHP &gt;= 8.1.0

Since Dec 26Pushed 1y ago1 watchersCompare

[ Source](https://github.com/alex-kalanis/restful)[ Packagist](https://packagist.org/packages/alex-kalanis/restful)[ RSS](/packages/alex-kalanis-restful/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (17)Versions (2)Used By (0)

Nette REST API
==============

[](#nette-rest-api)

[![Build Status](https://github.com/alex-kalanis/restful/actions/workflows/code_checks.yml/badge.svg)](https://github.com/alex-kalanis/restful/actions/workflows/code_checks.yml/badge.svg)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/03df43358490d39b45c423275d14e1b15bc252ca7827776e61c0aad65a817212/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c65782d6b616c616e69732f7265737466756c2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/alex-kalanis/restful/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/94dd19e264afaf09756ed21f56b8e23c3c391e7974c561d22fd548969849d922/68747470733a2f2f706f7365722e707567782e6f72672f616c65782d6b616c616e69732f7265737466756c2f762f737461626c652e7376673f763d31)](https://packagist.org/packages/alex-kalanis/restful)[![Minimum PHP Version](https://camo.githubusercontent.com/183804d09fec16ca7b6209b007250b7d8db1b915042feb093a9f20e6e1f25359/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e312d3838393242462e737667)](https://php.net/)[![Downloads](https://camo.githubusercontent.com/25668cdb0c99ee11faa3694a3c8349c1e68527ea85807143f8f5ac3fcffddcab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616c65782d6b616c616e69732f7265737466756c2e7376673f7631)](https://packagist.org/packages/alex-kalanis/restful)[![License](https://camo.githubusercontent.com/b607605b2685f4a4b03ffe59b20ffa3734adf2c707d8df19638f1b234270d0a7/68747470733a2f2f706f7365722e707567782e6f72672f616c65782d6b616c616e69732f7265737466756c2f6c6963656e73652e7376673f763d31)](https://packagist.org/packages/alex-kalanis/restful)[![Code Coverage](https://camo.githubusercontent.com/3bae154cfcd35474c86a8100c94f69c95278c86ea541ee87807754779a5f2eba/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c65782d6b616c616e69732f7265737466756c2f6261646765732f636f7665726167652e706e673f623d6d617374657226763d31)](https://scrutinizer-ci.com/g/alex-kalanis/restful/?branch=master)

This is repository for adding RestAPI into Nette. Fork of older Drahak repository with refactor to run on php 8.1+.

The main difference is in directory structure, namespaces, tests, static analysis and dependency check.

### Content

[](#content)

- [Requirements](#requirements)
- [Installation &amp; setup](#installation--setup)
- [Neon configuration](#neon-configuration)
- [Sample usage](#sample-usage)
- [Simple CRUD resources](#simple-crud-resources)
- [Accessing input data](#accessing-input-data)
- [Input data validation](#input-data-validation)
- [Error presenter](#error-presenter)
- [Security &amp; authentication](#security--authentication)
- [Secure your resources with OAuth2](#secure-your-resources-with-oauth2)
- [JSONP support](#jsonp-support)
- [Utilities that make life better](#utilities-that-make-life-better)

Requirements
------------

[](#requirements)

This repository requires PHP version 8.1.0 or higher. The production dependencies are [Nette framework 3.2.x](http://www.nette.org) and my OAuth2 provider (see [Secure your resources with OAuth2](#secure-your-resources-with-oauth2)) - everything has been solved by Composer.

Installation &amp; setup
------------------------

[](#installation--setup)

The easiest way is to use [Composer](http://doc.nette.org/en/composer)

```
$ composer require alex-kalanis/restful

```

Then register the extension by adding this code to `bootstrap.php` (before creating container):

```
kalanis\Restful\DI\RestfulExtension::install($configurator);
```

or register it in `config.neon`:

```
extensions:
  restful: kalanis\Restful\DI\RestfulExtension
```

Neon configuration
------------------

[](#neon-configuration)

You can configure alex-kalanis\\Restful library in config.neon in section `restful`:

```
restful:
	convention: 'snake_case'
	cacheDir: '%tempDir%/cache'
	jsonpKey: 'jsonp'
	prettyPrintKey: 'pretty'
	routes:
		generateAtStart: FALSE
		prefix: resources
		module: 'RestApi'
		autoGenerated: TRUE
		panel: TRUE
	mappers:
		myMapper:
			contentType: 'multipart/form-data'
			class:  \App\MyMapper
	security:
		privateKey: 'my-secret-api-key'
		requestTimeKey: 'timestamp'
		requestTimeout: 300
```

- `cacheDir`: not much to say, just directory where to store cache
- `jsonpKey`: sets query parameter name, which enables [JSONP envelope mode](#jsonp-support). Set this to FALSE if you wish to disable it.
- `prettyPrintKey`: API prints every resource with pretty print by default. You can use this query parameter to disable it
- `convention`: resource array keys conventions. Currently supported 3 values: `snake_case`, `camelCase` &amp; `PascalCase` which automatically converts resource array keys. You can write your own converter. Just implement `kalanis\Restful\Resource\IConverter` interface and tag your service with `restful.converter`.
- `routes.generateAtStart`: generating routes at start of Router (**only for auto generated routes and if is Router set over config.neon**)
- `routes.prefix`: mask prefix to resource routes (**only for auto generated routes**)
- `routes.module`: default module to resource routes (**only for auto generated routes**)
- `routes.autoGenerated`: if `TRUE` the library auto generate resource routes from Presenter action method annotations (see below)
- `routes.panel`: if `TRUE` the resource routes panel will appear in your nette debug bar
- `mappers`: replace existing mappers or add new mappers for different content-types
- `security.privateKey`: private key to hash secured requests
- `security.requestTimeKey`: key in request body, where to find request timestamp (see below - [Security &amp; authentication](#security--authentication))
- `security.requestTimeout`: maximal request timestamp age

**Tip:** Use gzip compression for your resources. You can enable it simply in neon:

```
php:
    zlib.output_compression: yes
```

#### Resource routes panel

[](#resource-routes-panel)

It is enabled by default but you can disable it by setting `restful.routes.panel` to `FALSE`. This panel show you all REST API resources routes (exactly all routes in default route list which implements `IResourceRouter` interface). This is useful e.g. for developers who develop client application, so they have all API resource routes in one place. [![REST API resource routes panel](https://raw.githubusercontent.com/alex-kalanis/restful/refs/heads/master/php-src/Diagnostics/icon.png "REST API resource routes panel")](https://raw.githubusercontent.com/alex-kalanis/restful/refs/heads/master/php-src/Diagnostics/icon.png)

Sample usage
------------

[](#sample-usage)

```
