Controller.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: ljt
  5. * DateTime: 2016/11/9 9:46
  6. * Description:
  7. */
  8. namespace common\components;
  9. use Yii;
  10. use yii\web\Response;
  11. use yii\widgets\ActiveForm;
  12. class Controller extends \yii\web\Controller
  13. {
  14. public function renderJson($status = 1, $message = '', $data = [])
  15. {
  16. \Yii::$app->response->format = 'json';
  17. return array_merge(['status' => $status, 'message' => $message], $data);
  18. }
  19. public function goReferrer()
  20. {
  21. return Yii::$app->controller->redirect(Yii::$app->request->getReferrer());
  22. }
  23. public function flash($key, $value)
  24. {
  25. Yii::$app->session->setFlash($key, $value);
  26. }
  27. public function performAjaxValidation($model)
  28. {
  29. if (Yii::$app->request->isAjax && !Yii::$app->request->isPjax && Yii::$app->request->get('ajax-validate')) {
  30. if ($model->load(Yii::$app->request->post())) {
  31. Yii::$app->response->format = Response::FORMAT_JSON;
  32. echo json_encode(ActiveForm::validate($model));
  33. Yii::$app->end();
  34. }
  35. }
  36. }
  37. // public function beforeAction($action)
  38. // {
  39. // if (parent::beforeAction($action)) {
  40. // if ($this->enableCsrfValidation && !in_array(Yii::$app->request->method, ['GET', 'HEAD', 'OPTIONS'], true)) {
  41. // Yii::$app->getRequest()->getCsrfToken(true);
  42. // }
  43. // return true;
  44. // }
  45. //
  46. // return false;
  47. // }
  48. //
  49. // public function beforeAction($action)
  50. // {
  51. // if (parent::beforeAction($action)) {
  52. // if ($this->enableCsrfValidation) {
  53. // Yii::$app->getRequest()->getCsrfToken(true);
  54. // }
  55. // return true;
  56. // }
  57. //
  58. // return false;
  59. // }
  60. }