Official Unimapper extension for Nette framework.
$ composer require unimapper/nette:@devRegister extension in config.neon.
extensions:
unimapper: UniMapper\Nette\ExtensionRegister extension in app/bootstrap.php.
UniMapper\Nette\Extension::register($configurator);
return$configurator->createContainer();unimapper:
adapters:
Mongo: @serviceMySQL: @anotherService...cache: truenamingConvention:
entity: 'YourApp\Model\*'repository: 'YourApp\Repository\*Repository'api:
enabled: falsemodule: "Api"route: truepanel:
enabled: trueajax: true # log queries in AJAX requestsprofiler: truecustomQueries:
- CustomQueryClass
- ...Creating new API for your application is very easy, all you need is presenter for every entity you have.
Remember that every API presenter should always extend UniMapper\Nette\Api\Presenter.
namespaceYourApp\ApiModule\Presenter;
class EntityPresenter extends \UniMapper\Nette\Api\Presenter
{
...
}Now you can call standard API methods like:
- associate - common parameter used to tell which association should be included in response. Syntax should be like
?associate[]=property1&associate[]=property2or?associate=property1,property2.
** Response**
{
"body": {..}
}Get a single record.
Get all records.
- count - optional parameter, if
?count=trueset then items count number will be returned in response body instead data. - limit - maximum limit is set to
10. You can change it by overriding property$maxLimitin your API presenter descendant. - offset
- where
Update all records with JSON data stored in request body. Filtering can be set and response body contains number of affected records.
** Response**
{
"body": 3,
"success": true
}Update single record with JSON data stored in request body.
** Response**
{
"success": true
}Create new record with JSON data stored in request body and primary value of new entity returned in response body.
** Response**
{
"success": true,
"link": "url to created entity",
"body": "id of created entity"
}Delete all records returned body contains number of deleted records.
** Response**
{
"body": {..}
"success": true
}Delete single record.
** Response**
{
"success": true
}You can even define your custom method.
namespaceYourApp\ApiModule\Presenter;
class EntityPresenter extends \UniMapper\Nette\Api\Presenter
{
publicfunctionactionYourCustomMethod($id)
{
...
}
}Then you can make a requests like /api/entity/1?action=yourCustomMehod.
Filter can be set as a GET parameter where in URL. It should be here a valid JSON format as described here.
If some bad request detected or an error occurred the returned response can be like this:
{
"success": false"code": 405,
"messages": []
}In your templates just use standard Nette link macro.
{link :Api:Entity:get}{link :Api:Entity:get 1}{link :Api:Entity:put 1}{link :Api:Entity:post}{link :Api:Entity:action}
You can even build another applications using this API, just register an official API adapter class UniMapper\Nette\Api\Adapter in your config.neon.
For easier API queries you can register factory interface as a dynamic service in your config.neon.
services:
- UniMapper\Nette\Api\ICustomRequestFactoryUsage in your reopistory can look like this:
class SomeRepository extends \UniMapper\Repository
{
private$requestFactory;
publicfunction__construct(
\UniMapper\Connection$connection,
\UniMapper\Nette\Api\ICustomRequestFactory$requestFactory
) {
parent::__construct($connection);
$this->requestFactory;
}
publicfunctiongetSomethingFromApi()
{
$this->requestFactory()->setResource("apiResource")->setAction("custom")->send();
}
}