From 1eee909cc8838a8ee897b4690623e1f94f5d4c93 Mon Sep 17 00:00:00 2001 From: michaelweixi <502028657@qq.com> Date: Sun, 13 Nov 2016 00:09:46 +0800 Subject: [PATCH] V1.11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 前台页面初步搭建 --- common/models/Post.php | 31 ++++++ frontend/config/main.php | 2 + frontend/controllers/PostController.php | 124 ++++++++++++++++++++++++ frontend/views/layouts/main.php | 17 ++-- frontend/views/post/_form.php | 35 +++++++ frontend/views/post/_listitem.php | 28 ++++++ frontend/views/post/_search.php | 41 ++++++++ frontend/views/post/create.php | 21 ++++ frontend/views/post/index.php | 83 ++++++++++++++++ frontend/views/post/update.php | 21 ++++ frontend/views/post/view.php | 42 ++++++++ 11 files changed, 437 insertions(+), 8 deletions(-) create mode 100644 frontend/controllers/PostController.php create mode 100644 frontend/views/post/_form.php create mode 100644 frontend/views/post/_listitem.php create mode 100644 frontend/views/post/_search.php create mode 100644 frontend/views/post/create.php create mode 100644 frontend/views/post/index.php create mode 100644 frontend/views/post/update.php create mode 100644 frontend/views/post/view.php diff --git a/common/models/Post.php b/common/models/Post.php index d90bebe..0d2546c 100644 --- a/common/models/Post.php +++ b/common/models/Post.php @@ -3,6 +3,7 @@ namespace common\models; use Yii; +use yii\helpers\Html; /** * This is the model class for table "post". @@ -129,8 +130,38 @@ public function afterDelete() Tag::updateFrequency($this->tags, ''); } + public function getUrl() + { + return Yii::$app->urlManager->createUrl( + ['post/detail','id'=>$this->id,'title'=>$this->title]); + } + + public function getBeginning($length=288) + { + $tmpStr = strip_tags($this->content); + $tmpLen = mb_strlen($tmpStr); + + $tmpStr = mb_substr($tmpStr,0,$length,'utf-8'); + return $tmpStr.($tmpLen>$length?'...':''); + } + + public function getTagLinks() + { + $links=array(); + foreach(Tag::string2array($this->tags) as $tag) + { + $links[]=Html::a(Html::encode($tag),array('post/index','PostSearch[tags]'=>$tag)); + } + return $links; + } + + public function getCommentCount() + { + return Comment::find()->where(['post_id'=>$this->id,'status'=>2])->count(); + } + diff --git a/frontend/config/main.php b/frontend/config/main.php index c9a8471..1c06353 100644 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -11,6 +11,8 @@ 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'controllerNamespace' => 'frontend\controllers', + 'defaultRoute'=>'post/index', + 'language'=>'zh-CN', 'components' => [ 'user' => [ 'identityClass' => 'common\models\User', diff --git a/frontend/controllers/PostController.php b/frontend/controllers/PostController.php new file mode 100644 index 0000000..bf0189b --- /dev/null +++ b/frontend/controllers/PostController.php @@ -0,0 +1,124 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Post models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new PostSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Post model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * 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(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } + + /** + * Updates an existing Post model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing Post model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Post model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Post the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Post::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index c86aa9b..a02e868 100644 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -28,25 +28,26 @@
'My Company', + 'brandLabel' => '魏曦教你学Yii2.0', + 'brandOptions'=> ['style'=>'color:yellow;font-size:23px'], 'brandUrl' => Yii::$app->homeUrl, 'options' => [ 'class' => 'navbar-inverse navbar-fixed-top', ], ]); $menuItems = [ - ['label' => 'Home', 'url' => ['/site/index']], - ['label' => 'About', 'url' => ['/site/about']], - ['label' => 'Contact', 'url' => ['/site/contact']], + + ['label' => '关于我们', 'url' => ['/site/about']], + ['label' => '联系我们', 'url' => ['/site/contact']], ]; if (Yii::$app->user->isGuest) { - $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']]; - $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']]; + $menuItems[] = ['label' => '注册', 'url' => ['/site/signup']]; + $menuItems[] = ['label' => '登录', 'url' => ['/site/login']]; } else { $menuItems[] = '
  • ' . Html::beginForm(['/site/logout'], 'post') . Html::submitButton( - 'Logout (' . Yii::$app->user->identity->username . ')', + '退出 (' . Yii::$app->user->identity->username . ')', ['class' => 'btn btn-link'] ) . Html::endForm() @@ -70,7 +71,7 @@