In this new post to his site Freek Van der Herten shares a new package he's worked up to help generate and maintain the status of menus in a Laravel application. While this example is Laravel-centric, there's also a framework-agnostic package that can be used in any application structure too.
Virtually every website displays some sort of menu. Generating html menus might seem simple, but it can become complex very quickly. Not only do you have to render some basic html, but you also have to manage which item is active. If a menu has a submenu you’ll also want the parents of an active item to be active. Sometimes you want to insert some html between menu items.
There are some packages out there that can help generating menus, but most of them have a messy API or have become victims of feature creep. Thanks why we decided to create our own modern menu package that has a beautiful API to work with.
He spends the rest of the post introducing the package, starting with the generation of a basic menu (and something a bit more complex). He also shows the use of the
isActive method call to mark something as "active" but the package will handle that automatically for you if you'd like to keep it simple. He ends the post with a listing of the components that make this menu handling work (three of them) and some of the "modern PHP" functionality that they use.