40katty04/cakephp-sitemap

CakePHP 4.x 应用程序的网站地图生成器插件

安装次数: 73

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 6

类型:cakephp-plugin

3.0 2020-11-29 02:19 UTC

This package is auto-updated.

Last update: 2024-09-29 05:21:41 UTC


README

CakePHP 4.1 应用程序的网站地图生成器插件

安装

此软件包可以通过 Packagist 实现轻松安装

composer require 40katty04/cakephp-sitemap "~2.0"

然后请确保在您的应用程序中正常加载插件。例如:

# somewhere in config/bootstrap.php

Plugin::load('Sitemap', [
    'routes' => true,
    ]);

最后,设置您的配置数组(见下文)

配置

插件将使用 Configure::read('Sitemap') 寻找一个包含信息的数组,因此请确保在启动过程中加载此类数组。最简单的方法是将它添加到您的 config/app.php 文件中。

插件会查找两个部分:静态页面和动态页面。

静态页面

静态页面位于 Sitemap.static 之下,该部分期望一个包含 URL 的数组。这可以接受 Router::url() 所接受的任何 URL。

	'Sitemap' => 
		'static' => 
			['_name' => 'pages:about'],
			'http://example.com/search',
			['controller'=>'Pages', 'action'=>'display', 'terms-of-service'],

动态页面

您可以在 Sitemap.dynamic 下嵌套一组配置设置来动态创建链接

	'Sitemap' => 
		'dynamic' => 
			'Items' =>  #the name of the model to get entities for
				'cachekey' => 'sitemap', 	# cachekey to use (e.g. from Configure::read('Cache.sitemap'))
				'finders' => [ .. ], 		# array of model-layer finders for getting entities
				'xmlTags' =>				# xml tags to output with each sitemap line
					'loc' => 'url'				# default 'url'; entity attribute name, or array, or string
					'priority' => 0.5			# default 0.5; 0 to 1 priority
					'lastmod' => 'updated'		# default 'updated'; entity attribute giving lastmod time
					'changefreq' => 'daily'		# default 'daily'; always, hourly, daily, weekly, yearly, never		
				

示例配置

以下是一个使用此插件的项目的示例配置

	# .. in config/app.php
	...,
    'Sitemap' => [
        'static' => [
            ['_name' => 'user-register'],
            ['_name' => 'user-resetpw'],
            ['_name' => 'user-login'],
            ['_name' => 'user-logout'],
            ['_name' => 'user-dashboard'],
            ['_name' => 'privacy'],
            ["_name" => "contact-us"],
            ["_name" => "rewards"],
            ["_name" => "terms-of-service"],
            ["_name" => "about-us"],            
            ["_name" => "search"],
        ],
        'dynamic' => [
            'Categories' => [
                'cacheKey' => 'sitemap',
                'finders' => [
                    'all' => [
		    	'condition' => [
			    'Categories.enabled' => true
			]
		    ],
                ],
                'xmlTags'=> [
                    'loc' => 'permalink',
                    'priority' => '0.9',
                    'changefreq' => 'always',
                ],
            ],
            'Products' => [
                'cacheKey' => 'sitemap',
                'finders' => [
                    'all' => [
		    	'condition' => [
			    'Products.enabled' => 'true',
			    function ($exp) {
                            	return $exp->notIn('Products.status', 
				    [
				       'Stalled', 
				       'Placed', 
				       'Lost to Competition', 
				       'Filled by Client', 
				       'Cancelled'
				    ]);
                            }
			],
		    ],
                ],
                'xmlTags'=> [
                    'loc' => [
                    	'urlBody' => '/product',
                    	'queryParams' => ['productId' => 'id'] // queryParam => CollumnNameInDb
                    ],
                    'priority' => '0.9',
                    'changefreq' => 'always',
                ],
            ],
        ],
    ],

它会产生以下结果

<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
        <loc>http://example.com/</loc>
        <changefreq>always</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc>http://example.com/register</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/reset-password</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/login</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/logout</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/my-account</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/privacy</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/contact-us</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/rewards</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/terms-of-service</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/about-us</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/search/</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>http://example.com/product/spice-widgets</loc>
        <priority>0.9</priority>
        <changefreq>always</changefreq>
    </url>
    <url>
        <loc>http://example.com/product/posh-widgets</loc>
        <priority>0.9</priority>
        <changefreq>always</changefreq>
    </url>
    <url>
        <loc>http://example.com/product?productId=45673</loc>
        <priority>0.9</priority>
        <changefreq>always</changefreq>
    </url>
    <url>
        <loc>http://example.com/product?productId=89654</loc>
        <priority>0.9</priority>
        <changefreq>always</changefreq>
    </url>
</urlset>