laravel5.6 面包屑扩展laravel-breadcrumbs应用

laravel-breadcrumbs扩展,是一种简单的Laravel风格的方式来创建面包屑。https://packagist.org/packages/davejamesmiller/laravel-breadcrumbs

1.laravel-breadcrumbs 配置信息

Laravel BreadcrumbsLaravelPHP
5.1.1+5.6 – 5.77.1+
5.0.0 – 5.1.05.677.1+
4.x5.57.0+
3.x5.0 – 5.45.4+
2.x4.0 – 4.25.3+

2.安装Laravel Breadcrumbs

在命令行运行:

composer require davejamesmiller/laravel-breadcrumbs:5.x

3.定义面包屑

创建一个名为的文件routes/breadcrumbs.php,如下所示:

<?php

// Home
Breadcrumbs::for('home', function ($trail) {
    $trail->push('Home', route('home'));
});

// Home > About
Breadcrumbs::for('about', function ($trail) {
    $trail->parent('home');
    $trail->push('About', route('about'));
});

// Home > Blog
Breadcrumbs::for('blog', function ($trail) {
    $trail->parent('home');
    $trail->push('Blog', route('blog'));
});

// Home > Blog > [Category]
Breadcrumbs::for('category', function ($trail, $category) {
    $trail->parent('blog');
    $trail->push($category->title, route('category', $category->id));
});

// Home > Blog > [Category] > [Post]
Breadcrumbs::for('post', function ($trail, $post) {
    $trail->parent('category', $post->category);
    $trail->push($post->title, route('post', $post->id));
});

4.选择一个模板

默认情况下,将呈现与Bootstrap兼容的有序列表,因此如果您使用的是Bootstrap 4,则可以跳过此步骤。
首先通过运行以下命令初始化配置文件:

php artisan vendor:publish --provider="DaveJamesMiller\Breadcrumbs\BreadcrumbsServiceProvider"

然后打开config/breadcrumbs.php并编辑此行:

'view' => 'breadcrumbs::bootstrap4',

可能的值是:

  • breadcrumbs::bootstrap4- Bootstrap 4
  • breadcrumbs::bootstrap3- Bootstrap 3
  • breadcrumbs::bootstrap2- Bootstrap 2
  • breadcrumbs::bulma- 布尔玛
  • breadcrumbs::foundation6- 基金会6
  • breadcrumbs::materialize- 实现
  • breadcrumbs::uikit- UIkit
  • breadcrumbs::json-ld- JSON-LD结构化数据(标签,无可见输出)

自定义视图的路径:例如 partials.breadcrumbs

{{ Breadcrumbs::render('home') }}
{{ Breadcrumbs::render('category', $category) }}
阅读 86

Comments