/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); $currentUser = \Yii::$app->user->identity; if (!$currentUser->isAuthor()) { //不是 作者 无权操作 return $this->goHome(); } if ($model->load(Yii::$app->request->post())) { if (isset($model->user_id) || $currentUser->id != $model->user_id && !$currentUser->isAdmin()) { $model->user_id = $currentUser->id; } $model->user_id = $currentUser->id; //处理slug $title = mb_strlen($model->title, 'utf8') > 6 ? mb_substr($model->title, 0, 5) : $model->title; $model->slug = \app\helpers\Pinyin::encode($title, 'all'); while (Post::getPostBySlug($model->slug)) { $model->slug .= '-1'; } if ($model->save()) { $this->flash('发布成功!', 'info'); return $this->redirect(['update', 'id' => $model->id]); } } $model->comment_status = Post::COMMENT_STATUS_OK; $model->user_id = $currentUser->id; return $this->render('create', ['model' => $model, 'isAdmin' => $currentUser->isAdmin()]); }
/** * 文章页面 * @param $slug * @return string */ public function actionView($slug) { $model = Post::getPostBySlug($slug); $commentSearch = new CommentSearch(); if (!$model) { } $dataProvider = $commentSearch->search(['post_id' => $model->id]); return $this->render('view', ['model' => $model, 'dataProvider' => $dataProvider]); }