Skip to content

Commit 1eee909

Browse files
committed
V1.11
前台页面初步搭建
1 parent 17e16e0 commit 1eee909

File tree

11 files changed

+437
-8
lines changed

11 files changed

+437
-8
lines changed

common/models/Post.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace common\models;
44

55
use Yii;
6+
use yii\helpers\Html;
67

78
/**
89
* This is the model class for table "post".
@@ -129,8 +130,38 @@ public function afterDelete()
129130
Tag::updateFrequency($this->tags, '');
130131
}
131132

133+
public function getUrl()
134+
{
135+
return Yii::$app->urlManager->createUrl(
136+
['post/detail','id'=>$this->id,'title'=>$this->title]);
137+
}
138+
139+
public function getBeginning($length=288)
140+
{
141+
$tmpStr = strip_tags($this->content);
142+
$tmpLen = mb_strlen($tmpStr);
143+
144+
$tmpStr = mb_substr($tmpStr,0,$length,'utf-8');
145+
return $tmpStr.($tmpLen>$length?'...':'');
146+
}
147+
148+
public function getTagLinks()
149+
{
150+
$links=array();
151+
foreach(Tag::string2array($this->tags) as $tag)
152+
{
153+
$links[]=Html::a(Html::encode($tag),array('post/index','PostSearch[tags]'=>$tag));
154+
}
155+
return $links;
156+
}
157+
158+
public function getCommentCount()
159+
{
160+
return Comment::find()->where(['post_id'=>$this->id,'status'=>2])->count();
161+
}
132162

133163

164+
134165

135166

136167

frontend/config/main.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
'basePath' => dirname(__DIR__),
1212
'bootstrap' => ['log'],
1313
'controllerNamespace' => 'frontend\controllers',
14+
'defaultRoute'=>'post/index',
15+
'language'=>'zh-CN',
1416
'components' => [
1517
'user' => [
1618
'identityClass' => 'common\models\User',
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace frontend\controllers;
4+
5+
use Yii;
6+
use common\models\Post;
7+
use common\models\PostSearch;
8+
use yii\web\Controller;
9+
use yii\web\NotFoundHttpException;
10+
use yii\filters\VerbFilter;
11+
12+
/**
13+
* PostController implements the CRUD actions for Post model.
14+
*/
15+
class PostController extends Controller
16+
{
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function behaviors()
21+
{
22+
return [
23+
'verbs' => [
24+
'class' => VerbFilter::className(),
25+
'actions' => [
26+
'delete' => ['POST'],
27+
],
28+
],
29+
];
30+
}
31+
32+
/**
33+
* Lists all Post models.
34+
* @return mixed
35+
*/
36+
public function actionIndex()
37+
{
38+
$searchModel = new PostSearch();
39+
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
40+
41+
return $this->render('index', [
42+
'searchModel' => $searchModel,
43+
'dataProvider' => $dataProvider,
44+
]);
45+
}
46+
47+
/**
48+
* Displays a single Post model.
49+
* @param integer $id
50+
* @return mixed
51+
*/
52+
public function actionView($id)
53+
{
54+
return $this->render('view', [
55+
'model' => $this->findModel($id),
56+
]);
57+
}
58+
59+
/**
60+
* Creates a new Post model.
61+
* If creation is successful, the browser will be redirected to the 'view' page.
62+
* @return mixed
63+
*/
64+
public function actionCreate()
65+
{
66+
$model = new Post();
67+
68+
if ($model->load(Yii::$app->request->post()) && $model->save()) {
69+
return $this->redirect(['view', 'id' => $model->id]);
70+
} else {
71+
return $this->render('create', [
72+
'model' => $model,
73+
]);
74+
}
75+
}
76+
77+
/**
78+
* Updates an existing Post model.
79+
* If update is successful, the browser will be redirected to the 'view' page.
80+
* @param integer $id
81+
* @return mixed
82+
*/
83+
public function actionUpdate($id)
84+
{
85+
$model = $this->findModel($id);
86+
87+
if ($model->load(Yii::$app->request->post()) && $model->save()) {
88+
return $this->redirect(['view', 'id' => $model->id]);
89+
} else {
90+
return $this->render('update', [
91+
'model' => $model,
92+
]);
93+
}
94+
}
95+
96+
/**
97+
* Deletes an existing Post model.
98+
* If deletion is successful, the browser will be redirected to the 'index' page.
99+
* @param integer $id
100+
* @return mixed
101+
*/
102+
public function actionDelete($id)
103+
{
104+
$this->findModel($id)->delete();
105+
106+
return $this->redirect(['index']);
107+
}
108+
109+
/**
110+
* Finds the Post model based on its primary key value.
111+
* If the model is not found, a 404 HTTP exception will be thrown.
112+
* @param integer $id
113+
* @return Post the loaded model
114+
* @throws NotFoundHttpException if the model cannot be found
115+
*/
116+
protected function findModel($id)
117+
{
118+
if (($model = Post::findOne($id)) !== null) {
119+
return $model;
120+
} else {
121+
throw new NotFoundHttpException('The requested page does not exist.');
122+
}
123+
}
124+
}

frontend/views/layouts/main.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,26 @@
2828
<div class="wrap">
2929
<?php
3030
NavBar::begin([
31-
'brandLabel' => 'My Company',
31+
'brandLabel' => '魏曦教你学Yii2.0',
32+
'brandOptions'=> ['style'=>'color:yellow;font-size:23px'],
3233
'brandUrl' => Yii::$app->homeUrl,
3334
'options' => [
3435
'class' => 'navbar-inverse navbar-fixed-top',
3536
],
3637
]);
3738
$menuItems = [
38-
['label' => 'Home', 'url' => ['/site/index']],
39-
['label' => 'About', 'url' => ['/site/about']],
40-
['label' => 'Contact', 'url' => ['/site/contact']],
39+
40+
['label' => '关于我们', 'url' => ['/site/about']],
41+
['label' => '联系我们', 'url' => ['/site/contact']],
4142
];
4243
if (Yii::$app->user->isGuest) {
43-
$menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
44-
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
44+
$menuItems[] = ['label' => '注册', 'url' => ['/site/signup']];
45+
$menuItems[] = ['label' => '登录', 'url' => ['/site/login']];
4546
} else {
4647
$menuItems[] = '<li>'
4748
. Html::beginForm(['/site/logout'], 'post')
4849
. Html::submitButton(
49-
'Logout (' . Yii::$app->user->identity->username . ')',
50+
'退出 (' . Yii::$app->user->identity->username . ')',
5051
['class' => 'btn btn-link']
5152
)
5253
. Html::endForm()
@@ -70,7 +71,7 @@
7071

7172
<footer class="footer">
7273
<div class="container">
73-
<p class="pull-left">&copy; My Company <?= date('Y') ?></p>
74+
<p class="pull-left">&copy; 魏曦教你学Yii2.0 <?= date('Y') ?></p>
7475

7576
<p class="pull-right"><?= Yii::powered() ?></p>
7677
</div>

frontend/views/post/_form.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
use yii\widgets\ActiveForm;
5+
6+
/* @var $this yii\web\View */
7+
/* @var $model common\models\Post */
8+
/* @var $form yii\widgets\ActiveForm */
9+
?>
10+
11+
<div class="post-form">
12+
13+
<?php $form = ActiveForm::begin(); ?>
14+
15+
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
16+
17+
<?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>
18+
19+
<?= $form->field($model, 'tags')->textarea(['rows' => 6]) ?>
20+
21+
<?= $form->field($model, 'status')->textInput() ?>
22+
23+
<?= $form->field($model, 'create_time')->textInput() ?>
24+
25+
<?= $form->field($model, 'update_time')->textInput() ?>
26+
27+
<?= $form->field($model, 'author_id')->textInput() ?>
28+
29+
<div class="form-group">
30+
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
31+
</div>
32+
33+
<?php ActiveForm::end(); ?>
34+
35+
</div>

frontend/views/post/_listitem.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
use yii\helpers\Html;
3+
?>
4+
5+
<div class="post">
6+
<div class="title">
7+
<h2><a href="<?= $model->url;?>"><?= Html::encode($model->title);?></a></h2>
8+
9+
<div class="author">
10+
<span class="glyphicon glyphicon-time" aria-hidden="true"></span><em><?= date('Y-m-d H:i:s',$model->create_time)."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";?></em>
11+
<span class="glyphicon glyphicon-user" aria-hidden="true"></span><em><?= Html::encode($model->author->nickname);?></em>
12+
</div>
13+
</div>
14+
15+
<br>
16+
<div class="content">
17+
<?= $model->beginning;?>
18+
</div>
19+
20+
<br>
21+
<div class="nav">
22+
<span class="glyphicon glyphicon-tag" aria-hidden="true"></span>
23+
<?= implode(', ',$model->tagLinks);?>
24+
<br>
25+
<?= Html::a("评论 ({$model->commentCount})",$model->url.'#comments')?> | 最后修改于 <?= date('Y-m-s H:i:s',$model->update_time);?>
26+
</div>
27+
28+
</div>

frontend/views/post/_search.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
use yii\widgets\ActiveForm;
5+
6+
/* @var $this yii\web\View */
7+
/* @var $model common\models\PostSearch */
8+
/* @var $form yii\widgets\ActiveForm */
9+
?>
10+
11+
<div class="post-search">
12+
13+
<?php $form = ActiveForm::begin([
14+
'action' => ['index'],
15+
'method' => 'get',
16+
]); ?>
17+
18+
<?= $form->field($model, 'id') ?>
19+
20+
<?= $form->field($model, 'title') ?>
21+
22+
<?= $form->field($model, 'content') ?>
23+
24+
<?= $form->field($model, 'tags') ?>
25+
26+
<?= $form->field($model, 'status') ?>
27+
28+
<?php // echo $form->field($model, 'create_time') ?>
29+
30+
<?php // echo $form->field($model, 'update_time') ?>
31+
32+
<?php // echo $form->field($model, 'author_id') ?>
33+
34+
<div class="form-group">
35+
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
36+
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
37+
</div>
38+
39+
<?php ActiveForm::end(); ?>
40+
41+
</div>

frontend/views/post/create.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use yii\helpers\Html;
4+
5+
6+
/* @var $this yii\web\View */
7+
/* @var $model common\models\Post */
8+
9+
$this->title = 'Create Post';
10+
$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];
11+
$this->params['breadcrumbs'][] = $this->title;
12+
?>
13+
<div class="post-create">
14+
15+
<h1><?= Html::encode($this->title) ?></h1>
16+
17+
<?= $this->render('_form', [
18+
'model' => $model,
19+
]) ?>
20+
21+
</div>

0 commit comments

Comments
 (0)