spatie / laravel-navigation
管理Laravel应用中的菜单、面包屑和其他导航元素
1.3.0
2024-09-20 13:15 UTC
Requires
- php: ^8.0
- spatie/url: ^1.3.5|^2.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.23|^7.0|^8.0
- pestphp/pest-plugin-laravel: ^1.3
- phpunit/phpunit: ^9.5
- spatie/pest-plugin-snapshots: ^1.1
- spatie/phpunit-snapshot-assertions: ^4.2
README
管理Laravel应用中的菜单、面包屑和其他导航元素
Laravel Navigation旨在成为Laravel Menu的精神继承者。Laravel Menu将继续得到积极维护,但这两个包之间有一些主要区别。
Laravel Menu的主要目标是使用PHP构建HTML菜单。Laravel Navigation描述了一个应用的导航树,可以用作创建菜单、面包屑等导航元素的基础。Laravel Menu具有丰富的API用于生成HTML。Laravel Navigation不执行任何HTML生成(尽管我们可能会在未来提供一些Blade文件)。相反,Laravel Navigation应该为您提供一个灵活性,以便在不担心导航树和激活状态复杂性的情况下构建自己的UI。将其视为一个无渲染组件。
Navigation::make() ->add('Home', route('home')) ->add('Blog', route('blog.index'), fn (Section $section) => $section ->add('All posts', route('blog.index')) ->add('Topics', route('blog.topics.index')) ) ->addIf( Auth::user()->isAdmin(), 'Admin', route('admin.index'), fn (Section $section) => $section->add('Create post', route('blog.create')) );
如果您想在服务提供程序中注册导航,这是推荐的,可以使用Laravel容器事件向导航添加项。
namespace App\Providers; use Illuminate\Support\ServiceProvider; use Spatie\Navigation\Navigation; use Spatie\Navigation\Section; class NavigationServiceProvider extends ServiceProvider { public function register(): void { $this->app->resolving(Navigation::class, function (Navigation $navigation): Navigation { return $navigation ->add('Home', route('home')) ->add('Blog', route('blog.index'), fn (Section $section) => $section ->add('All posts', route('blog.index')) ->add('Topics', route('blog.topics.index')) ) ->addIf( Auth::user()->isAdmin(), 'Admin', route('admin.index'), fn (Section $section) => $section->add('Create post', route('blog.create')) ); }); } }
导航对象可以渲染成树形结构或面包屑。
以下是一些示例,当访问/blog/topics/laravel
时
// Render to tree Navigation::make()->tree();
[ { "title": "Home", "url": "/", "active": false, "attributes": [], "children": [], "depth": 0 }, { "title": "Blog", "url": "/blog", "active": false, "attributes": [], "children": [ { "title": "All posts", "url": "/blog", "active": false, "attributes": [], "children": [], "depth": 1 }, { "title": "Topics", "url": "/blog/topics", "active": true, "attributes": [], "children": [], "depth": 1 } ], "depth": 0 }, { "title": "Admin", "url": "/admin", "active": false, "attributes": [], "children": [ { "title": "Create post", "url": "/blog/create", "active": false, "attributes": [], "children": [], "depth": 1 } ], "depth": 0 } ]
// Append additional pages in your controller Navigation::make()->activeSection()->add($topic->name, route('blog.topics.show', $topic)); // Render to breadcrumbs Navigation::make()->breadcrumbs();
[ { "title": "Blog", "url": "/blog", "attributes": [] }, { "title": "Topics", "url": "/blog/topics", "attributes": [] }, { "title": "Laravel", "url": "/blog/topics/laravel", "attributes": [] } ]
// Render the current section Navigation::make()->current();
{ "title": "Home", "url": "/", "attributes": [] }
支持我们
我们投入了大量资源来创建一流的开放源代码包。您可以通过购买我们的付费产品之一来支持我们。
我们非常感激您从家乡寄给我们明信片,注明您正在使用我们哪些包。您可以在我们的联系页面上找到我们的地址。我们将在我们的虚拟明信片墙上发布所有收到的明信片。
安装
您可以通过composer安装此包
composer require spatie/laravel-navigation
测试
composer test
更新日志
有关最近更改的更多信息,请参阅更新日志。
贡献
有关详细信息,请参阅贡献。
安全
如果您发现有关安全性的bug,请通过security@spatie.be发送邮件,而不是使用问题跟踪器。
鸣谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。