webakula/sitemap

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

9.1.1 2023-04-06 14:34 UTC

README

Laravel Sitemap

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

StyleCI Total Downloads Latest Stable Version Latest Unstable Version License

安装

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

当搜索引擎爬虫索引网站时,Sitemap 非常有用。如果您使用的是分割成多个文件的 sitemap,您只需要在 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 许可证 许可。