service = $service; } public function behaviors(): array { return [ 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['FormsManagement'], ], [ // all the action are accessible to admin 'allow' => true, 'roles' => ['admin'], ], ], ], 'verbs' => [ 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['POST'], ], ], ]; } /** * @return mixed */ public function actionIndex() { $searchModel = new FormMessageSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } /** * @param $id * * @return string * @throws NotFoundHttpException */ public function actionView($id) { $this->service->setRead($id); return $this->render('view', [ 'message' => $this->findModel($id), ]); } /** * @param integer $id * @return mixed */ public function actionDelete($id) { try { $this->service->remove($id); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } return $this->redirect(['index']); } /** * @param $id * * @return FormMessage * @throws NotFoundHttpException */ protected function findModel($id): FormMessage { if (($model = FormMessage::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException('The message does not exist.'); } }