此包已被弃用,不再维护。作者建议使用 spatie/laravel-sitemap 包。

PHP框架的简单站点地图生成器。

v5.5.0 2022-03-31 10:40 UTC

README

Laravel Sitemap

PHP框架的简单站点地图生成器。

StyleCI Total Downloads Latest Stable Version Latest Unstable Version License

注意!

此包已被弃用,不再维护。作者建议使用 spatie/laravel-sitemap 包。

安装

Laravel框架5.4和5.5版本,使用 版本3

要获取Laravel Sitemap的最新版本,只需使用 Composer 引入项目。

composer require andrey-helldar/sitemap

当然,您也可以手动更新require块并运行 composer update

{
    "require": {
        "andrey-helldar/sitemap": "^5.0"
    }
}

如果您不使用自动发现,请将 ServiceProvider 添加到 config/app.php 文件中的providers数组。

Helldar\Sitemap\ServiceProvider::class,

您还可以发布配置文件以更改实现(例如,接口到特定类)。

php artisan vendor:publish --provider="Helldar\Sitemap\ServiceProvider"

现在您可以使用 app('sitemap') 方法。

升级指南

配置

要配置生成,您需要填写 config/sitemap.php 文件中的 models 数组。

'models' => [
    \App\User::class => [
        'route' => 'route.name',
        'route_parameters' => [
            'slug' => 'table_field_for_slug',
            'foo'  => 'table_field_for_foo',
            bar'  => 'my_relation.slug',
            'baz',
        ],
        'lastmod' => 'updated_at',
        'frequency' => 'daily',
        'priority'  => 0.8,
    ],
]

作为数组的关键,您必须使用将定义规则的模型名称。

  • route - 生成URL的路线名称。
  • route_parameters - 传递给URL生成方法的参数,其中
    • 键是路由参数的名称。如果路由参数的名称与数据库中列的名称匹配,则可以指定仅值。
    • 值是数据库中用于替换值的列的名称。如果需要从链接中获取值,则应通过点指定该值。例如:'bar' => 'my_relation.slug'
  • lastmod - 包含记录日期的列的名称。如果没有,则使用当前日期。如果模型不需要考虑时间字段,则将 lastmod 参数设置为 false
  • frequency - 内容刷新率的值。这对于某些搜索引擎机器人是必要的。您也可以使用 Helldar\Sitemap\Services\Sitemap 类中的常量。
  • priority - 模型记录的引用优先级。

如果任何模型值未定义,则将使用全局值。

使用

手动

您还可以传递手动创建的项目数组

use Carbon\Carbon;
use Helldar\Sitemap\Helpers\Variables;
use Helldar\Sitemap\Services\Sitemap;

$items_a = [];
$items_b = [];

for ($i = 0; $i < 3; $i++) {
    $item = app('sitemap')->makeItem()
        ->changefreq('weekly')
        ->lastmod(Carbon::now())
        ->loc("http://mysite.local/page/" . $i);

    array_push($items_a, $item);
}


for ($i = 0; $i < 3; $i++) {
    $item = app('sitemap')->makeItem()
        ->changefreq(Variables::FREQUENCY_WEEKLY)
        ->lastmod(Carbon::now())
        ->loc("http://mysite.local/offers/" . $i);

    array_push($items_b, $item);
}

return app('sitemap')
         ->manual($items_a, $items_b)
         ->show();

返回

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <changefreq>weekly</changefreq>
        <lastmod>2018-03-06T12:30:17+03:00</lastmod>
        <loc>http://mysite.local/page/0</loc>
        <priority>0.5</priority>
    </url>
    <url>
        <changefreq>weekly</changefreq>
        <lastmod>2018-03-06T12:38:24+03:00</lastmod>
        <loc>http://mysite.local/page/1</loc>
        <priority>0.5</priority>
    </url>
    <url>
        <changefreq>weekly</changefreq>
        <lastmod>2018-03-06T12:30:17+03:00</lastmod>
        <loc>http://mysite.local/page/2</loc>
        <priority>0.5</priority>
    </url>
    <url>
        <changefreq>weekly</changefreq>
        <lastmod>2018-03-06T12:30:17+03:00</lastmod>
        <loc>http://mysite.local/offers/0</loc>
        <priority>0.5</priority>
    </url>
    <url>
        <changefreq>weekly</changefreq>
        <lastmod>2018-03-06T12:38:24+03:00</lastmod>
        <loc>http://mysite.local/offers/1</loc>
        <priority>0.5</priority>
    </url>
    <url>
        <changefreq>weekly</changefreq>
        <lastmod>2018-03-06T12:30:17+03:00</lastmod>
        <loc>http://mysite.local/offers/2</loc>
        <priority>0.5</priority>
    </url>
</urlset>

您还可以将模型构建器的数据与手动传递的数据结合

$query1 = \App\Catalog::query()->where('id', '>', '1000');
$query2 = \App\News::query()->where('category_id', 10);
$query3 = \App\Pages::query();

return app('sitemap')
         ->builders($query1, $query2, $query3)
         ->manual($items1, $items2, $items3)
         ->show();

图片

您还可以手动创建一个图片项目数组

use Helldar\Sitemap\Services\Sitemap;

$items = [];

for ($i = 0; $i < 2; $i++) {
    $item = app('sitemap')->makeImages()
        ->loc("http://mysite.local/page/" . $i)
        ->image("http://mysite.local/images/1.jpg", "My Title 1-".$i, "Caption for image", "Limerick, Ireland", "Royalty free")
        ->image("http://mysite.local/images/2.jpg", "My Title 2-".$i)
        ->image("http://mysite.local/images/3.jpg");
    
    array_push($items, $item);
}

return app('sitemap')
         ->images($items)
         ->show();

返回

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    <url>
        <loc>http://mysite.local/page/0</loc>
        <image:image>
            <image:loc>http://mysite.local/images/1.jpg</image:loc>
            <image:title>My Title 1-0</image:title>
            <image:caption>Caption for image</image:caption>
            <image:geo_location>Limerick, Ireland</image:geo_location>
            <image:license>Royalty free</image:license>
        </image:image>
        <image:image>
            <image:loc>http://mysite.local/images/2.jpg</image:loc>
            <image:title>My Title 2-0</image:title>
        </image:image>
        <image:image>
            <image:loc>http://mysite.local/images/3.jpg</image:loc>
        </image:image>
    </url>
    <url>
        <loc>http://mysite.local/page/1</loc>
        <image:image>
            <image:loc>http://mysite.local/images/1.jpg</image:loc>
            <image:title>My Title 1-1</image:title>
            <image:caption>Caption for image</image:caption>
            <image:geo_location>Limerick, Ireland</image:geo_location>
            <image:license>Royalty free</image:license>
        </image:image>
        <image:image>
            <image:loc>http://mysite.local/images/2.jpg</image:loc>
            <image:title>My Title 2-1</image:title>
        </image:image>
        <image:image>
            <image:loc>http://mysite.local/images/3.jpg</image:loc>
        </image:image>
    </url>
</urlset>

注意!由于文档结构不同,在尝试调用 show() 方法时,如果没有调用其他方法,则只会显示图像映射。

示例

// Will show the image map.
return app('sitemap')
         ->images($items)
         ->show();

// Shows the map for `builders`. The image map will be ignored.
return app('sitemap')
         ->builders($query1, $query2, $query3)
         ->images($items)
         ->show();

保存到单个文件时也适用相同的原理 - 图片将被忽略。但是,当保存到多个文件时,映射将成功创建。

显示

要在屏幕上显示内容,请使用 show() 方法

$query1 = \App\Catalog::query()->where('id', '>', '1000');
$query2 = \App\News::query()->where('category_id', 10);
$query3 = \App\Pages::query();

return app('sitemap')
         ->builders($query1, $query2, $query3)
         ->show();

要返回页面内容,请添加任何路由

app('route')->get('sitemap', function() {
    $query1 = \App\Catalog::query()->where('id', '>', '1000');
    $query2 = \App\News::query()->where('category_id', 10);
    $query3 = \App\Pages::query();

    return app('sitemap')
             ->builders($query1, $query2, $query3)
             ->show();
});

并访问您的URL。例如:http://mysite.dev/sitemap

保存

如果选项 separate_files 已禁用

要将内容保存到文件,请使用 save() 方法

$query1 = \App\Catalog::query()->where('id', '>', '1000');
$query2 = \App\News::query()->where('category_id', 10);
$query3 = \App\Pages::query();

app('sitemap')
     ->builders($query1, $query2, $query3)
     ->save();

如果您想保存多个文件,请将文件路径作为参数传递给 save() 方法

$query1 = \App\Catalog::query()->where('id', '>', '1000');
$query2 = \App\News::query()->where('category_id', 10);
$query3 = \App\Pages::query();

app('sitemap')
     ->builders($query1, $query2, $query3)
     ->save('sitemap-1.xml');

app('sitemap')
     ->builders($query1, $query2, $query3)
     ->save('foo/bar/sitemap-2.xml');

如果选项 separate_files 已启用

要将内容保存到单独的文件中,请使用具有 'separate_files' => true 参数的 save() 方法,在 config/sitemap.php 文件中。

$query1 = \App\Catalog::query()->where('id', '>', '1000');
$query2 = \App\News::query()->where('category_id', 10);
$query3 = \App\Pages::query();

app('sitemap')
     ->builders($query1, $query2, $query3)
     ->save();

在这种情况下,文件名称将是设置中的默认名称:'filename' => 'sitemap.xml'。文件将通过 Storage 门面保存(见 config/sitemap.php)。

每个模型构建器都将分别处理并保存到单独的文件中,共享文件将包含对其的引用(使用选定的 public 存储名称)

/storage/sitemap.xml   // general file
/storage/sitemap-1.xml // generated file for the $query1 collection
/storage/sitemap-2.xml // generated file for the $query2 collection
/storage/sitemap-3.xml // generated file for the $query3 collection
<?xml version="1.0" encoding="utf-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <lastmod>2018-07-05T13:51:40+00:00</lastmod>
    <loc>http://example.com/storage/sitemap-1.xml</loc>
  </sitemap>
  <sitemap>
    <lastmod>2018-07-05T13:51:41+00:00</lastmod>
    <loc>http://example.com/storage/sitemap-2.xml</loc>
  </sitemap>
  <sitemap>
    <lastmod>2018-07-05T13:51:41+00:00</lastmod>
    <loc>http://example.com/storage/sitemap-3.xml</loc>
  </sitemap>
</sitemapindex>

如果您使用的是多域名应用程序,可以在设置中预指定要生成的指向其他文件的链接的域名

'domains' => [
    'foo' => env('APP_URL'), // http://example.com
    'bar' => 'http://foo.bar',
],
app('sitemap')
     ->builders($query1, $query2, $query3)
     ->domain('foo')
     ->save();
     
app('sitemap')
     ->builders($query1, $query2, $query3)
     ->domain('bar')
     ->save();

此方法将创建具有以下链接的文件

<?xml version="1.0" encoding="utf-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <lastmod>2018-07-05T13:51:40+00:00</lastmod>
    <loc>http://example.com/storage/sitemap-1.xml</loc>
  </sitemap>
  <sitemap>
    <lastmod>2018-07-05T13:51:41+00:00</lastmod>
    <loc>http://example.com/storage/sitemap-2.xml</loc>
  </sitemap>
  <sitemap>
    <lastmod>2018-07-05T13:51:41+00:00</lastmod>
    <loc>http://example.com/storage/sitemap-3.xml</loc>
  </sitemap>
</sitemapindex>

<?xml version="1.0" encoding="utf-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <lastmod>2018-07-05T13:51:40+00:00</lastmod>
    <loc>http://foo.bar/storage/sitemap-1.xml</loc>
  </sitemap>
  <sitemap>
    <lastmod>2018-07-05T13:51:41+00:00</lastmod>
    <loc>http://foo.bar/storage/sitemap-2.xml</loc>
  </sitemap>
  <sitemap>
    <lastmod>2018-07-05T13:51:41+00:00</lastmod>
    <loc>http://foo.bar/storage/sitemap-3.xml</loc>
  </sitemap>
</sitemapindex>

默认情况下,将使用在 .env 文件的 APP_URL 中指定的域名。

如果您想保存多个文件,请使用具有 'separate_files' => true 参数的 save($path) 方法在 config/sitemap.php 文件中传递文件路径

$query1 = \App\Catalog::query()->where('id', '>', '1000');
$query2 = \App\News::query()->where('category_id', 10);
$query3 = \App\Pages::query();

$manual_items = [];

for ($i = 0; $i < 3; $i++) {
    $item = app('sitemap')->makeItem()
        ->changefreq('weekly')
        ->lastmod(Carbon\Carbon::now())
        ->loc("http://mysite.local/page/" . $i);

    array_push($manual_items, $item);
}

app('sitemap')
     ->builders($query1, $query2, $query3)
     ->manual($manual_items)
     ->save(public_path('first.xml'));

app('sitemap')
     ->builders($query1, $query2, $query3)
     ->manual($manual_items)
     ->save(storage_path('foo/bar/second.xml'));

将创建文件

/storage/first.xml   // general file
/storage/first-1.xml // generated file for the $query1 collection
/storage/first-2.xml // generated file for the $query2 collection
/storage/first-3.xml // generated file for the $query3 collection
/storage/first-4.xml // generated file for the $manual_items collection

/storage/foo/bar/second.xml   // general file
/storage/foo/bar/second-1.xml // generated file for the $query1 collection
/storage/foo/bar/second-2.xml // generated file for the $query2 collection
/storage/foo/bar/second-3.xml // generated file for the $query3 collection
/storage/foo/bar/second-4.xml // generated file for the $manual_items collection

SEO

当搜索引擎机器人索引网站时,站点地图非常有用。如果您使用多个文件的站点地图,只需在 robots.txt 文件中添加主文件的链接即可。

例如,您创建了几个文件

/storage/sitemaps/promo.xml
/storage/sitemaps/promo-1.xml
/storage/sitemaps/promo-2.xml
/storage/sitemaps/promo-3.xml

robots.txt 文件中,您只需指定主文件的链接

Sitemap: http://example.com/storage/sitemaps/promo.xml

其余的搜索引擎机器人将为您完成。

祝您享受!

许可证

此软件包根据 MIT 许可证 许可。