Simple API components.
composer config minimum-stability dev
composer config prefer-stable true
composer config repositories.cent/api-components git https://github.com/EvgeniyBlinov/ApiComponents
composer require "cent/api-components:v0.0.2"
Yii2 example with simple API action.
Set API error and response type to custom json:
/**
* Init
*
* @return void
**/
public function init()
{
if (defined('YII_ENV') && YII_ENV == 'prod') {
$handler = new ApiErrorHandler();
\Yii::$app->set('errorHandler', $handler);
$handler->register();
}
\Yii::$app->response->format = 'json';
parent::init();
}
Create any version action:
/**
* Action for version 1
*
* @param string $arguments
* @return array
**/
public function actionV1($arguments)
{
$meta = [
'server_name' => $_SERVER['SERVER_NAME'],
];
// get production API if Development environvent enable
if (defined('YII_ENV') && YII_ENV == 'dev_api') {
$content = @file_get_contents(
'http://main-site-host' .
\Yii::$app->request->url
);
if ($result = json_decode($content, true)) {
return $result;
}
}
$apiArgv = explode('/', $arguments);
$params = array_diff_key(Yii::$app->request->get(), array_flip(array('arguments')));
if ($apiArgv[0] == 'doc') {
return $this->renderDoc();
}
if (!empty($apiArgv[1])) {
$params['id'] = $apiArgv[1];
}
return (new ApiResponse(
$this->getAllModelData($apiArgv[0], $params),
compact('meta')
))->__toArray();
}