PHP code example of vonbraunlabs / slimx

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

    

vonbraunlabs / slimx example snippets


$entrypoints['testGet'] = new Action(
    'GET',
    '/test,
    [
        'application/vnd.vbl.slimx.v1+json' => function ($request, $response, $args) {
            $response->write("{'api-version': 'v1'}");

            return $response;
        },
        'application/vnd.vbl.slimx.v2+json' => function ($request, $response, $args) {
            $response->write("{'api-version': 'v2'}");

            return $response;
        }
    ]
);



namespace My\Controllers;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use SlimX\Controllers\AbstractController;
use SlimX\Controllers\Action;

class DefaultController extends AbstractController
{
    /**
     * Implements the abstract method loadAction defined on AbstractController.
     * This method is called when it is time to load the actions into the
     * \Slim\App object.
     */
    public function loadActions()
    {
        $this->pushEntrypoint(new Action(
            'GET',
            '/publickey',
            ['application/vnd.my.project.v1+json' => [$this, 'getBooksAction']],
            [$this, 'handleApiVersionNotSpecified']
        ));
    }

    /**
     * Returns a list of books. In this example method an empty array.
     *
     * @param RequestInterface $request The HTTP request
     * @param ResponseInterface $response The generated HTTP response
     * @param array $args Optional arguments
     *
     * @return ResponseInterface Response processed by the Action
     */
    public function getBooksAction(
        RequestInterface $request,
        ResponseInterface $response,
        array $args
    ) {
        $response->write(json_encode([]));
        return $response;
    }

    /**
     * To be used as a callback by SlimX, handling requests that doesn't
     * provide a valid API version.
     *
     * @param $response ResponseInterface object
     * @return Returns the response, with status code and body set.
     */
    public function handleApiVersionNotSpecified(ResponseInterface $response)
    {
        return $this->container->get('error')->handle($response, 1000);
    }
}

$error = new Error();
$error->setCodeList([
    1000 => [
        'status' => 404,
        'message' => 'User info not found'
    ],
    1001 => [
        'status' => 406,
        'message' => 'You must specify API version'
    ],
]);

return $error->handle($response, 1000);