Manage navigational elements in Laravel apps

Packages

June 14th, 2022

laravel-navigation-featured.png

Laravel Navigation is a package by Spatie to manage menus, breadcrumbs, and other navigational elements in Laravel apps.

While the Spatie Laravel Menu package is a Html menu generator for Laravel, think of this package as a "renderless component" for navigation components:

1app(Navigation::class)
2 ->add('Home', route('home'))
3 ->add('Blog', route('blog.index'), function (Section $section) {
4 $section
5 ->add('All posts', route('blog.index'))
6 ->add('Topics', route('blog.topics.index'));
7 })
8 ->addIf(Auth::user()->isAdmin(), function (Navigation $navigation) {
9 $navigation->add('Admin', route('admin.index'));
10 });
11 
12// Render to tree
13app(Navigation::class)->tree();
14 
15/*
16 
17[
18 { "title": "Home", "url": "/", "active": false, "children": [] },
19 {
20 "title": "Blog",
21 "url": "/blog",
22 "active": false,
23 "children": [
24 { "title": "All posts", "url": "/blog", "active": false, "children": [] },
25 { "title": "Topics", "url": "/blog/topics", "active": true, "children": [] }
26 ],
27 },
28 { "title": "Admin", "url": "/admin", "active": false, "children": [] }
29]
30 
31*/

With this package you and also generate breadcrumbs from navigation using the following method:

1// Append additional pages in your controller
2app(Navigation::class)->activeSection()->add($topic->name, route('blog.topics.show', $topic));
3 
4// Render to breadcrumbs
5app(Navigation::class)->breadcrumbs();
6 
7/*
8[
9 { "title": "Blog", "url": "/blog" },
10 { "title": "Topics", "url": "/blog/topics" },
11 { "title": "Laravel", "url": "/blog/topics/laravel" }
12]
13*/

You can learn about this package, get full installation instructions, and view the source code on GitHub. Thanks to Sebastian De Deyne and the Spatie team for this package, and all the fantastic open-source PHP and Laravel packages like this one 👏

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners