Как создать Sitemap без плагина.



Краткая инструкция, как создавать Sitemap для Yii framework. В url rules:


 'sitemap.xml'=>'site/sitemapxml',

В site controller:


public function actionSitemapxml()
{

$posts=Post::model()->findAll(array(
'order'=>'create_time DESC',
'condition'=>'status="swPost/published"',
));

$pages=Page::model()->findAll(array(
'order'=>'create_time DESC',
'condition'=>'status="swPost/published"',
));

header('Content-Type: application/xml');
$this->renderPartial('../site/sitemapxml',array('posts'=>$posts,'pages'=>$pages));
}

views/site/sitemapxml:


<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>

<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

<?php foreach($posts as $model): ?>
<url>
<loc><?php echo CHtml::encode($this->createAbsoluteUrl('post/view',array('slug'=>$model->slug))); ?></loc>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<?php endforeach; ?>

<?php foreach($pages as $model): ?>
<url>
<loc><?php echo CHtml::encode($this->createAbsoluteUrl('page/view',array('slug'=>$model->slug))); ?></loc>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<?php endforeach; ?>

</urlset>

 

846   0  

Comments

    Ничего не найдено.