J
J
jekahm2016-02-12 02:03:37
Yii
jekahm, 2016-02-12 02:03:37

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;
      }

I parse the url itself in the overridden parseRequest method of the UrlRule class:
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];
            }

Controller code responsible for displaying blog posts:
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
            ]);
    }

Everything works fine, except for the pagination buttons. Since in this case there are no page and per-page parameters, and the button link = the URL of the page itself.
Please help me find a way out of this problem.
I got the code from previous coders.
Thank you all in advance for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Arutyunov, 2016-02-12
@arutyunov

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
        ]);
    }

Post model uses sluggable behavior

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question