PHP code example of tyhand / json-api-tools-bundle

1. Go to this page and download the library: Download tyhand/json-api-tools-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tyhand / json-api-tools-bundle example snippets


$bundles = array(
  // ...
  new TyHand\JsonApiToolsBundle\TyHandJsonApiToolsBundle(),
  // ...
);


// 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']
        ];
    }
}


// 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;
}


// BookController

namespace AppBundle\Controller;

use JsonApiBundle\Controller\ResourceController;

class BookController extends ResourceController
{
}