Answer the question
In order to leave comments, you need to log in
How to generate pagination button links for page with cnc url?
Good day!
The situation is the following.
In the overridden createUrl method of the UrlManager class to create the CNC url for the links to the blog post lists, I did the following:
if ($params[0] == 'blogs/posts' && isset($params['id'])) {
$model = Blogs::findOne($params['id']);
$alias = $model->blog_alias;
$url = '/blogs/'.$alias;
}
if (strpos(Url::to(''), '/blogs/') !== false) {
$alias = explode('/blogs/', Url::to(''));
$model = Blogs::find()->where('blog_alias = :name', [':name'=>array_pop($alias)])->one();
if ($model) {
$params['id'] = $model->blog_id;
return ['/blogs/posts', $params];
}
public function actionPosts($id)
{
$this->layout = 'default_two_columns';
$blog = $this->findModel($id);
$query = BlogsPosts::find()
->where('bp_blog_id = :blog', [':blog' => $id])
->orderBy('bp_post_created DESC');
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('posts', [
'dataProvider' => $dataProvider,
'blog' => $blog
]);
}
Answer the question
In order to leave comments, you need to log in
Why are you overriding createUrl?
You do not know how to make rules for urlManager in the config?
Yii2 has excellent CNC support, including for blog posts/news.
Config:
Forming a link to a post:
Controller:
public function actionView($slug)
{
$post = Post::findBySlug($slug);
if($post === null) {
throw new NotFoundHttpException;
}
return $this->render('view', [
'post' => $post
]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question