noprotocol / laravel-sitemap
此包的最新版本(dev-master)没有提供许可信息。
基于Eloquent的简单多站点地图生成器
dev-master
2016-02-26 16:55 UTC
This package is auto-updated.
Last update: 2024-09-17 21:54:12 UTC
README
为Laravel提供的动态站点地图创建器。从简单的配置运行,可以用于创建多个站点地图。在运行多个域名和/或多个模型时非常有用。
它是如何工作的
composer install noprotocol/laravel-sitemap
php artisan vendor:publish --provider="Noprotocol\LaravelSitemap\SitemapServiceProvider"
打开config/sitemap.php文件并编辑它。已提供示例
<?php return [ /*********************** EXAMPLE ************************ 'sites' => [ 1 => [ // namespace to request from '\App\Page' => [ // query eloquent inject 'query' => function($query) { return $query; }, // the route to call 'route' => 'page', // the database column where the slug (if any) resides in 'slug' => 'slug', // the database column where the last updated date resides in 'updated' => 'updated_at', ], // urls that are not generated from DB are run straight through the xml generator 'MANUAL' => [ [ 'loc' => 'http://www.site.com/', 'lastmod' => '2016-02-25', ], ], ], 2 => [ '\App\Article' => [ 'query' => function($query) { return $query->where('something', '=', 'something'); }, 'route' => 'page', // if the url consists of multiple parts, define them as array 'slug' => [ // first part of the slug, but it needs some functionality 'category' => function($slug) { // the function to run with the url part before adding return str_slug($slug); }, 'slug' => false, // no function will run on this url part ], 'updated' => 'updated_at', ], ], ] ***************************** END EXAMPLE *****************************/ 'sites' => [ 1 => [ // urls that are not generated from DB are run straight through the xml generator 'MANUAL' => [ [ 'loc' => 'http://www.site.com/', 'lastmod' => '2016-02-25', ], ], ], ] ];
将以下内容添加到路由文件中
Route::get('sitemap.php', '\Noprotocol\LaravelSitemap\Http\Controllers\SitemapController@index');
到此为止,您已经拥有了一个可用的站点地图。默认的站点地图位于/sitemap.php
为什么是sitemap.php而不是sitemap.xml
首先,爬虫并不关心你向它们提供文件名。但我们确实看到过,当请求.xml文件时,PHP服务器不会启动。
自定义
如果您想尝试一些新事物或更改设置,可以创建自己的控制器并将路由指向那里。
<?php namespace App\Http\Controllers; use Noprotocol\LaravelSitemap\Sitemap; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Http\Requests; class SitemapController extends Controller { private $sitemap; public function __construct(Sitemap $sitemap) { $this->sitemap = $sitemap; } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return response($this->sitemap->init(1)->get(false), '200') ->header('Content-Type', 'text/xml'); } }
选项
在构建响应时,您有一些选项
无缓存,每次请求都创建一个新站点地图
$this->sitemap->init(1)->get(false);
设置一个替代的间隔(始终、每小时、每天(默认)、每周、每月、每年、永不)
setInterval([interval])
设置缓存结果的时间
cache($minutes)
其他所有内容都在配置中设置。