Sitemap.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace addons\blog\controller;
  3. use think\Config;
  4. /**
  5. * Sitemap控制器
  6. * Class Sitemap
  7. * @package addons\blog\controller
  8. */
  9. class Sitemap extends Base
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $options = [
  13. 'item_key' => '',
  14. 'root_node' => 'urlset',
  15. 'item_node' => 'url',
  16. 'root_attr' => 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/"'
  17. ];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. Config::set('default_return_type', 'xml');
  22. }
  23. /**
  24. * Sitemap
  25. */
  26. public function index()
  27. {
  28. $postList = \addons\blog\model\Post::where('status', 'normal')->cache(3600)->field('id,category_id,createtime')->paginate(500000);
  29. $list = [];
  30. foreach ($postList as $index => $item) {
  31. $list[] = [
  32. 'loc' => $item->fullurl,
  33. 'priority' => 0.8
  34. ];
  35. }
  36. return xml($list, 200, [], $this->options);
  37. }
  38. }