alantmiller/php-sitemap-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alantmiller/php-sitemap-generator

Composer 安装命令:

composer require alantmiller/php-sitemap-generator

包简介

Alan's PHP sitemap generator

README 文档

README

A completely new version of my old XML Stemap Generator application

// autoload.php

require 'vendor/autoload.php';

use alantmiller\PhpSitemapGenerator\Sitemap;
use alantmiller\PhpSitemapGenerator\Entry;
use alantmiller\PhpSitemapGenerator\Generator;
use alantmiller\PhpSitemapGenerator\Config;

// Create and populate config 
$config = new Config();

// Domain regex allows alphanumeric, dashes, dot 
$domainRegex = '/^[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/';
$domain = 'www.example.com';

if (preg_match($domainRegex, $domain)) {
  // Valid domain, use it
  $config->setDomain($domain);
} else {
  // Invalid domain, throw error
  throw new Exception('Invalid domain format');
}

$config->setFilename('sitemap.xml');

// Sitemap
$sitemap = new Sitemap($config);

// Add entry with video  
$video = [
  'thumbnail' => '/images/video1_thumbnail.jpg',
  'title' => 'Awesome Video'
];

$entry = new Entry('/video1', 0.9); 
$entry->setVideos([$video]);
$sitemap->addEntry($entry);

// Add entry with images
$images = [
  [
     'loc' => '/images/image1.jpg',
     'title' => 'Image 1'
  ],
  [
     'loc' => '/images/image2.jpg',
     'title' => 'Image 2'
  ]
];


$entry = new Entry('/page1', 0.8);
$entry->setImages($images);
$sitemap->addEntry($entry);

$dateTime = new DateTime(); // current date

// Create random dates
for($i = 1; $i < 8; $i++) {

  $dateTime = new DateTime("+$i days");  
  $page = "/page$i";
  $priority = mt_rand(10, 20) / 10;
  
  $sitemap->addEntry($page, $priority, 'monthly', $dateTime);
}

// Generate
$generator = new Generator($config);
$sitemap = $generator->toString();

// Output sitemap
echo $sitemap;

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-10