本文整理匯總了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]);
}