本文主要是介绍laravel sitemap.xml生成,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
sitemap.xml格式
<?xml version="1.0" encoding="UTF-8"?><urlset><url><loc>http://www.example.com/</loc><lastmod>2010-01-01</lastmod><changefreq>always</changefreq><priority>1.0</priority></url><url><loc>http://www.example.com/1.html</loc><lastmod>2010-02-14</lastmod><changefreq>weekly</changefreq><priority>0.8</priority></url>
</urlset>
class SitemapController extends ApplicationController
{public function index() {$xml = $this->sitemap();$file = fopen('sitemap.xml', 'w');fwrite($file, $xml);fclose($file);return redirect()->action('xxx@index')->with('success', __('common.success'));}private function sitemap() {$xml = '<?xml version="1.0" encoding="utf-8"?>';$xml .= '<urlset>';// 文章分类$categories = Category::all();foreach($categories as $data) {$xml .= $this->execute_xml('articles/'.$data->id);}// 文章Article::chunk(500, function($articles) use(&$xml) {foreach($articles as $data) {$xml .= $this->execute_xml('article/'.$data->id);}});$xml .= '</urlset>';return $xml;}private function execute_xml($url) {$xml_url = '<url>';$xml_url .= '<loc>'. config('app.url') . $url .'</loc>';$xml_url .= '<lastmod>'. date("Y-m-d", time()) .'</lastmod>';$xml_url .= '<changefreq>weekly</changefreq>';$xml_url .= '<priority>0.8</priority>';$xml_url .= '</url>';return $xml_url;}}
这篇关于laravel sitemap.xml生成的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!