PHP code example of fabricio872 / api-modeller-bundle

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

    

fabricio872 / api-modeller-bundle example snippets


// config/bundles.php

return [
    // ...
    Fabricio872\ApiModeller\ApiModellerBundle::class => ['all' => true],
];

// src/ApiModels/Users.php

use Fabricio872\ApiModeller\Annotations\Resource;

/**
 * @Resource(
 *     endpoint="{{api_url}}/api/users",
 *     method="GET",
 *     type="json",
 *     options={
 *          "headers"={
 *              "accept"= "application/json"
 *          }
 *     }
 * )
 */
class Users
{
    public $page;
    public $per_page;
    public $total;
    public $total_pages;
    public $data;
}

// src/ApiModels/Users.php

use Fabricio872\ApiModeller\Annotations\Resource;
use Fabricio872\ApiModeller\Annotations\Resources;

/**
 * @Resources({
 *      "multiple"= @Resource(
 *          endpoint="{{api_url}}/api/users",
 *          method="GET",
 *          type="json",
 *          options={
 *              "headers"={
 *                  "accept"= "application/json"
 *              }
 *          }
 *      ),
 *      "single"= @Resource(
 *          endpoint="{{api_url}}/api/users/{{id}}",
 *          method="GET",
 *          type="json",
 *          options={
 *              "headers"={
 *                  "accept"= "application/json"
 *              }
 *          }
 *      ),
 * })
 */
class Users
{
    public $page;
    public $per_page;
    public $total;
    public $total_pages;
    public $data;
}

// src/Controller/SomeController.php


    /**
     * @Route("/", name="some_controller")
     */
    public function index(Modeller $modeller): Response
    {
        dump($modeller->getData(
            Repo::new(Users::class)
                ->setOptions([
                    "query" => [
                        "page" => 2
                    ]
                ])
        ));

        return $this->render('some_controller/index.html.twig', [
            'controller_name' => 'SomeController',
        ]);
    }

// src/Controller/SomeController.php


    /**
     * @Route("/", name="some_controller")
     */
    public function index(Modeller $modeller): Response
    {
        dump($modeller->getData(
            Repo::new(Users::class)
                ->setParameters([
                    "id" => 2
                ])
                ->setIdentifier("single")
        ));

        return $this->render('some_controller/index.html.twig', [
            'controller_name' => 'SomeController',
        ]);
    }