[ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], ], ], ], ]; } public function actionIndex() { $query = Message::find()->where(['to_uid' => Yii::$app->user->id])->innerJoinWith('data')->orderBy('id desc'); $dataProvider = new ActiveDataProvider([ 'query' => $query ]); return $this->render('index', [ 'dataProvider' => $dataProvider ]); } public function actionView($id) { $userId = Yii::$app->user->id; $model = Message::find()->where(['id' => $id, 'to_uid' => $userId])->one(); if ($model == null) { throw new NotFoundHttpException('消息不存在'); } return $this->render('view', [ 'model' => $model ]); } public function actionCreate($id = null) { $model = new MessageForm(); if (!is_null($id)) { $toUser = Yii::$app->user->identity->findIdentity($id); $model->toUsername = $toUser->username; $model->toUid = $id; } if ($model->load(Yii::$app->request->post()) && $model->send()) { Yii::$app->session->setFlash('success', '发送成功'); return $this->redirect(['index']); } return $this->render('create', [ 'model' => $model ]); } }