我已经使用Composer为我的Laravel应用程序this制作了github程序包,该程序可以创建站点地图,但是我无法理解这些站点地图的渲染位置。
点击网站的/sitemap.xml URL位置后,我很幸运无法找到主站点地图,但是我无法找到该软件包支持的其他站点地图,例如Google新闻站点地图。
负责任的功能似乎是这一功能,但是我的编码技能使我无法理解渲染路径:
/**
* Returns document with all sitemap items from $items array
*
* @param string $format (options: xml, html, txt, ror-rss, ror-rdf, google-news)
* @param string $style (path to custom xls style like '/styles/xsl/xml-sitemap.xsl')
*
* @return View
*/
public function render($format = 'xml', $style = null)
{
// limit size of sitemap
if ($this->model->getMaxSize() > 0 && count($this->model->getItems()) > $this->model->getMaxSize())
{
$this->model->limitSize($this->model->getMaxSize());
}
else if ('google-news' == $format && count($this->model->getItems()) > 1000)
{
$this->model->limitSize(1000);
}
else if ('google-news' != $format && count($this->model->getItems()) > 50000)
{
$this->model->limitSize();
}
$data = $this->generate($format, $style);
if ('html' == $format)
{
return $data['content'];
}
return $this->response->make($data['content'], 200, $data['headers']);
}
我使用以下功能在HomeController上生成我的站点地图:
public function sitemap()
{
$settings_general = Utils::getSettings("general");
if ($settings_general->generate_sitemap == 1) {
// create new sitemap object
$sitemap = App::make("sitemap");
// get all posts from db
$posts = DB::table('posts')->orderBy('created_at', 'desc')->limit(600)->get();
// add every post to the sitemap
foreach ($posts as $post) {
$sitemap->add(URL::to('/') . "/" . $post->slug, $post->updated_at, '1', 'hourly', null, $post->title);
}
$pages = DB::table('pages')->orderBy('created_at', 'desc')->get();
// add every page to the sitemap
foreach ($pages as $page) {
$sitemap->add(URL::to('/') . "/" . $page->slug, $page->updated_at, '1', 'hourly', null, $page->title);
}
$categories = DB::table('categories')->orderBy('created_at', 'desc')->get();
// add every category to the sitemap
foreach ($categories as $category) {
$sub_categories = SubCategories::where('parent_id', $category->id)->get();
$sitemap->add(URL::to('/') . "/category/" . $category->slug, $category->updated_at, '1', 'hourly', null, $category->title);
foreach ($sub_categories as $sub_category) {
$sitemap->add(URL::to('/') . "/category/" . $category->slug . "/" . $sub_category->slug, $category->updated_at, '1', 'hourly', null, $category->title);
}
}
return $sitemap->render('xml');
}
}