ash/monal-sitemap

Monal CMS的网站地图生成器。

0.1.1 2014-07-09 15:12 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:49:34 UTC


README

Monal CMS的网站地图生成器。Monal CMS允许按包注册实体。

默认情况下,Monal Sitemap将网站地图放在sitemapsitemap.xml中。可以通过config['filename']选项自定义此名称。

安装

  1. ash\monal-sitemap添加为Composer依赖项并安装
  2. 'Ash\MonalSitemap\MonalSitemapServiceProvider'添加到应用提供者
  3. 'Sitemap' => 'Ash\MonalSitemap\Facades\Sitemap'添加到应用别名

注册网站地图部分

Monal Sitemap由实体集合组成。实体可以是单个实体(一个SitemapEntity)或实体集合(一个SitemapCollection)。

使用register函数注册实体。

Sitemap::register( string $name, Callable $register [, $cache ] )

$name

集合/实体的名称。必须是唯一的。

$register

一个返回SitemapCollectionSitemapEntity实例的函数。

$cache

覆盖默认缓存(1440分钟)。设置为自定义的分钟数,或false以禁用缓存。

SitemapEntity和SitemapCollection

SitemapEntitySitemapCollection在所有方面都相同,除了SitemapCollection可以包含实体集合的能力。

$entity/$collection->uri - 必须要求

实体的完整URI(不包括域名)。

$entity/$collection->name - 必须要求

$entity/$collection->lastmod

$entity/$collection->changefreq

$entity/$collection->priority

$collection->add($collection/$entity)

将集合或实体添加到集合中。可以是链式操作或提供数组,例如

$collection
	->add($subCollection)
	->add($subCollection2);
	
$collection->add([
	$subCollection,
	$subCollection2
]);

示例

// Register a collection named 'products'

Sitemap::register('products', function($uri) {

	// The second param of the register function is a callback
// that should return a collection or an entity
	
	// Create a new collection
	$collection = App::make('SitemapCollection');
	
	// Set the collection URI
	// $uri passed to this callback is the same as the collection name (products)
	$collection->uri = $uri;
	
	$collection->name = 'Products';

	// Gather data for a resource	
	foreach(Product::all() as $product) {
		
		// Create a new single entity
		$entity = App::make('SitemapEntity');
		
		// Set entity URI
		$entity->uri = 'products/' . $product->uri;
		
		$entity->name = $product->name;
		
		// Set entity lastmod
		// The default Laravel updated_at timestamp works here
		$entity->lastmod = $product->updated_at;
		
		// Add the single entity to the collection
		$collection->add($entity);
		
	}

	return $collection;

});

模板化

网站地图使用名为sitemap.blade.php的视图,您可以将它发布到本地视图目录以进行自定义。网站地图对象作为$sitemap传递给视图。

您可以选择手动遍历集合和实体,或者使用$sitemap->html()函数输出包含网站地图的<ul>

遍历网站地图以创建自定义输出非常简单,请参见默认视图中Blade的@each函数的使用。

待办事项

  • 这应该/可以是一个通用的Laravel工具,而不是专门为Monal设计?
  • 添加实体验证 - $entity->valid()在添加时进行检查,目前始终返回true
  • 单元测试
  • 添加默认缓存持续时间作为配置项
  • 实现XML版本的gzip压缩

发布

0.1.1 - 09/07/14

  • 修复XML输出

0.1.0 - 02/07/14

  • 改进HTML模板(现在使用可以发布到本地视图目录的Blade视图)

0.0.1 - 01/07/14