Answer the question
In order to leave comments, you need to log in
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();
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question