b2bcat/sitemap

网站地图生成器

v1.0.0 2022-11-21 09:03 UTC

This package is auto-updated.

Last update: 2024-09-21 12:35:16 UTC


README

Latest Version on Packagist Total Downloads

安装

您可以通过composer安装此包

composer require b2bcat/sitemap

基本用法

use B2bcat\SiteMap\SiteMap;

$pages = [
    [
        'loc' => 'https://foo.me',
        'lastmod' => '2022-02-02 23.12.12',
        'priority' => 0.5,
        'changefreq' => 'daily' // hourly, daily, weekly
    ]   
];
$type = 'xml'; 
$path = '/var/www/site.ru/upload/sitemap.xml'

(new B2bcat\SiteMap(
    $pages,
    $type,
    $path
))

Laravel包

您可以使用如下示例制作artisan命令

namespace App\Console\Commands;

use Illuminate\Console\Command;
use B2bcat\SiteMap\Laravel\SiteMapGenerator;
use B2bcat\SiteMap\Laravel\Generator\StaticRoute;
use B2bcat\SiteMap\Laravel\Generator\DynamicRoute;
use App\Models\Product;

class SiteMapGenerateCommand extends Command
{
    public $signature = 'sitemap:generate {file_type?}';

    public $description = 'Generate site map to file. Supported formats: xml, csv, json';

    public function handle (): int
    {
       $fileType = $this->hasArgument('file_type') ? $this->argument('file_type') : 'xml';
        
        (new SiteMapGenerator())
            ->route(
                (new StaticRoute())
                    ->setRouteName('products.list') // is required
                    ->setPriority(1) // is required
                    ->setChangefreq('daily')
                    ->setLastmod('2022-11-20 01:12:10')
            )
            ->route(
                (new DynamicRoute())
                    ->setRouteName('products.show') // is required
                    ->setQueryBuilder(Product::query()) // is required
                    ->setPriority(1)
                    ->setChangefreq('daily')
                    ->setLastmodField('updated_at')
            )
            ->generate(
                '/path/to/sitemap.' . $fileType
            );

        return self::SUCCESS;
    }
}

该命令必须在控制台内核中安排

// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    ...
    $schedule->command('sitemap:generate')->daily();
    ...
}

测试

composer test

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。