fuelviews/laravel-sitemap

Laravel 站点地图包


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel 站点地图是一个强大且易于使用的解决方案,旨在自动为您的 Laravel 应用程序生成站点地图。

安装

您可以通过 composer 需求此包及其依赖项

composer require fuelviews/laravel-sitemap

您可以通过以下命令手动发布配置文件

php artisan vendor:publish --provider="Fuelviews\Sitemap\SitemapServiceProvider" --tag="sitemap-config"

这是已发布配置文件的内容

return [
    /**
     * Specifies the default filesystem disk that should be used.
     * The 'public_path' disk is typically used for files that need to be publicly accessible to users.
     * This setting can influence where files, such as generated sitemaps, are stored by default.
     */
    'disk' => 'public',

    /**
     * Determines whether the index page should be excluded from the sitemap.
     * Setting this to `true` will exclude the index page, `false` will include it.
     */
    'exclude_subcategory_sitemap_links' => true,

    /**
     * Controls whether redirect URLs should be excluded from the sitemap.
     * When set to `true`, all redirects are excluded to ensure the sitemap only contains direct links.
     */
    'exclude_redirects' => true,

    /**
     * An array of route names to be excluded from the sitemap.
     * Useful for excluding specific pages that should not be discoverable via search engines.
     */
    'exclude_route_names' => [
    ],

    /**
     * Specifies paths that should be excluded from the sitemap.
     * Any routes starting with these paths will not be included in the sitemap, enhancing control over the sitemap contents.
     */
    'exclude_paths' => [
    ],

    /**
     * An array of full URLs to be excluded from the sitemap.
     * This allows for fine-grained exclusion of specific pages, such as sitemap files or any other URLs not suitable for search engine indexing.
     */
    'exclude_urls' => [
        '/sitemap.xml',
        '/pages_sitemap.xml',
        '/posts_sitemap.xml',
    ],

    /**
     * Specifies the model class to be used for fetching posts to be included in the sitemap.
     * This setting allows for customization of the source of content, enabling the sitemap to reflect the structure and content of your website accurately.
     * The specified model should implement any necessary logic to retrieve only the posts that should be visible to search engines.
     */
    'post_model' => [
        //App\Models\Post::class,
    ],
];

使用方法

您也可以通过实现 Spatie\Sitemap\Contracts\Sitemapable 接口直接添加您的模型。您还需要在 fv-sitemap.php 配置文件中定义您的 post_model。

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Contracts\Sitemapable;
use Spatie\Sitemap\Tags\Url;

class Post extends Model implements Sitemapable {
    /**
     * Convert the Post model instance into a sitemap URL entry.
     *
     * @return \Spatie\Sitemap\Tags\Url
     */
    public function toSitemapUrl() {
        $url = Url::create(url("{$this->id}"))
            ->setLastModificationDate($this->updated_at)
            ->setChangeFrequency('daily')
            ->setPriority(0.8);

        return $url;
    }
}

您可以使用以下命令生成站点地图

php artisan sitemap:generate

您可以使用以下方式链接到站点地图

route('sitemap', ['filename' => 'sitemap.xml'])

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 变更日志

贡献

有关详细信息,请参阅 贡献指南

安全漏洞

如果您发现与安全相关的错误,请通过 support@fuelviews.com 发送邮件,而不是使用问题跟踪器。

鸣谢

支持我们

Fuelviews 是一家总部位于缅因州波特兰的网站开发公司。您可以在我们的网站上找到我们所有项目的概述 这里

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件