Skip to content

Commit

Permalink
V1.11
Browse files Browse the repository at this point in the history
前台页面初步搭建
  • Loading branch information
michaelweixi committed Nov 12, 2016
1 parent 17e16e0 commit 1eee909
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 8 deletions.
31 changes: 31 additions & 0 deletions common/models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace common\models;

use Yii;
use yii\helpers\Html;

/**
* This is the model class for table "post".
Expand Down Expand Up @@ -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();
}






Expand Down
2 changes: 2 additions & 0 deletions frontend/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'defaultRoute'=>'post/index',
'language'=>'zh-CN',
'components' => [
'user' => [
'identityClass' => 'common\models\User',
Expand Down
124 changes: 124 additions & 0 deletions frontend/controllers/PostController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace frontend\controllers;

use Yii;
use common\models\Post;
use common\models\PostSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
* PostController implements the CRUD actions for Post model.
*/
class PostController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'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.');
}
}
}
17 changes: 9 additions & 8 deletions frontend/views/layouts/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@
<div class="wrap">
<?php
NavBar::begin([
'brandLabel' => '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[] = '<li>'
. Html::beginForm(['/site/logout'], 'post')
. Html::submitButton(
'Logout (' . Yii::$app->user->identity->username . ')',
'退出 (' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link']
)
. Html::endForm()
Expand All @@ -70,7 +71,7 @@

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

<p class="pull-right"><?= Yii::powered() ?></p>
</div>
Expand Down
35 changes: 35 additions & 0 deletions frontend/views/post/_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\Post */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="post-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>

<?= $form->field($model, 'tags')->textarea(['rows' => 6]) ?>

<?= $form->field($model, 'status')->textInput() ?>

<?= $form->field($model, 'create_time')->textInput() ?>

<?= $form->field($model, 'update_time')->textInput() ?>

<?= $form->field($model, 'author_id')->textInput() ?>

<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>
28 changes: 28 additions & 0 deletions frontend/views/post/_listitem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
use yii\helpers\Html;
?>

<div class="post">
<div class="title">
<h2><a href="<?= $model->url;?>"><?= Html::encode($model->title);?></a></h2>

<div class="author">
<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>
<span class="glyphicon glyphicon-user" aria-hidden="true"></span><em><?= Html::encode($model->author->nickname);?></em>
</div>
</div>

<br>
<div class="content">
<?= $model->beginning;?>
</div>

<br>
<div class="nav">
<span class="glyphicon glyphicon-tag" aria-hidden="true"></span>
<?= implode(', ',$model->tagLinks);?>
<br>
<?= Html::a("评论 ({$model->commentCount})",$model->url.'#comments')?> | 最后修改于 <?= date('Y-m-s H:i:s',$model->update_time);?>
</div>

</div>
41 changes: 41 additions & 0 deletions frontend/views/post/_search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\PostSearch */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="post-search">

<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>

<?= $form->field($model, 'id') ?>

<?= $form->field($model, 'title') ?>

<?= $form->field($model, 'content') ?>

<?= $form->field($model, 'tags') ?>

<?= $form->field($model, 'status') ?>

<?php // echo $form->field($model, 'create_time') ?>

<?php // echo $form->field($model, 'update_time') ?>

<?php // echo $form->field($model, 'author_id') ?>

<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>
21 changes: 21 additions & 0 deletions frontend/views/post/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use yii\helpers\Html;


/* @var $this yii\web\View */
/* @var $model common\models\Post */

$this->title = 'Create Post';
$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-create">

<h1><?= Html::encode($this->title) ?></h1>

<?= $this->render('_form', [
'model' => $model,
]) ?>

</div>
Loading

0 comments on commit 1eee909

Please sign in to comment.