zaichaopan / breadcrumb
为您的laravel应用添加动态面包屑的包
v0.1-beta
2018-06-21 22:01 UTC
Requires
- php: >=7.1.0
- illuminate/support: ~5.5.0|~5.6.0
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ^3.5.0
- phpunit/phpunit: ^7.2
This package is not auto-updated.
Last update: 2024-09-29 05:09:20 UTC
README
此包允许您为laravel应用添加动态面包屑。它受到code course的启发。它可以用于laravel 5.5或更高版本。
安装
composer require zaichaopan/breadcrumb
用法
- 在您想要用于生成面包屑的eloquent模型中实现 BreadcrumbLinkInterface。
为了更好地了解如何使用此包。考虑以下示例:您的应用可以列出不同产品,这些产品可能属于不同的类别。
// web.php Route::get('/categories/{category}/products/{product}', 'ProductsController@show');
// ... class ProductsController extends Controller { public function show(Category $category, Product $product) { // ... return view('product.show', compact('product')); } }
为了在 products.show 视图中生成面包屑链接。您需要在您的 Category 和 Product 模型中实现 BreadcrumbLinkInterface。
BreadcrumbLinkInterface 中只有一个方法:LinkText。它用于在生成面包屑时定义链接的文本。此包需要知道它,因为您可能需要在URL中使用模型ID,或者您可以将ID替换为uuid或slug。我们不想在生成面包屑时使用任何一种作为URL的文本。
在模型中实现接口
// Category.php class Category extends Model implements BreadcrumbLinkInterface { public function linkText() : string { // you want to display category name in the breadcrumb link return $this->name; } }
// Product.php class Product extends Model implements BreadcrumbLinkInterface { public function linkText(): string { // you want to display product name in the breadcrumb link return $this->name; } }
- 在视图中包含面包屑导航链接
此包提供默认的bootstrap4风格的面包屑导航部分视图。
在您的 products.show 视图中包含
@include('breadcrumb::_nav')
如果您想自定义面包屑导航。您可以发布视图。
php artisan vendor:publish
然后选择提供者: Zaichaopan\Breadcrumb\BreadcrumbServiceProvider
这就是您需要做的,以使用此包。