Componette

Componette

unimapper

unimapper / nette v1.2.1

Nette extension for UniMapper

download-cloud-line composer require unimapper/nette

Unimapper Nette extension

Build Status

Official Unimapper extension for Nette framework.

Install

$ composer require unimapper/nette:@dev

Nette 2.1 and higher

Register extension in config.neon.

extensions:
    unimapper: UniMapper\Nette\Extension

Nette 2.0

Register extension in app/bootstrap.php.

UniMapper\Nette\Extension::register($configurator);

return $configurator->createContainer();

Configuration

unimapper:
    adapters:
        Mongo: @service
        MySQL: @anotherService
        ...
    cache: true
    namingConvention:
        entity: 'YourApp\Model\*'
        repository: 'YourApp\Repository\*Repository'
    api:
        enabled: false
        module: "Api"
        route: true
    panel:
        enabled: true
        ajax: true # log queries in AJAX requests
    profiler: true
    customQueries:
        - CustomQueryClass
        - ...

API

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.

namespace YourApp\ApiModule\Presenter;

class EntityPresenter extends \UniMapper\Nette\Api\Presenter
{
    ...
}

Now you can call standard API methods like:

GET

  • associate - common parameter used to tell which association should be included in response. Syntax should be like ?associate[]=property1&associate[]=property2 or ?associate=property1,property2.

** Response**

{
    "body": {..}
}

/api/entity/id

Get a single record.

/api/entity

Get all records.

  • count - optional parameter, if ?count=true set 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 $maxLimit in your API presenter descendant.
  • offset
  • where

PUT

/api/entity

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
}

/api/entity/id

Update single record with JSON data stored in request body.

** Response**

{
    "success": true
}

POST

Create new record with JSON data stored in request body and primary value of new entity returned in response body.

/api/entity

** Response**

{
    "success": true,
    "link": "url to created entity",
    "body": "id of created entity"
}

DELETE

/api/entity

Delete all records returned body contains number of deleted records.

** Response**

{
    "body": {..}
    "success": true
}

/api/entity/id

Delete single record.

** Response**

{
    "success": true
}

Custom API methods

You can even define your custom method.

namespace YourApp\ApiModule\Presenter;

class EntityPresenter extends \UniMapper\Nette\Api\Presenter
{
    public function actionYourCustomMethod($id)
    {
        ...
    }
}

Then you can make a requests like /api/entity/1?action=yourCustomMehod.

Filtering data

Filter can be set as a GET parameter where in URL. It should be here a valid JSON format as described here.

Error response

If some bad request detected or an error occurred the returned response can be like this:

{
    "success": false
    "code": 405,
    "messages": []
}

Generating links

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}

Usage

You can even build another applications using this API, just register an official API adapter class UniMapper\Nette\Api\Adapter in your config.neon.

Custom request factory

For easier API queries you can register factory interface as a dynamic service in your config.neon.

services:
    - UniMapper\Nette\Api\ICustomRequestFactory

Usage in your reopistory can look like this:

class SomeRepository extends \UniMapper\Repository
{
    private $requestFactory;

    public function __construct(
        \UniMapper\Connection $connection,
        \UniMapper\Nette\Api\ICustomRequestFactory $requestFactory
    ) {
        parent::__construct($connection);
        $this->requestFactory;
    }

    public function getSomethingFromApi()
    {
        $this->requestFactory()->setResource("apiResource")->setAction("custom")->send();
    }
}
  • v1.2.1 v1.2.1

    • panel: fixed JS error for IE
  • v1.2.0 v1.2.0

    compatibility with UniMapper 1.2.x

  • v1.1.2 v1.1.2

    • api: custom request post/put
  • 1.1.1 v1.1.1

    • panel: workaround for Chrome header limit [issue #4]
  • v1.1.0 v1.1.0

    • update to unimapper 1.1.0
  • v1.0.10 v1.0.10

    • api: custom request error handling
    • api: AdapterException second parameter fix
  • v1.0.9 v1.0.9

    • re-released last empty tag with:

    api: route can be disabled in config.neon

  • v1.0.8 v1.0.8

    • api: route can be disabled in config.neon
  • v1.0.7 v1.0.7

    • extension: fixed notice on dynamic services
    • api: added custom request factory
  • v1.0.6 v1.0.6

    • api: api: fixed route on presenters without entity
  • v1.0.5 v1.0.5

    • api: presenter is no longer tied with repository
    • api: added get(), getOne() and count() methods for easier overriding
  • v1.0.4 v1.0.4

    • API - implemented all remaining adapter methods
    • API - unified response with readme BC break
  • v1.0.3 v1.0.3

    • api: added Nette API adapter
  • v1.0.2 v1.0.2

    • panel: removed broken UML schema support
  • v1.0.1

    • api: route match only existing entities
    • api: improved POST & PUT lifecycle events with required arguments
    • api: support for WHERE filter
    • composer: switched to PSR-4
    • composer: dependency changed to UniMapper 1.0.0 stable
  • v1.0.0

bar-chart-fill

Statistics

download-cloud-fill
88
star-fill
1
bug-fill
2
flashlight-fill
7.2y
price-tag-2-line

Badges

guide-fill

Dependencies

php (>=5.4.0)
ext-curl (*)
Componette Componette felix@nette.org