b2bcat / sitemap
Site map generator
v1.0.0
2022-11-21 09:03 UTC
Requires
- php: ^8.0
- ext-mbstring: *
- ext-xmlwriter: *
- illuminate/contracts: ^9.0
- symfony/validator: ^6.1
Requires (Dev)
- ext-xmlreader: *
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
README
Installation
You can install the package via composer:
composer require b2bcat/sitemap
Basic Usage
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 Package
You can make the artisan command like this example:
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; } }
That command must be scheduled in the console kernel:
// app/Console/Kernel.php protected function schedule(Schedule $schedule) { ... $schedule->command('sitemap:generate')->daily(); ... }
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.