tyhand / json-api-tools-bundle
JSON API tools for Symfony
Installs: 60
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- doctrine/doctrine-bundle: ^1.4
- symfony/symfony: 2.8.*
README
A collection of some very rough draft stuff I have for speeding up writing apis for symfony in json api standard. Not done, and very ugly.
Installation
Add with composer
composer require "tyhand/json-api-tools-bundle" "^0.1.2"
Add to AppKernel
$bundles = array( // ... new TyHand\JsonApiToolsBundle\TyHandJsonApiToolsBundle(), // ... );
Create Resource
For example, here is book resource
<?php // BookResource namespace AppBundle\ApiResource; use TyHand\JsonApiToolsBundle\Annotation\Resource; use TyHand\JsonApiToolsBundle\Annotation\Attribute; use TyHand\JsonApiToolsBundle\Annotation\HasOne; use TyHand\JsonApiToolsBundle\Annotation\Filter; use TyHand\JsonApiToolsBundle\Annotation\Validator; use TyHand\JsonApiToolsBundle\Extra\SearchableResource; /** * @Resource(entity="AppBundle\Entity\Book") */ class BookResource extends SearchableResource { /** * @Attribute */ public $title; /** * @Attribute */ public $genre; /** * @HasOne */ public $author; protected function getSearchableEntityFields() { return [ 'genre', 'title', ['property' => 'author.name', 'joinType' => 'outer'] ]; } }
Here is also an Author resource connected to the above example
<?php // Author Resource namespace AppBundle\ApiResource; use TyHand\JsonApiToolsBundle\Annotation\Resource; use TyHand\JsonApiToolsBundle\Annotation\Attribute; use TyHand\JsonApiToolsBundle\Annotation\HasMany; use TyHand\JsonApiToolsBundle\Annotation\Filter; use TyHand\JsonApiToolsBundle\Annotation\Validator; use TyHand\JsonApiToolsBundle\ApiResource\JsonApiResource; use TyHand\JsonApiToolsBundle\ApiResource\Resource as ApiResource; /** * @Resource(entity="AppBundle\Entity\Author") */ class AuthorResource extends ApiResource { /** * @Attribute */ public $name; /** * @HasMany */ public $books; }
Tag Resource
Tag the resources as services
# services.yml services: author_resource: class: AppBundle\ApiResource\AuthorResource tags: - { name: jsonapi_tools.resource } book_resource: class: AppBundle\ApiResource\BookResource tags: - { name: jsonapi_tools.resource }
Create Controllers
Just inherit the resources controller for quick setup
<?php // BookController namespace AppBundle\Controller; use JsonApiBundle\Controller\ResourceController; class BookController extends ResourceController { }
Routing
# routing.yml jsonapi_author: resource: AppBundle\Controller\AuthorController type: jsonapi_resource jsonapi_book: resource: AppBundle\Controller\BookController type: jsonapi_resource
If all is well, then this stuff should work. Real documentation incoming soon.