PHP code example of bpa / api-sandbox-bundle

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

    

bpa / api-sandbox-bundle example snippets


class AppKernel extends Kernel {
    public function registerBundles() {
        // ...
        $bundles = [
            // ... 
            new Bpa\ApiSandboxBundle\ApiSandboxBundle(),
            // ...
        ];
        // ...
    }
}

$kernel = new AppKernel('sandbox', false);



namespace AppBundle\Controller;

use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Bpa\ApiSandboxBundle\Annotation as Bpa;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * Controller for books
 */
class BookController extends FOSRestController
{
    /**
     * Get book informations
     *
     * Retrieve informations about a specific book
     *
     * @ApiDoc(
     *     section="Books",
     *     resource=true,
     *     statusCodes={
     *         200="Ok",
     *     }
     * )
     *
     * @Bpa\SandboxRequest(
     *     responses={
     *         @Bpa\SandboxResponse(
     *             statusCode=200,
     *             content="@AppBundle/Resources/sandbox/responses/books/get.json",
     *         )
     *     }
     * )
     *
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function getAction(Request $request)
    {
        return new JsonResponse([
            'books' => [ 'id' => 1, 'title' => 'A Brief History of Time' ],
        ]);        
    }
}



class BookController extends FOSRestController
{
    /**
     * ...
     * 
     * @Bpa\SandboxRequest(
     *    responses={
     *        @Bpa\SandboxResponse(
     *            statusCode=200,
     *            content="@AppBundle/Resources/sandbox/responses/books/get_1.json",
     *            parameters={
     *                @Bpa\SandboxRequest\Parameter(name="id", value="1"),
     *            }
     *        ),
     *        @Bpa\SandboxResponse(
     *            statusCode=200,
     *            content="@AppBundle/Resources/sandbox/responses/books/get_2.json",
     *            parameters={
     *                @Bpa\SandboxRequest\Parameter(name="id", value="2"),
     *            }
     *        ),
     *        @Bpa\SandboxResponse(
     *            statusCode=200,
     *            content="@AppBundle/Resources/sandbox/responses/books/get.json",
     *        )
     *    }
     * )
     *
     * ...
     */
    public function getAction(Request $request) { /* ... */ }
}



namespace DocBundle\Service\ApiDoc\Extractor;

use Bpa\ApiSandboxBundle\Annotation\SandboxResponse;
use Bpa\ApiSandboxBundle\Service\ApiDoc\Extractor\ExampleGenerator as BaseGenerator;
use Symfony\Component\Routing\Route;

class ExampleGenerator extends BaseGenerator
{
    /**
     * @param Route           $route
     * @param SandboxResponse $response
     *
     * @return mixed|string
     */
    protected function buildExample(Route $route, SandboxResponse $response)
    {
        return 'Your custom example markdown';
    }
}