PHP code example of skrip42 / api-skeleton

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

    

skrip42 / api-skeleton example snippets


/** in you controller */

class CustomController extends AbstractApiController //extends AbstractApiController
{
    public function sameMethod() : Response
    {
        ...
        //use api(mixed $data, array $meta = []) method to serialize response
        return $this->api($dataOrEntity);
    }

    ...

    //in you entity/DOT class
    /** @Groups("groupName") */
    private $field; //add annotation to filed

    /** @Groups("groupName") */
    public function GetField(): fieldtype //or to you public method

    //in you controller
    return $this->api(
        $dataOrEntity,
        [
            'groups' => 'groupName'
        ]
    );

    //in you controller print some like this
    $page = $request->query->getInt('page', 1);
    $count = $repository->count([]);
    $perPage = $request->query->getInt('perPage', $count);
    $sort = $request->query->get('sort');
    $entities = $repository->findBy(
        $params,
        $sort,
        $perPage,
        $perPage * ($page - 1)
    );
    $this->api(
        $entities,
        [
            'currentPage' => $page,
            'perPage' => $perPage,
            'pagesTotal' => ceil($count / $perPage)
        ]
    );

use App\Exceptions\ApiException;
...
throw new ApiException($message, $statusCode);