banki.test/controllers/ApiController.php

50 lines
1.3 KiB
PHP

<?php
namespace app\controllers;
use app\models\Parameter;
use app\models\Image;
use yii\filters\ContentNegotiator;
use yii\web\NotFoundHttpException;
use yii\web\Request;
use yii\web\Response;
class ApiController extends \yii\rest\Controller
{
public function actionIndex()
{
return Parameter::find()->with(['icon', 'iconGray'])->all();
}
public function actionImage(Request $request, Response $response)
{
$img = Image::find()->where([ 'sha256' => $request->getQueryParam('sha256') ])->one();
if (!$img) {
throw new NotFoundHttpException();
}
$response->headers->set('Content-Type', $img->mime);
$response->headers->set('X-Image-Data', json_encode($img->getAttributes()));
return $response->sendFile(env('UPLOADS_PATH') . '/' . $img->sha256, $img->original_name);
}
public function verbs()
{
return [
'index' => [ 'GET' ],
];
}
public function behaviors()
{
return [
[
'class' => ContentNegotiator::class,
'formats' => [
'image/*' => Response::FORMAT_RAW,
'application/json' => Response::FORMAT_JSON
]
]
];
}
}