aelora/laravel-breadcrumbs

为Laravel快速便捷地创建面包屑导航

0.1.0 2022-07-10 01:23 UTC

This package is auto-updated.

Last update: 2024-09-22 00:51:40 UTC


README

Latest Version on Packagist Total Downloads

快速为Laravel页面构建面包屑路径,并自动输出HTML和JSON-LD元数据。

安装

您可以通过Composer安装此包

composer require aelora/laravel-breadcrumbs

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="breadcrumbs-config"

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

return [
    'home' => [

        // Should the home link be automatically included in the breadcrumbs? You can also manually include
        // or exclude the home link when you build the breadcrumb class.
        'include' => true,

        'text' => 'Home',

        'url' => url('/'),

        'image' => '',
    ],

    // Which blade template to use. Currently only tailwind is supported. 
    'theme' => 'tailwind',

    // View to use to generate the HTML breadcrumbs. If defined, this will
    // override the theme above.
    'view' => '',

    // Character to use as a separator between crumbs. Use and encoded entity
    // if you want something like > 
    'separator' => '/',
];

可选地,您可以使用以下命令发布视图

php artisan vendor:publish --tag="breadcrumbs-views"

目前只有两个视图:jsonld.blade.php,用于Schema元数据,以及breadcrumbs-tailwind.blade.css

使用方法

// In your controller
use Aelora\Breadcrumbs\Breadcrumbs;
$breadcrumbs = Breadcrumbs::create()
    ->add('Page #1', url('/page-1'))
    ->add('Page #2', url('/page-2'))
    ->add('Current Page');

return view('some.view.file', ['breadcrumbs' => $breadcrumbs]);
// In your view where ever your want the breadcrumbs
{!! $breadcrumbs !!}

您还可以调用 $breadcrumbs->generate() 达到相同的效果。 Breadcrumbs 实现了 Stringable 并具有 __toString() 方法,所以您不需要这样做。 __toString() 会调用 generate()

注意,我们使用 {!!!!} 而不是 {{}}。我们不希望面包屑的内容被转义。

主页

您不需要显式地将您的首页添加到面包屑中。使用默认配置,它将自动作为输出前的第一个元素添加。如果您不想包含首页链接,可以在配置文件中将 home.include 设置为 false,或者在创建面包屑时调用 setHome(false)

方法

public function add(string $title, string $url = '', string $image = '')

在当前堆栈末尾添加一个新的面包屑链接。

唯一必需的参数是 $title。如果 $url 为空,则面包屑将只显示为链接。如果它不为空,则 $title 将显示为无链接。 $image 是面包屑的图片链接。它目前仅在Schema元数据中使用,不在可见的HTML输出中使用,尽管如果您构建自己的视图,它也可以使用。

public function count()

返回面包屑项的数量,不包括首页链接。

public static function create()

返回一个新实例,这样您可以在一行中构建,而无需调用 new Breadcrumbs()

public function generate($echo = false)

返回生成的面包屑,包括可见的HTML和Schema元数据。如果 $echotrue,则面包屑也会在返回之前输出。

public function reset()

清除面包屑路径。

public function reverse()

反转内部面包屑路径。有时从后往前构建路径并在最后翻转它更容易。

public function setHome($title, string $url = '', string $image = '')

允许您更改单个页面的首页链接,而无需更改配置文件。参数与 add 方法相同。

如果 $titlefalse,则不会包含首页链接。否则它应该是一个 string

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可证

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