定制 web-id/breadcrumb 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

web-id/breadcrumb

最新稳定版本:3.0.1

Composer 安装命令:

composer require web-id/breadcrumb

包简介

Make breadcrumbs using a route macro

README 文档

README

Installation

Requires PHP 8.3+, Laravel 12 or 13, and inertiajs/inertia-laravel 2.x or 3.x when using the Inertia frontend option. (This package does not support PHP 8.2, even though Laravel 12 allows it upstream.)

You can install the package via composer:

composer require web-id/breadcrumb

Publish config file

php artisan vendor:publish --provider="WebId\Breadcrumb\BreadcrumbServiceProvider" --tag="config"

Usage

This package uses Spatie's Laravel Navigation package (https://github.com/spatie/laravel-navigation).

Frontend

If you are using Inertia, update the frontend key to the config file to inertia.

'frontend' => 'inertia',

If your project uses Blade templates, set the config to blade.

'frontend' => 'blade',

Breadcrumb root element

Edit the breadcrumb_root title and route name in your published breadcrumb.php config file.

For instance, if your root element is your homepage:

'breadcrumb_root' => [
        'title' => 'Homepage',
        'route_name' => 'homepage',
    ],

Note
Your route must be named.

Breadcrumb class

To create a new breadcrumb, create a class that extends the WebId\Breadcrumb\Breadcrumb class.

In this example, we will create a breadcrumb for the blog page (list of all articles):

use WebId\Breadcrumb\Breadcrumb;

class BlogBreadcrumb extends Breadcrumb
{
    public function index(): array
    {
        return $this->render(
            $this->baseBreadcrumb()
                ->add('Blog')
                ->tree()
        );
    }
}

Notice that the add() method only takes a name (blog). Indeed, the last element of a breadcrumb being the active page, you don't need to attach a link to it.

In this example, your view should have a breadcrumb key with this data:

[
    {
        "url": "https://www.yourwebsite.com",
        "title": "Homepage"
    },
    {
        "title": "Blog"
    }
]

If you want to create the breadcrumb for a single blog post, you probably want the parent element to be the blog page. In that case, add a method as follow:

public function show(Post $post): array
{
    return $this->render(
        $this->baseBreadcrumb()
            ->add('Blog', route('blog.index'))
            ->add($post->title, route('blog.show', ['post' => $post]))
            ->tree()
    );
}

Notice that your can Typehint a model in your breadcrumb methods as if you were in a controller method.

In this example, your view should have a breadcrumb key with this data:

[
    {
        "url": "https://www.yourwebsite.com",
        "title": "Homepage"
    },
    {
        "url": "https://www.yourwebsite.com/blog",
        "title": "Blog"
    },
    {
        "title": "Article title"
    }
]

Register a breadcrumb to a route

In order for your breadcrumb to be accessible in your Inertia view, you have to register it to the associated route as follow:

Route::get('/blog', [BlogController::class, 'index'])->breadcrumb([BlogBreadcrumb:class, 'index']);
Route::get('/blog/{post}', [BlogController::class, 'show'])->breadcrumb([BlogBreadcrumb:class, 'show']);

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 14.65k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 5
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固