PHP code example of denisok94 / yii-helper
1. Go to this page and download the library: Download denisok94/yii-helper 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/ */
denisok94 / yii-helper example snippets
namespace app\controllers;
use \denisok94\helper\yii2\StatusController;
class MyController extends StatusController
{
// code
}
// получить все данные
$message = $this->post; // array
// получить параметр из данных
$phone = $this->getPost('phone'); // phone or null
// Сообщить об успешной обработки
return $this->sendSuccess(); // http status code 200
// ['code' => '200', 'status' => 'OK', 'data' => []];
// Вернуть результат работы
return $this->sendSuccess($data); // http status code 200
// ['code' => '200', 'status' => 'OK', 'data' => $data];
return $this->sendError(); // http status code 400
// ['code' => '400', 'status' => 'FAIL', 'message' => 'Error', 'data' => []]
return $this->sendError($message); // http status code 400
// ['code' => '400', 'status' => 'FAIL', 'message' => $message, 'data' => []]
return $this->sendError($message, $data); // http status code 400
// ['code' => '400', 'status' => 'FAIL', 'message' => $message, 'data' => $data]
return $this->sendError($message, $data, 401); // http status code 401
// ['code' => '401', ...]
// return ['code', 'status', 'message'];
return $this->sendBadRequest(); // http status code 400
return $this->sendUnauthorized(); // http status code 401
return $this->sendForbidden(); // http status code 403
return $this->sendNotFound(); // http status code 404
return $this->sendInternalServerError(); // http status code 500
if (!$this->post) {
return $this->sendBadRequest("Request is null"); // http status code 400
// ['code' => '400', 'status' => 'FAIL', 'message' => 'Request is null']
}
try {
//code...
} catch (\Exception $e) {
return $this->sendInternalServerError($e->getMessage()); // http status code 500
// ['code' => '500', 'status' => 'FAIL', 'message' => '...']
}
return $this->send([...]); // http status code 200
// [...];
return $this->send(['code' => 204]); // http status code 204
// ['code' => '204'];
return $this->send(['code' => 201, 'data' => $data]); // http status code 201
// ['code' => '201', 'data' => $data];
return $this->sendResponse($data); // http status code 200
// ['code' => '200', 'status' => 'OK', 'message' => '', 'data' => $data]
return $this->sendResponse($data, $message); // http status code 200
// ['code' => '200', 'status' => 'OK', 'message' => $message, 'data' => $data]
return $this->sendResponse($data, $message, $status, 999); // http status code 999
// ['code' => '999', 'status' => $status, 'message' => $message, 'data' => $data]
namespace app\commands;
use \denisok94\helper\yii2\ConsoleController;
class MyController extends ConsoleController
{
// code
}
use \denisok94\helper\yii2\Helper as H;
H::exec('controller/action', [params]);
$init = $this->params;
namespace app\commands;
use \denisok94\helper\yii2\ConsoleController;
class MyController extends ConsoleController
{
public function actionTest()
{
$init = $this->params; // ['test' => 'test1']
$test = $this->params['test']; // 'test1'
}
}
//
use \denisok94\helper\yii2\Helper as H;
H::exec('my/test', ['test' => 'test1']);
use \denisok94\helper\yii2\Helper as H;
H::methodName($arg);
namespace app\components;
use app\components\H;
class Filter
{
//.....
/**
* @return array
*/
public static function getTypes()
{
$types = H::getCache('types'); // dir: app/cache/types.json
if ($types) {
return $types;
} else {
$types = \app\models\Types::find()
->select(['id', 'name'])->all();
$array = [];
foreach ($types as $key => $value) {
$array[$value->id] = ucfirst($value->name);
}
H::setCache('types', $array);
return $array;
}
}
}