akse1a/seo
Composer 安装命令:
composer require akse1a/seo
包简介
A PHP library for working with SEO meta tags, Open Graph, Schema.org markup, and sitemap.xml generation
README 文档
README
A PHP library for working with SEO meta tags, Open Graph, Schema.org markup, and sitemap.xml generation.
Installation
Install via Composer:
composer require akse1a/seo
Or add to your composer.json:
{
"require": {
"akse1a/seo": "^1.0"
}
}
Usage
Basic Usage
<?php use Akse1a\Seo\Seo; $seo = new Seo(); // Set basic meta tags $seo->setTitle('Page Title') ->setDescription('Page description') ->setKeywords(['keyword', 'word', 'more']); // Open Graph $seo->setImage('https://example.com/image.jpg') ->setUrl('https://example.com/page') ->setType(\Akse1a\Seo\Enum\OpenGraphType::ARTICLE); // Output HTML echo $seo->render();
Adding Custom Meta Tags
$seo->addMetaTag('author', 'Author Name') ->addMetaTag('robots', 'index, follow');
Adding Open Graph Tags
$seo->addOpenGraph('og:site_name', 'Site Name') ->addOpenGraph('og:locale', 'en_US');
Schema.org Markup
$seo->addSchema([ '@context' => 'https://schema.org', '@type' => 'Article', 'headline' => 'Article Headline', 'author' => [ '@type' => 'Person', 'name' => 'Author Name' ] ]);
Generating sitemap.xml
Basic Example
<?php use Akse1a\Seo\Sitemap; use Akse1a\Seo\Enum\ChangeFrequency; use Akse1a\Seo\Exception\RuntimeException; $sitemap = new Sitemap(); // Add URLs $sitemap->addUrl('https://example.com/') ->addUrl( 'https://example.com/page', '2024-01-15', // lastmod 'weekly', // changefreq 0.8 // priority ) ->addUrlWithNow('https://example.com/new-page', ChangeFrequency::DAILY, 1.0); // Generate XML string $xml = $sitemap->generate(); echo $xml; // Save to file sitemap.xml try { $sitemap->save('sitemap.xml'); echo "Sitemap successfully saved to sitemap.xml\n"; } catch (RuntimeException $e) { echo "Error saving: " . $e->getMessage() . "\n"; }
Full Example with Error Handling
<?php use Akse1a\Seo\Sitemap; use Akse1a\Seo\Enum\ChangeFrequency; use Akse1a\Seo\Exception\InvalidArgumentException; use Akse1a\Seo\Exception\RuntimeException; try { $sitemap = new Sitemap(); // Add URLs with different parameters $sitemap->addUrl('https://example.com/') ->addUrl( 'https://example.com/about', new \DateTime('2024-01-15'), ChangeFrequency::MONTHLY, 0.8 ) ->addUrl( 'https://example.com/blog', '2024-01-20', ChangeFrequency::WEEKLY, 0.9 ) ->addUrlWithNow('https://example.com/contact', ChangeFrequency::YEARLY, 0.7); // Generate XML $xml = $sitemap->generate(); // Save to file $filename = __DIR__ . '/sitemap.xml'; $sitemap->save($filename); // Check if file was created if (file_exists($filename)) { echo sprintf("Sitemap successfully created: %s (%d bytes)\n", $filename, filesize($filename)); } } catch (InvalidArgumentException $e) { echo "Validation error: " . $e->getMessage() . "\n"; } catch (RuntimeException $e) { echo "Runtime error: " . $e->getMessage() . "\n"; }
Output Sitemap Directly to Browser
<?php use Akse1a\Seo\Sitemap; $sitemap = new Sitemap(); $sitemap->addUrl('https://example.com/') ->addUrl('https://example.com/page'); // Output with correct HTTP headers $sitemap->output(); exit;
Parameters for addUrl()
$loc(required) - Page URL (automatically validated)$lastmod(optional) - Last modification date (format: Y-m-d or DateTimeInterface object)$changefreq(optional) - Change frequency: string orChangeFrequencyenum$priority(optional) - Priority from 0.0 to 1.0 (automatically validated)
Duplicate URL Handling
The library automatically prevents duplicate URLs in the sitemap. If you try to add the same URL twice:
- Duplicate URLs are automatically detected using URL normalization (case-insensitive scheme/host, trailing slash handling)
- Existing URL is updated with new parameters if provided (lastmod, changefreq, priority)
- Original parameters are preserved if new ones are not provided
Examples of URLs considered duplicates:
https://example.com/pageandhttps://example.com/page/(trailing slash)https://Example.com/pageandhttps://example.com/page(case difference)http://example.com/pageandhttps://example.com/page(different schemes are NOT considered duplicates)
You can check if a URL exists using:
if ($sitemap->hasUrl('https://example.com/page')) { echo "URL already exists in sitemap"; }
Error Handling
The library throws exceptions for invalid data:
use Akse1a\Seo\Exception\InvalidArgumentException; use Akse1a\Seo\Sitemap; try { $sitemap = new Sitemap(); $sitemap->addUrl('invalid-url'); // Will throw InvalidArgumentException } catch (InvalidArgumentException $e) { echo $e->getMessage(); }
Example with Multiple Pages
$sitemap = new Sitemap(); $pages = [ ['url' => 'https://example.com/', 'priority' => 1.0, 'changefreq' => 'daily'], ['url' => 'https://example.com/about', 'priority' => 0.8, 'changefreq' => 'monthly'], ['url' => 'https://example.com/contact', 'priority' => 0.7, 'changefreq' => 'monthly'], ]; foreach ($pages as $page) { $sitemap->addUrlWithNow($page['url'], $page['changefreq'], $page['priority']); } $sitemap->save('sitemap.xml');
Development
Requirements
- PHP 8.0 or higher
- Composer
Install Dependencies for Development
composer install
Run Examples
# Run sitemap generation example
php example_sitemap.php
License
MIT
Author
akse1a
akse1a/seo 适用场景与选型建议
akse1a/seo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「schema」 「Sitemap」 「html」 「seo」 「meta」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 akse1a/seo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 akse1a/seo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 akse1a/seo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony sitemap generator
Extension for Opis JSON Schema
PHP Simple Sitemap Generator
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
Interfaces for web resource models and services to retrieve and create them
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-09