PHP code example of comoco / slim-api-bean

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

    

comoco / slim-api-bean example snippets


use comoco\SlimApiBean\Bean as SlimApiBean;
use comoco\SlimApiBean\Handler\AbstractAction;

class HomeAction extends AbstractAction
{
    public function handle($request, $response, $args)
    {
        return "Welcome";
    }
}

class HelloAction extends AbstractAction
{
    public function handle($request, $response, $args)
    {
        $datas = $request->getParsedBody();
        $name = $datas['name'];
        return "hello, {$name}!";
    }
}

$apiBean = new SlimApiBean;
$apiBean->bindAction(['GET', 'POST'], '/', new HomeAction)
    ->bindAction(['POST'], '/hello', new HelloAction)
    ->run();

use comoco\SlimApiBean\Bean as SlimApiBean;
use comoco\SlimApiBean\Handler\AbstractAction;

class HelloAction extends AbstractAction
{
    public function handle($request, $response, $args)
    {
        $datas = $request->getParsedBody();
        $name = $datas['name'];
        return "hello, {$name}!";
    }
}

$apiBean = new SlimApiBean;
$apiBean->bindAction(['POST'], '/hello', new HelloAction);

$response = $apiBean->dryRun('POST', '/hello', [
    'headers' => [
        'Content-Type' => 'application/json'
    ],
    'body' => [
        'name' => 'Bob'
    ]
]);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('hello, Bob!', (string) $response->getBody());
bash
php -S localhost:8089