Skip to content

como65416/slim-api-bean

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Slim Api Bean

一個將 Slim 封裝起來的 library,簡化一些流程上的操作。

Document

參考:文件

Api Bean Example

建立一個 index.php

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();

啟動

php -S localhost:8089

發出 request

GET

curl -X GET localhost:8089

輸出:

Welcome

POST

curl -d "name=Bob" -X POST localhost:8089/hello

輸出:

hello, Bob!

Test Example

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());

About

A simple and easy to use slim wrapper.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages