M
M
Maxim Osadchy2017-10-15 12:06:22
Yii
Maxim Osadchy, 2017-10-15 12:06:22

How to add pagination pages to xml sitemap?

Hello!
I wrote a sitemap generator (I looked at how Eliseev did it and reworked it a bit) for search engines, but I don’t know how to add pagination pages there, the generator code:

class XmlController extends AppController{
  
  public function actionMap()
  {
        $urls = array();
 		$categories = Category::find()->asArray()->all();
 		foreach ($categories as $category => $categoryVal){
 			$posts = Product::find()->where(['category_id' => $categoryVal['id']])->asArray()->all();       
        	foreach ($posts as $post => $postVal){
            	$urls[] = '/product/' . $postVal['id'];
        	}
            $urls[] = '/category/' . $categoryVal['id'];
        }

        $pages = Page::find()->where(['enabled' => 1])->asArray()->all();
 		foreach ($pages as $page => $pageVal){
            $urls[] = '/page/' . $pageVal['alias'];
        }
 
        $host = Yii::$app->request->hostInfo;
 
        echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        foreach ($urls as $url){
            echo '<url>
                <loc>' . $host . $url . '</loc>
                <changefreq>monthly</changefreq>
                <priority>0.5</priority>
            </url>';
        }
        echo '</urlset>';   
        Yii::$app->end();            
    }
}

Do some while true and through for, like $i = 0, $i++, and the url is example.com/category/e?page=$i
And another question - do search engines need it at all or is there no need to make such a thorough map?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-10-15
@waspmax1

You have a query in the database in a cycle, this is bad! It is better to make 2 separate requests than to produce requests in a cycle
. Eliseev is fine with this:
https://github.com/ElisDN/yii2-demo-shop/blob/mast...
he made a separate service for Sitemap:
https:/ /github.com/ElisDN/yii2-demo-shop/tree/mast...
Also layout in the controller! In general, break the generation of different entities into actions.
you can use the DataProvider:

$dataProvider = new ActiveDataProvider([
    'query' => Page::find()->where(['enabled' => 1]),
    'pagination' => array('pageSize' => 50),
]);

echo \yii\widgets\LinkPager::widget([
    'pagination'=>$dataProvider->pagination,
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question