本文整理汇总了PHP中app\models\Post::getPostBySlug方法的典型用法代码示例。如果您正苦于以下问题:PHP Post::getPostBySlug方法的具体用法?PHP Post::getPostBySlug怎么用?PHP Post::getPostBySlug使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Post
的用法示例。
在下文中一共展示了Post::getPostBySlug方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionCreate
/**
* 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()]);
}
示例2: actionView
/**
* 文章页面
* @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]);
}