service = $service; } public function behaviors(): array { return [ 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['PostManagement'], ], [ // all the action are accessible to admin 'allow' => true, 'roles' => ['admin'], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], ], ], ]; } /** * @param $tid * * @return string * @throws NotFoundHttpException */ public function actionIndex($tid) { $type = $this->findType($tid); $searchModel = new PostCategorySearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'type' => $type, ]); } /** * @param $id * * @return string * @throws NotFoundHttpException */ public function actionView($id) { $category = $this->findModel($id); $type = $this->findType($category->type_id); return $this->render('view', [ 'category' => $category, 'type' => $type, ]); } /** * @param $tid * * @return string|\yii\web\Response * @throws NotFoundHttpException */ public function actionCreate($tid) { $type = $this->findType($tid); $form = new PostCategoryForm(); $form->type_id = $type->id; $form->updateSort(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { $category = $this->service->create($form); return $this->redirect(['view', 'id' => $category->id]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } return $this->render('create', [ 'model' => $form, 'type' => $type, ]); } /** * @param $id * * @return string|\yii\web\Response * @throws NotFoundHttpException */ public function actionUpdate($id) { $category = $this->findModel($id); $type = $this->findType($category->type_id); $form = new PostCategoryForm($category); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { $this->service->edit($category->id, $form); return $this->redirect(['view', 'id' => $category->id]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } return $this->render('update', [ 'model' => $form, 'category' => $category, 'type' => $type, ]); } /** * @param $id * * @return \yii\web\Response * @throws NotFoundHttpException */ public function actionDelete($id) { $category = $this->findModel($id); $type = $this->findType($category->type_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', 'tid' => $type->id]); } /** * @param integer $id * @return PostCategory the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id): PostCategory { if (($model = PostCategory::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException('The requested page does not exist.'); } protected function findType($id): PostType { if (($type = PostType::findOne($id)) !== null) { return $type; } throw new NotFoundHttpException('The requested page does not exist.'); } }