PHP code example of everycheck / api-rest-utils

1. Go to this page and download the library: Download everycheck/api-rest-utils 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/ */

    

everycheck / api-rest-utils example snippets

 
    public function getAction($id)
    {
        $response  = new ResponseBuilder($this->get('jms_serializer'));
        $entity = $this->getDoctrine()->getManager()->getRepository(Entity::class)->find($id);
        
        if(empty($entity)) return $response->notFound();
        
        return $response->ok($entities);
    }
 
    public function getAction($id)
    {
        $response  = new ResponseBuilder($this->get('jms_serializer'));
        $entity = $this->getDoctrine()->getManager()->getRepository(Entity::class)->find($id);
        
        if(empty($entity)) return $response->notFound();
        
        return $response->addHeaders('X-something','some-value')->ok($entities);
    }



namespace ACMEBundle\Repository;

use EveryCheck\ApiRest\Utils\PaginatedRepositoryTrait;

class PostRepository extends \Doctrine\ORM\EntityRepository
{
    use PaginatedRepositoryTrait;

    const BASE_QUERY_NAME = 'post';

    const LEFT_JOIN_ALIAS_LIST = [
        'post.author'          => 'author',
        'post.responses'        => 'response',
    ];

    const FILTER_OPTION = [
        ['filterOn'=>'author.username'   , 'filterName' => 'username'         , 'filterKind'=>'like'         ],
        ['filterOn'=>'response.message'  , 'filterName' => 'response_message' , 'filterKind'=>'like'         ],
        ['filterOn'=>'response.date'     , 'filterName' => 'date'             , 'filterKind'=>'greaterThan'  ],
    ];

}

    /**
     * @Route("/posts", name="get_post_list", methods={"GET"})
     */
    public function getPostListAction(Request $request)
    {       
        $posts = $this->getEntityManager()->getRepository(Post::class)->findPaginatedFromRequest($request);
        return $this->getResponseBuilder()->ok($posts);
    }