banki.test/controllers/ApiController.php

38 lines
937 B
PHP

<?php
namespace app\controllers;
use app\models\Parameter;
use app\models\Image;
use yii\data\ActiveDataProvider;
use yii\rest\IndexAction;
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)
{
$img = Image::find()->where([ 'sha256' => $request->getQueryParam('sha256') ])->one();
if (!$img) {
throw new NotFoundHttpException();
}
\Yii::$app->response->format = Response::FORMAT_RAW;
\Yii::$app->response->headers->set('Content-Type', $img->mime);
return file_get_contents(env('UPLOADS_PATH') . '/' . $img->sha256);
}
public function verbs()
{
return [
'index' => [ 'GET' ],
];
}
}