sergeybruhin/html-sitemap

Laravel Html Sitemap 包

1.0.3 2023-11-04 17:13 UTC

This package is auto-updated.

Last update: 2024-09-04 19:50:43 UTC


README

Latest Version on Packagist Total Downloads

这是一个基本且简单的包,可以帮助您在blade布局中生成html站点地图。

安装

您可以通过composer安装此包

composer require sergeybruhin/html-sitemap

发布CSS文件

php artisan vendor:publish --provider="SergeyBruhin\HtmlSitemap\Providers\HtmlSitemapServiceProvider"

在控制器中组合站点地图

在控制器中做任何您想做的,只要别忘了将$htmlSitemap传递给视图。以您需要的方式缓存。

<?php

namespace App\Http\Controllers;

use App\Models\Page;
use App\Models\Post;
use Illuminate\Contracts\View\View;
use SergeyBruhin\HtmlSitemap\Dto\HtmlSitemapCategory;
use SergeyBruhin\HtmlSitemap\HtmlSitemap;

class HtmlSitemapController
{
    public function __invoke(): View
    {
        $htmlSitemap = new HtmlSitemap;
        $htmlSitemap->addCategory($this->getPagesCategory());
        $htmlSitemap->addCategory($this->getPostsCategory());

        return view('your-layout-here')
            ->with('htmlSitemap', $htmlSitemap);
    }

    private function getPagesCategory(): HtmlSitemapCategory
    {
        $category = new HtmlSitemapCategory('Pages', route('pages'));
        Page::each(function (Page $page) use ($category) {
            $category->addLink($page->name, route('page', $page->slug));
        });
        return $category;
    }

    private function getPostsCategory(): HtmlSitemapCategory
    {
        $category = new HtmlSitemapCategory('News', route('posts'));
        Post::each(function (Post $post) use ($category) {
            $category->addLink($post->name, route('blog.show', $post->slug));
        });
        return $category;
    }

}

或者像这样在服务类中组合它,并将结果模型传递给控制器

<?php

namespace App\Services\Sitemap\Handlers;

use App\Models\Post;
use SergeyBruhin\HtmlSitemap\Dto\HtmlSitemapCategory;
use SergeyBruhin\HtmlSitemap\HtmlSitemap;


class GenerateHtmlSitemapHandler
{
    public function run(): HtmlSitemap
    {
        $htmlSitemap = new HtmlSitemap;
        $htmlSitemap->addCategory($this->getPostsCategory());
        $htmlSitemap->addCategory($this->getPagesCategory());


        return $htmlSitemap;
    }

    private function getPagesCategory(): HtmlSitemapCategory
    {
        $category = new HtmlSitemapCategory('Pages');

        $category->addLink('Resume', route('resume'));
        $category->addLink('Crypto', route('crypto'));
        $category->addLink('Dashboard', route('dashboard'));
        $category->addLink('Privacy Policy', route('privacy-policy'));
        $category->addLink('Data Deletion Instruction', route('data-deletion-instruction'));
        $category->addLink('Terms', route('terms'));

        return $category;
    }

    private function getPostsCategory(): HtmlSitemapCategory
    {
        $category = new HtmlSitemapCategory('Blog', route('posts'));
        Post::each(function (Post $post) use ($category) {
            $category->addLink($post->title, route('posts.show', $post->slug));
        });
        return $category;
    }
}

<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Services\Sitemap\Interfaces\SitemapServiceContract;

class HtmlSitemapController extends Controller
{
    public function __invoke(SitemapServiceContract $sitemapService)
    {
        $htmlSitemap = $sitemapService->generateHtmlSitemap();
        return view('frontend.pages.sitemap.master')
            ->with('htmlSitemap', $htmlSitemap);
    }
}

渲染站点地图

您可以在您喜欢的位置渲染站点地图。

 @include('html-sitemap::widget')

如果变量$htmlSitemap被设置,小部件将被渲染。

注册您的控制器路由

对URL或路由名称没有限制,您可以做您想做的事。

Route::get('sitemap', HtmlSitemapController::class)
    ->middleware('cache.headers:private;max_age=3600;etag')
    ->name('html-sitemap');

待办事项

  • 添加更改包小部件模板的能力
  • 添加缓存站点图小部件的能力
  • 添加以独立HTML页面显示站点地图的能力

测试(尚未💁‍♂️)

composer test

变更日志

请参阅CHANGELOG以获取更多关于最近更改的信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全性

如果您发现任何与安全性相关的问题,请通过电子邮件sundaycreative@gmail.com联系,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。